88 InputEvent ,
99 DynamicForm ,
1010 FormFields ,
11+ FormOptions ,
1112} from '@/core/models' ;
1213import {
1314 computed ,
@@ -27,6 +28,7 @@ interface DynamicFormComposition {
2728 controls : Ref < FormControl < InputType > [ ] > ;
2829 forceValidation : Ref < boolean > ;
2930 formValues : ComputedRef < ( string | { [ key : string ] : boolean } ) [ ] > ;
31+ formOptions : Ref < FormOptions > ;
3032 errors : ComputedRef < Record < string , unknown > > ;
3133 isValid : ComputedRef < boolean > ;
3234 normalizedControls : ComputedRef < Record < string , unknown > > ;
@@ -42,6 +44,7 @@ interface DynamicFormComposition {
4244 findControlByName : ( name : string | unknown ) => FormControl < InputType > ;
4345 resetForm : ( ) => void ;
4446 detectChanges : ( fields : FormFields ) => void ;
47+ onOptionsChanged : ( options : FormOptions ) => void ;
4548}
4649
4750export function useDynamicForm (
@@ -56,6 +59,11 @@ export function useDynamicForm(
5659 let cache = deepClone ( toRaw ( form . fields ) ) ;
5760
5861 const controls : Ref < FormControl < InputType > [ ] > = ref ( [ ] ) ;
62+ const formOptions : Ref < FormOptions > = ref ( {
63+ resetAfterSubmit : true ,
64+ ...options ?. form ,
65+ ...form ?. options ,
66+ } ) ;
5967 const forceValidation = ref ( false ) ;
6068
6169 const deNormalizedScopedSlots = computed ( ( ) => Object . keys ( ctx . slots ) ) ;
@@ -107,10 +115,7 @@ export function useDynamicForm(
107115 } ) ;
108116
109117 const formattedOptions = computed ( ( ) => {
110- const opts = {
111- ...options ?. form ,
112- ...form ?. options ,
113- } ;
118+ const opts = formOptions . value ;
114119
115120 if ( opts ) {
116121 const {
@@ -220,6 +225,10 @@ export function useDynamicForm(
220225 cache = deepClone ( toRaw ( fields ) ) ;
221226 }
222227
228+ function onOptionsChanged ( changes ) {
229+ Object . assign ( formOptions . value , changes ) ;
230+ }
231+
223232 function resetForm ( ) {
224233 mapControls ( true ) ;
225234 forceValidation . value = false ;
@@ -232,7 +241,9 @@ export function useDynamicForm(
232241
233242 if ( isValid . value ) {
234243 ctx . emit ( 'submitted' , formValues . value ) ;
235- resetForm ( ) ;
244+ if ( formOptions . value . resetAfterSubmit ) {
245+ resetForm ( ) ;
246+ }
236247 } else {
237248 ctx . emit ( 'error' , errors . value ) ;
238249 }
@@ -251,6 +262,7 @@ export function useDynamicForm(
251262 mapControls,
252263 valueChange,
253264 formValues,
265+ formOptions,
254266 handleSubmit,
255267 isValid,
256268 errors,
@@ -264,5 +276,6 @@ export function useDynamicForm(
264276 findControlByName,
265277 resetForm,
266278 detectChanges,
279+ onOptionsChanged,
267280 } ;
268281}
0 commit comments