8
8
InputEvent ,
9
9
DynamicForm ,
10
10
FormFields ,
11
+ FormOptions ,
11
12
} from '@/core/models' ;
12
13
import {
13
14
computed ,
@@ -27,6 +28,7 @@ interface DynamicFormComposition {
27
28
controls : Ref < FormControl < InputType > [ ] > ;
28
29
forceValidation : Ref < boolean > ;
29
30
formValues : ComputedRef < ( string | { [ key : string ] : boolean } ) [ ] > ;
31
+ formOptions : Ref < FormOptions > ;
30
32
errors : ComputedRef < Record < string , unknown > > ;
31
33
isValid : ComputedRef < boolean > ;
32
34
normalizedControls : ComputedRef < Record < string , unknown > > ;
@@ -42,6 +44,7 @@ interface DynamicFormComposition {
42
44
findControlByName : ( name : string | unknown ) => FormControl < InputType > ;
43
45
resetForm : ( ) => void ;
44
46
detectChanges : ( fields : FormFields ) => void ;
47
+ onOptionsChanged : ( options : FormOptions ) => void ;
45
48
}
46
49
47
50
export function useDynamicForm (
@@ -56,6 +59,11 @@ export function useDynamicForm(
56
59
let cache = deepClone ( toRaw ( form . fields ) ) ;
57
60
58
61
const controls : Ref < FormControl < InputType > [ ] > = ref ( [ ] ) ;
62
+ const formOptions : Ref < FormOptions > = ref ( {
63
+ resetAfterSubmit : true ,
64
+ ...options ?. form ,
65
+ ...form ?. options ,
66
+ } ) ;
59
67
const forceValidation = ref ( false ) ;
60
68
61
69
const deNormalizedScopedSlots = computed ( ( ) => Object . keys ( ctx . slots ) ) ;
@@ -107,10 +115,7 @@ export function useDynamicForm(
107
115
} ) ;
108
116
109
117
const formattedOptions = computed ( ( ) => {
110
- const opts = {
111
- ...options ?. form ,
112
- ...form ?. options ,
113
- } ;
118
+ const opts = formOptions . value ;
114
119
115
120
if ( opts ) {
116
121
const {
@@ -220,6 +225,10 @@ export function useDynamicForm(
220
225
cache = deepClone ( toRaw ( fields ) ) ;
221
226
}
222
227
228
+ function onOptionsChanged ( changes ) {
229
+ Object . assign ( formOptions . value , changes ) ;
230
+ }
231
+
223
232
function resetForm ( ) {
224
233
mapControls ( true ) ;
225
234
forceValidation . value = false ;
@@ -232,7 +241,9 @@ export function useDynamicForm(
232
241
233
242
if ( isValid . value ) {
234
243
ctx . emit ( 'submitted' , formValues . value ) ;
235
- resetForm ( ) ;
244
+ if ( formOptions . value . resetAfterSubmit ) {
245
+ resetForm ( ) ;
246
+ }
236
247
} else {
237
248
ctx . emit ( 'error' , errors . value ) ;
238
249
}
@@ -251,6 +262,7 @@ export function useDynamicForm(
251
262
mapControls,
252
263
valueChange,
253
264
formValues,
265
+ formOptions,
254
266
handleSubmit,
255
267
isValid,
256
268
errors,
@@ -264,5 +276,6 @@ export function useDynamicForm(
264
276
findControlByName,
265
277
resetForm,
266
278
detectChanges,
279
+ onOptionsChanged,
267
280
} ;
268
281
}
0 commit comments