File tree Expand file tree Collapse file tree 3 files changed +53
-2
lines changed
apps/docs2/src/content/docs Expand file tree Collapse file tree 3 files changed +53
-2
lines changed Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ export interface FormOptions<T, V extends Validator>
50
50
idPseudoSeparator? : string ;
51
51
//
52
52
value? : [() => T , (v : T ) => void ];
53
- initialValue? : Partial <T >;
53
+ initialValue? : InitialValue <T >;
54
54
initialErrors? : InitialErrors <V >;
55
55
/**
56
56
* @default waitPrevious
@@ -143,5 +143,6 @@ export interface FormOptions<T, V extends Validator>
143
143
*/
144
144
onReset? : (e : Event ) => void ;
145
145
schedulerYield? : SchedulerYield ;
146
+ keyedArraysMap? : KeyedArraysMap ;
146
147
}
147
148
```
Original file line number Diff line number Diff line change @@ -38,4 +38,48 @@ export interface FormState<T, V extends Validator> {
38
38
submit(e : SubmitEvent ): void ;
39
39
reset(e : Event ): void ;
40
40
}
41
- ```
41
+ ```
42
+
43
+ ## Direct modification of form state
44
+
45
+ If you are using a [ controlled form] ( ../../guides/quickstart/#controlled-form ) ,
46
+ you should consider the following aspects:
47
+
48
+ ### Initialization
49
+
50
+ It is recommended to initialize the state as follows:
51
+
52
+ ``` ts
53
+ let value = $state (
54
+ merger .mergeFormDataAndSchemaDefaults (initialValue , schema )
55
+ );
56
+ ```
57
+
58
+ ### Arrays
59
+
60
+ To modify arrays, use one of the following methods:
61
+
62
+ 1 . Reassign
63
+
64
+ ``` ts
65
+ value .array = value .array .concat (123 )
66
+ ```
67
+
68
+ 2 . Use ` KeyedArray ` API
69
+
70
+ ``` ts
71
+ import { createForm , type KeyedArraysMap } from " @sjsf/form" ;
72
+
73
+ const keyedArraysMap: KeyedArraysMap = new WeakMap ()
74
+ let value = $state ({ array: [] })
75
+ const form = createForm ({
76
+ keyedArraysMap ,
77
+ value: [() => value , (v ) => (value = v )],
78
+ // ...otherOptions
79
+ })
80
+
81
+ const api = keyedArraysMap .get (value .array )
82
+ if (api ) {
83
+ api .push (123 )
84
+ }
85
+ ```
Original file line number Diff line number Diff line change @@ -105,6 +105,12 @@ so here is a list of situations in which this approach can be used
105
105
106
106
:::
107
107
108
+ :::caution
109
+
110
+ Before using, please read the section - [ Direct modification of form state] ( ../../form/state/#direct-modification-of-form-state )
111
+
112
+ :::
113
+
108
114
<Code code = { controlledFormCode } lang = " svelte" />
109
115
110
116
<FormCard >
You can’t perform that action at this time.
0 commit comments