Skip to content

Commit 13b2663

Browse files
committed
[docs] Add info about direct array modification
1 parent a3992b9 commit 13b2663

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

apps/docs2/src/content/docs/form/options.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export interface FormOptions<T, V extends Validator>
5050
idPseudoSeparator?: string;
5151
//
5252
value?: [() => T, (v: T) => void];
53-
initialValue?: Partial<T>;
53+
initialValue?: InitialValue<T>;
5454
initialErrors?: InitialErrors<V>;
5555
/**
5656
* @default waitPrevious
@@ -143,5 +143,6 @@ export interface FormOptions<T, V extends Validator>
143143
*/
144144
onReset?: (e: Event) => void;
145145
schedulerYield?: SchedulerYield;
146+
keyedArraysMap?: KeyedArraysMap;
146147
}
147148
```

apps/docs2/src/content/docs/form/state.mdx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,48 @@ export interface FormState<T, V extends Validator> {
3838
submit(e: SubmitEvent): void;
3939
reset(e: Event): void;
4040
}
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+
```

apps/docs2/src/content/docs/guides/quickstart.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ so here is a list of situations in which this approach can be used
105105

106106
:::
107107

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+
108114
<Code code={controlledFormCode} lang="svelte" />
109115

110116
<FormCard>

0 commit comments

Comments
 (0)