Skip to content

Commit 787889d

Browse files
committed
feat(form): resetOnSuccess: false
1 parent 3372faa commit 787889d

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

packages/vue/src/form.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,43 @@ export interface CreateFormObjectOptions<
99
TAdditionalProps,
1010
TResult extends TData | void = TData,
1111
> {
12+
/**
13+
* Function returning the default values for the form
14+
*
15+
* If not provided, the form will be initialized with an empty object
16+
*
17+
* If `resetDefaultValues` is provided, `defaultValues` will only be used for the initial values
18+
*/
1219
defaultValues?: (() => Partial<TData>) | undefined
20+
/**
21+
* Function returning the default values for the form when `$reset()` is called. `resetDefaultValues` is **not** called when initializing the form.
22+
*
23+
* If not provided, `defaultValues` will be used instead
24+
*
25+
* If neither `resetDefaultValues` nor `defaultValues` are provided, the form will be reset to an empty object
26+
*/
1327
resetDefaultValues?: (() => Awaitable<Partial<TData>>) | undefined
28+
/**
29+
* Schema to validate the form data against. It should be compatible with Standard Schema v1.
30+
*/
1431
schema?: TSchema
32+
/**
33+
* Function to transform the data before submission (e.g. to remove extra properties).
34+
*/
1535
transformData?: (data: Partial<TData>) => Partial<TData>
36+
/**
37+
* Function called when the form is submitted.
38+
*/
1639
submit: (data: Partial<TData>) => Promise<TResult>
40+
/**
41+
* Additional properties to add to the form object. Their name must start with `$` to avoid conflicts with form fields.
42+
*/
1743
additionalProps?: TAdditionalProps
44+
/**
45+
* Resets the form to default values after a successful submission
46+
* @default true
47+
*/
48+
resetOnSuccess?: boolean
1849
}
1950

2051
export type FormObjectChanged<TData> = {
@@ -111,7 +142,9 @@ export function createFormObject<
111142
}
112143
const item = await options.submit(data)
113144
onSuccess.trigger(item)
114-
await form.$reset()
145+
if (options.resetOnSuccess || options.resetOnSuccess == null) {
146+
await form.$reset()
147+
}
115148
return item
116149
}
117150
catch (error: any) {

packages/vue/test/form.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { createFormObject } from '../src'
3+
4+
describe('createFormObject', () => {
5+
it('calls submit', async () => {
6+
let submittedData: any = null
7+
const obj = createFormObject({
8+
defaultValues: () => ({ name: 'John' }),
9+
submit: async (data) => {
10+
submittedData = data
11+
},
12+
})
13+
14+
obj.name = 'Jane'
15+
await obj.$submit()
16+
17+
expect(submittedData).toEqual({ name: 'Jane' })
18+
})
19+
20+
it('auto resets the form', async () => {
21+
const obj = createFormObject({
22+
defaultValues: () => ({ name: 'John' }),
23+
submit: async () => {
24+
// Meow
25+
},
26+
})
27+
28+
obj.name = 'Jane'
29+
30+
await obj.$submit()
31+
32+
expect(obj.name).toBe('John')
33+
})
34+
35+
it('not auto resets the form when `autoReset` is false', async () => {
36+
const obj = createFormObject({
37+
defaultValues: () => ({ name: 'John' }),
38+
submit: async () => {
39+
// Meow
40+
},
41+
resetOnSuccess: false,
42+
})
43+
44+
obj.name = 'Jane'
45+
46+
await obj.$submit()
47+
48+
expect(obj.name).toBe('Jane')
49+
})
50+
})

0 commit comments

Comments
 (0)