@ilokesto/form 1.0.3 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.ko.md +153 -487
- package/README.md +155 -490
- package/dist/FormInput-DuT7QFe-.d.ts +37 -0
- package/dist/chunk-4F4SAGQ5.js +1465 -0
- package/dist/chunk-4F4SAGQ5.js.map +1 -0
- package/dist/chunk-JK644RR7.js +215 -0
- package/dist/chunk-JK644RR7.js.map +1 -0
- package/dist/index.d.ts +58 -2
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -0
- package/dist/react/index.d.ts +87 -2
- package/dist/react/index.js +131 -1
- package/dist/react/index.js.map +1 -0
- package/dist/solid/index.d.ts +89 -2
- package/dist/solid/index.js +170 -1
- package/dist/solid/index.js.map +1 -0
- package/dist/svelte/index.d.ts +68 -2
- package/dist/svelte/index.js +198 -1
- package/dist/svelte/index.js.map +1 -0
- package/dist/types-DBlvL6AC.d.ts +229 -0
- package/dist/vue/index.d.ts +87 -2
- package/dist/vue/index.js +192 -1
- package/dist/vue/index.js.map +1 -0
- package/package.json +15 -8
- package/dist/adapters/FormInput.d.ts +0 -8
- package/dist/adapters/FormInput.js +0 -10
- package/dist/adapters/FormStateSummary.d.ts +0 -14
- package/dist/adapters/FormStateSummary.js +0 -27
- package/dist/adapters/dom/FieldValue.d.ts +0 -2
- package/dist/adapters/dom/FieldValue.js +0 -34
- package/dist/adapters/dom/RegisterBinding.d.ts +0 -14
- package/dist/adapters/dom/RegisterBinding.js +0 -110
- package/dist/adapters/dom/SubmitHandler.d.ts +0 -9
- package/dist/adapters/dom/SubmitHandler.js +0 -6
- package/dist/adapters/dom/index.d.ts +0 -5
- package/dist/adapters/dom/index.js +0 -3
- package/dist/adapters/dom/types.d.ts +0 -22
- package/dist/adapters/dom/types.js +0 -1
- package/dist/core/array/ArrayItemReorder.d.ts +0 -6
- package/dist/core/array/ArrayItemReorder.js +0 -24
- package/dist/core/array/ArrayKeyGenerator.d.ts +0 -4
- package/dist/core/array/ArrayKeyGenerator.js +0 -7
- package/dist/core/array/FormArrayController.d.ts +0 -21
- package/dist/core/array/FormArrayController.js +0 -54
- package/dist/core/array/FormArrayFactory.d.ts +0 -8
- package/dist/core/array/FormArrayFactory.js +0 -13
- package/dist/core/array/FormArrayMutationPlanner.d.ts +0 -14
- package/dist/core/array/FormArrayMutationPlanner.js +0 -68
- package/dist/core/array/FormArrayPath.d.ts +0 -6
- package/dist/core/array/FormArrayPath.js +0 -11
- package/dist/core/array/FormArrayRebaser.d.ts +0 -7
- package/dist/core/array/FormArrayRebaser.js +0 -54
- package/dist/core/array/index.d.ts +0 -1
- package/dist/core/array/index.js +0 -1
- package/dist/core/form/CreateForm.d.ts +0 -23
- package/dist/core/form/CreateForm.js +0 -64
- package/dist/core/form/FormFieldCommands.d.ts +0 -15
- package/dist/core/form/FormFieldCommands.js +0 -40
- package/dist/core/form/FormSubmitter.d.ts +0 -9
- package/dist/core/form/FormSubmitter.js +0 -26
- package/dist/core/form/index.d.ts +0 -1
- package/dist/core/form/index.js +0 -1
- package/dist/core/index.d.ts +0 -2
- package/dist/core/index.js +0 -1
- package/dist/core/path/FormPath.d.ts +0 -9
- package/dist/core/path/FormPath.js +0 -36
- package/dist/core/path/index.d.ts +0 -1
- package/dist/core/path/index.js +0 -1
- package/dist/core/state/FieldStateFactory.d.ts +0 -6
- package/dist/core/state/FieldStateFactory.js +0 -19
- package/dist/core/state/FormStateInitializer.d.ts +0 -7
- package/dist/core/state/FormStateInitializer.js +0 -45
- package/dist/core/state/FormStateReader.d.ts +0 -11
- package/dist/core/state/FormStateReader.js +0 -30
- package/dist/core/state/FormStateStore.d.ts +0 -24
- package/dist/core/state/FormStateStore.js +0 -66
- package/dist/core/state/FormStateWriter.d.ts +0 -17
- package/dist/core/state/FormStateWriter.js +0 -107
- package/dist/core/state/index.d.ts +0 -1
- package/dist/core/state/index.js +0 -1
- package/dist/core/types.d.ts +0 -112
- package/dist/core/types.js +0 -1
- package/dist/core/validation/StandardSchemaValidator.d.ts +0 -14
- package/dist/core/validation/StandardSchemaValidator.js +0 -63
- package/dist/core/validation/ValidationEngine.d.ts +0 -25
- package/dist/core/validation/ValidationEngine.js +0 -116
- package/dist/core/validation/index.d.ts +0 -1
- package/dist/core/validation/index.js +0 -1
- package/dist/core/value/ValueHelper.d.ts +0 -7
- package/dist/core/value/ValueHelper.js +0 -66
- package/dist/core/value/index.d.ts +0 -1
- package/dist/core/value/index.js +0 -1
- package/dist/react/RegisterBinding.d.ts +0 -5
- package/dist/react/RegisterBinding.js +0 -21
- package/dist/react/types.d.ts +0 -73
- package/dist/react/types.js +0 -1
- package/dist/react/useField.d.ts +0 -3
- package/dist/react/useField.js +0 -23
- package/dist/react/useFieldSchemaRegistration.d.ts +0 -4
- package/dist/react/useFieldSchemaRegistration.js +0 -25
- package/dist/react/useFieldState.d.ts +0 -3
- package/dist/react/useFieldState.js +0 -6
- package/dist/react/useForm.d.ts +0 -4
- package/dist/react/useForm.js +0 -30
- package/dist/react/useFormSnapshot.d.ts +0 -2
- package/dist/react/useFormSnapshot.js +0 -4
- package/dist/react/useFormState.d.ts +0 -3
- package/dist/react/useFormState.js +0 -7
- package/dist/react/useRegister.d.ts +0 -6
- package/dist/react/useRegister.js +0 -26
- package/dist/solid/RegisterBinding.d.ts +0 -5
- package/dist/solid/RegisterBinding.js +0 -28
- package/dist/solid/types.d.ts +0 -71
- package/dist/solid/types.js +0 -1
- package/dist/solid/useField.d.ts +0 -3
- package/dist/solid/useField.js +0 -28
- package/dist/solid/useFieldSchemaRegistration.d.ts +0 -3
- package/dist/solid/useFieldSchemaRegistration.js +0 -15
- package/dist/solid/useForm.d.ts +0 -4
- package/dist/solid/useForm.js +0 -13
- package/dist/solid/useFormSnapshot.d.ts +0 -3
- package/dist/solid/useFormSnapshot.js +0 -9
- package/dist/solid/useFormState.d.ts +0 -3
- package/dist/solid/useFormState.js +0 -38
- package/dist/solid/useRegister.d.ts +0 -6
- package/dist/solid/useRegister.js +0 -21
- package/dist/svelte/RegisterAction.d.ts +0 -4
- package/dist/svelte/RegisterAction.js +0 -121
- package/dist/svelte/types.d.ts +0 -13
- package/dist/svelte/types.js +0 -1
- package/dist/svelte/useForm.d.ts +0 -4
- package/dist/svelte/useForm.js +0 -17
- package/dist/svelte/useFormState.d.ts +0 -4
- package/dist/svelte/useFormState.js +0 -10
- package/dist/vue/RegisterBinding.d.ts +0 -5
- package/dist/vue/RegisterBinding.js +0 -28
- package/dist/vue/types.d.ts +0 -71
- package/dist/vue/types.js +0 -1
- package/dist/vue/useField.d.ts +0 -3
- package/dist/vue/useField.js +0 -28
- package/dist/vue/useFieldSchemaRegistration.d.ts +0 -3
- package/dist/vue/useFieldSchemaRegistration.js +0 -17
- package/dist/vue/useForm.d.ts +0 -4
- package/dist/vue/useForm.js +0 -13
- package/dist/vue/useFormSnapshot.d.ts +0 -3
- package/dist/vue/useFormSnapshot.js +0 -11
- package/dist/vue/useFormState.d.ts +0 -3
- package/dist/vue/useFormState.js +0 -38
- package/dist/vue/useRegister.d.ts +0 -6
- package/dist/vue/useRegister.js +0 -21
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @ilokesto/form
|
|
2
2
|
|
|
3
|
-
English | [한국어](./README.ko.md)
|
|
3
|
+
**English** | [한국어](./README.ko.md)
|
|
4
4
|
|
|
5
5
|
`@ilokesto/form` is a framework-agnostic form state core. It keeps form values, field metadata, validation errors, submit attempts, and array item keys in one normalized store, while leaving rendering and event binding to framework adapters.
|
|
6
6
|
|
|
@@ -12,6 +12,12 @@ The package is designed around five ideas:
|
|
|
12
12
|
4. **Standard Schema validation**: the core depends only on the Standard Schema v1 `~standard.validate` contract, not on a specific schema library.
|
|
13
13
|
5. **Array rebasing**: when array items move, swap, insert, or disappear, child field metadata such as `errors`, `touched`, `dirty`, and `modified` is moved with the item.
|
|
14
14
|
|
|
15
|
+
## Documentation
|
|
16
|
+
|
|
17
|
+
- Full docs: [English](https://ilokesto.ayden94.com/en/form) · [한국어](https://ilokesto.ayden94.com/ko/form)
|
|
18
|
+
- Source: [packages/form](https://github.com/ilokesto/ilokesto/tree/main/packages/form)
|
|
19
|
+
- npm: [@ilokesto/form](https://www.npmjs.com/package/@ilokesto/form)
|
|
20
|
+
|
|
15
21
|
## Table of contents
|
|
16
22
|
|
|
17
23
|
- [Installation](#installation)
|
|
@@ -30,7 +36,7 @@ The package is designed around five ideas:
|
|
|
30
36
|
|
|
31
37
|
## Installation
|
|
32
38
|
|
|
33
|
-
This
|
|
39
|
+
This package is published to npm under the `@ilokesto/form` name with public access. In a workspace, add it through your package manager or workspace protocol. When the package is published or linked, the import surface is:
|
|
34
40
|
|
|
35
41
|
```ts
|
|
36
42
|
import { CreateForm } from '@ilokesto/form';
|
|
@@ -47,6 +53,8 @@ pnpm test
|
|
|
47
53
|
|
|
48
54
|
The package emits ESM JavaScript and TypeScript declarations to `dist/`.
|
|
49
55
|
|
|
56
|
+
> **ESM-only.** This package ships `"type": "module"` with ESM-only `exports`. CommonJS `require()` is not supported. Use a modern bundler (Vite, webpack 5+, esbuild, tsup) or Node.js ESM (`import`). If you need CJS compatibility, use dynamic `import()` or configure your bundler to transpile `@ilokesto/form`.
|
|
57
|
+
|
|
50
58
|
## Quick start
|
|
51
59
|
|
|
52
60
|
```ts
|
|
@@ -146,9 +154,7 @@ const { form, useRegister, handleSubmit } = useForm({
|
|
|
146
154
|
});
|
|
147
155
|
```
|
|
148
156
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
The React adapter also accepts reactive external `values`. When the `values` reference changes, the adapter calls `form.reset(values, resetOptions)` for you. This is useful for server/query data; use `keepDirtyValues` when refetches should not overwrite fields the user already edited.
|
|
157
|
+
`defaultValues` and the other `CreateForm` options create the component-owned form once. `ReactFormOptions` also accepts the current render `values` and plain `resetOptions`. The first defined value and each later value with a different `Object.is` identity call `form.reset(values, resetOptions)`. `undefined` pauses synchronization without resetting; passing the last defined object again remains a no-op. Changing only `resetOptions` is also a no-op because it applies only when a new `values` reference drives a reset.
|
|
152
158
|
|
|
153
159
|
```tsx
|
|
154
160
|
const { useRegister } = useForm({
|
|
@@ -161,6 +167,8 @@ const { useRegister } = useForm({
|
|
|
161
167
|
});
|
|
162
168
|
```
|
|
163
169
|
|
|
170
|
+
The values effect ends on component unmount. `useForm(existingForm)` keeps its existing overload behavior and does not install external-value synchronization.
|
|
171
|
+
|
|
164
172
|
The React adapter has three first-version hooks. `useRegister` is overloaded for single, array, and rest-argument registration:
|
|
165
173
|
|
|
166
174
|
| Hook | Purpose |
|
|
@@ -187,6 +195,21 @@ The event model is DOM-event centered. Custom components can use `useRegister` w
|
|
|
187
195
|
|
|
188
196
|
Vue bindings are exposed through the `./vue` subpath. They use the same `useForm(form)` shape as the React adapter, but return Vue-friendly `v-bind` props with `onInput`, `onChange`, `onBlur`, and `onFocus` handlers.
|
|
189
197
|
|
|
198
|
+
`useForm` also accepts `VueFormOptions`. Its `values` source is `MaybeRefOrGetter<TValues | undefined>` and `resetOptions` stays plain. The first defined value and each later value with a different `Object.is` identity call `form.reset(values, resetOptions)`. `undefined` pauses synchronization without resetting, and re-emitting the last defined object remains a no-op. Pass `ref`, `computed`, or a getter to track changes; a plain value is evaluated once. Reactive values require an active Vue effect scope; the adapter fails before creating a form when none exists. The watcher stops with that scope, and `useForm(existingForm)` does not install it.
|
|
199
|
+
|
|
200
|
+
```vue
|
|
201
|
+
<script setup lang="ts">
|
|
202
|
+
import { ref } from 'vue';
|
|
203
|
+
import { useForm } from '@ilokesto/form/vue';
|
|
204
|
+
|
|
205
|
+
const serverValues = ref({ email: 'initial@example.com' });
|
|
206
|
+
const { form, useRegister } = useForm({
|
|
207
|
+
defaultValues: { email: '' },
|
|
208
|
+
values: serverValues,
|
|
209
|
+
});
|
|
210
|
+
</script>
|
|
211
|
+
```
|
|
212
|
+
|
|
190
213
|
```vue
|
|
191
214
|
<script setup lang="ts">
|
|
192
215
|
import { CreateForm } from '@ilokesto/form';
|
|
@@ -244,7 +267,7 @@ The Vue adapter exposes the same three concepts:
|
|
|
244
267
|
| `useRegister(options)` | Returns one input-oriented `v-bind` binding object. It includes `type` and defaults to `text`; text inputs update on `input`; checkbox/radio update on `change`. Use a generic for select/textarea binding types. |
|
|
245
268
|
| `useRegister(options[])` / `useRegister(optionA, optionB)` | Returns multiple binding objects for map-friendly rendering. |
|
|
246
269
|
| `useField(options)` | Returns `{ props, value, setValue, errors, dirty, touched }` with getter-backed reactive reads. |
|
|
247
|
-
| `useFormState()` | Returns form-wide aggregate getters such as `errors`, `dirtyFields`, `touchedFields`, `isDirty`, `isValid`, and `submitCount`. |
|
|
270
|
+
| `useFormState()` | Returns form-wide aggregate getters such as `errors`, `dirtyFields`, `touchedFields`, `focusedField`, `isDirty`, `isValid`, and `submitCount`. |
|
|
248
271
|
|
|
249
272
|
Field-local schemas work the same way as React and are cleaned up with the current Vue effect scope.
|
|
250
273
|
|
|
@@ -292,9 +315,22 @@ function LoginForm() {
|
|
|
292
315
|
|
|
293
316
|
The Solid adapter exposes `useRegister`, `useField`, and `useFormState` with the same semantics as Vue: text inputs update on `input`, checkbox/radio/multiple select update on `change`, and field-local schemas are disposed with the current Solid owner.
|
|
294
317
|
|
|
318
|
+
For component-owned forms, `SolidFormOptions` accepts `values` as `Accessor<TValues | undefined>` and plain `resetOptions`:
|
|
319
|
+
|
|
320
|
+
```tsx
|
|
321
|
+
const [serverValues, setServerValues] = createSignal<User | undefined>();
|
|
322
|
+
const { form } = useForm({
|
|
323
|
+
defaultValues: emptyUser,
|
|
324
|
+
values: serverValues,
|
|
325
|
+
resetOptions: { keepDirtyValues: true },
|
|
326
|
+
});
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
The first defined accessor value and each later value with a different `Object.is` identity reset the form. `undefined` pauses without resetting, re-emitting the last defined object is a no-op, and `resetOptions` applies only to value-driven resets. Reactive values require an active Solid owner; the adapter fails before creating a form when none exists. Tracking is disposed with that owner. `useForm(existingForm)` does not install it.
|
|
330
|
+
|
|
295
331
|
## Svelte adapter
|
|
296
332
|
|
|
297
|
-
Svelte bindings are exposed through the `./svelte` subpath.
|
|
333
|
+
Svelte bindings are exposed through the `./svelte` subpath. The adapter exposes register actions plus readable stores for both form-wide and field-local state.
|
|
298
334
|
|
|
299
335
|
```svelte
|
|
300
336
|
<script lang="ts">
|
|
@@ -310,12 +346,17 @@ Svelte bindings are exposed through the `./svelte` subpath. Svelte does not use
|
|
|
310
346
|
validateOn: ['blur', 'submit'],
|
|
311
347
|
});
|
|
312
348
|
|
|
313
|
-
const { register, useFormState } = useForm(form);
|
|
349
|
+
const { register, useField, useFormState } = useForm(form);
|
|
350
|
+
const email = useField({ name: 'email', schema: emailSchema });
|
|
351
|
+
const emailProps = email.props;
|
|
314
352
|
const state = useFormState();
|
|
315
353
|
</script>
|
|
316
354
|
|
|
317
355
|
<form>
|
|
318
|
-
<input use:
|
|
356
|
+
<input use:emailProps />
|
|
357
|
+
{#each $email.errors as error}
|
|
358
|
+
<p>{error.message}</p>
|
|
359
|
+
{/each}
|
|
319
360
|
<input type="checkbox" use:register={{ name: 'remember', type: 'checkbox' }} />
|
|
320
361
|
|
|
321
362
|
<select use:register={{ name: 'role' }}>
|
|
@@ -327,7 +368,25 @@ Svelte bindings are exposed through the `./svelte` subpath. Svelte does not use
|
|
|
327
368
|
</form>
|
|
328
369
|
```
|
|
329
370
|
|
|
330
|
-
The Svelte action owns DOM synchronization directly
|
|
371
|
+
The Svelte action owns DOM synchronization directly. `useField` is a `Readable<SvelteFieldSnapshot>` consumed as `$email`; it emits current `value`, `errors`, `dirty`, and `touched` state and releases its core subscription when the last subscriber leaves.
|
|
372
|
+
|
|
373
|
+
For component-owned forms, `SvelteFormOptions` accepts `values` as `Readable<TValues | undefined>` and plain `resetOptions`:
|
|
374
|
+
|
|
375
|
+
```svelte
|
|
376
|
+
<script lang="ts">
|
|
377
|
+
import { writable } from 'svelte/store';
|
|
378
|
+
import { useForm } from '@ilokesto/form/svelte';
|
|
379
|
+
|
|
380
|
+
const serverValues = writable<User | undefined>(undefined);
|
|
381
|
+
const { form } = useForm({
|
|
382
|
+
defaultValues: emptyUser,
|
|
383
|
+
values: serverValues,
|
|
384
|
+
resetOptions: { keepDirtyValues: true },
|
|
385
|
+
});
|
|
386
|
+
</script>
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
The first defined emission and each later emission with a different `Object.is` identity reset the form. `undefined` pauses without resetting, re-emitting the last defined object is a no-op, and `resetOptions` applies only to value-driven resets. Call this overload during component initialization; its subscription ends on unmount. `useForm(existingForm)` does not subscribe.
|
|
331
390
|
|
|
332
391
|
## Core concepts
|
|
333
392
|
|
|
@@ -401,6 +460,7 @@ type FieldState<TValue = unknown> = {
|
|
|
401
460
|
touched: boolean;
|
|
402
461
|
dirty: boolean;
|
|
403
462
|
modified: boolean;
|
|
463
|
+
isFocused: boolean;
|
|
404
464
|
};
|
|
405
465
|
```
|
|
406
466
|
|
|
@@ -409,6 +469,7 @@ The flags mean:
|
|
|
409
469
|
- `touched`: the field was blurred at least once.
|
|
410
470
|
- `dirty`: the current value is not `Object.is`-equal to the initial value at the same path.
|
|
411
471
|
- `modified`: the field was changed by a user-sourced write, `source: 'user'`.
|
|
472
|
+
- `isFocused`: the field currently has focus. Set to `true` by `focus()` and cleared by `blur()` (always, regardless of `validateOn`).
|
|
412
473
|
- `errors`: validation or manually assigned field errors.
|
|
413
474
|
|
|
414
475
|
### `FormState`
|
|
@@ -444,9 +505,9 @@ The core stores leaf field states and array container keys separately:
|
|
|
444
505
|
items: [{ title: 'A' }, { title: 'B' }],
|
|
445
506
|
},
|
|
446
507
|
fields: {
|
|
447
|
-
'["user","name"]': { value: 'Ada', errors: [], touched: false, dirty: false, modified: false },
|
|
448
|
-
'["items",0,"title"]': { value: 'A', errors: [], touched: false, dirty: false, modified: false },
|
|
449
|
-
'["items",1,"title"]': { value: 'B', errors: [], touched: false, dirty: false, modified: false },
|
|
508
|
+
'["user","name"]': { value: 'Ada', errors: [], touched: false, dirty: false, modified: false, isFocused: false },
|
|
509
|
+
'["items",0,"title"]': { value: 'A', errors: [], touched: false, dirty: false, modified: false, isFocused: false },
|
|
510
|
+
'["items",1,"title"]': { value: 'B', errors: [], touched: false, dirty: false, modified: false, isFocused: false },
|
|
450
511
|
},
|
|
451
512
|
submitCount: 0,
|
|
452
513
|
arrayKeys: {
|
|
@@ -627,6 +688,8 @@ Effects:
|
|
|
627
688
|
|
|
628
689
|
`setValue()` returns `void`; change validation is started asynchronously and is not awaited by the method.
|
|
629
690
|
|
|
691
|
+
Async validation is target-aware. New validation supersedes only overlapping fields, while independent field validations may complete concurrently. Full-form submit validation also verifies the value snapshot it validated; stale submit attempts retry until they obtain an authoritative result and never call `onValid` by default.
|
|
692
|
+
|
|
630
693
|
### `blur(path)`
|
|
631
694
|
|
|
632
695
|
Marks the field as touched and optionally validates it.
|
|
@@ -639,7 +702,9 @@ If `validateOn` does not contain `'blur'`, it returns `true` after touching the
|
|
|
639
702
|
|
|
640
703
|
### `focus(path)`
|
|
641
704
|
|
|
642
|
-
|
|
705
|
+
Sets `isFocused: true` on the field at `path`. Other fields are not touched — DOM naturally fires a `blur` event on the previously focused element, which clears `isFocused` via `blur()`. Array rebasing preserves `isFocused` across `move`/`swap`/`insert`/`remove`.
|
|
706
|
+
|
|
707
|
+
The core is DOM-independent, so calling `focus()` on multiple fields can leave more than one field with `isFocused: true`. In DOM adapters this is naturally bounded to one by the browser; in direct core usage it is the caller's responsibility to `blur()` the previous field. The `useFormState().focusedField` aggregate returns the **first** focused field found in `Object.entries` order, not the most recently focused one — to enumerate all focused fields, iterate `state.fields` directly.
|
|
643
708
|
|
|
644
709
|
### `setErrors(path, errors)`
|
|
645
710
|
|
|
@@ -753,502 +818,102 @@ Replaces the whole array. Existing item links are intentionally broken, so child
|
|
|
753
818
|
|
|
754
819
|
## Runtime flows
|
|
755
820
|
|
|
756
|
-
###
|
|
757
|
-
|
|
758
|
-
```txt
|
|
759
|
-
new CreateForm(options)
|
|
760
|
-
-> new FormStateStore(options.defaultValues)
|
|
761
|
-
-> FormStateInitializer.initialize(defaultValues)
|
|
762
|
-
-> fields for leaf values
|
|
763
|
-
-> arrayKeys for array containers
|
|
764
|
-
-> new ValidationEngine(store, options)
|
|
765
|
-
-> new FormFieldCommands(store, validation)
|
|
766
|
-
-> new FormArrayFactory(store)
|
|
767
|
-
-> new FormSubmitter(store, validation)
|
|
768
|
-
```
|
|
769
|
-
|
|
770
|
-
The important point is that all collaborators share the same `FormStateStore`.
|
|
771
|
-
|
|
772
|
-
### Value write flow
|
|
773
|
-
|
|
774
|
-
```txt
|
|
775
|
-
form.setValue(path, value, options)
|
|
776
|
-
-> FormFieldCommands.setValue()
|
|
777
|
-
-> FormPath.toFieldPath(path)
|
|
778
|
-
-> FormStateStore.setValue()
|
|
779
|
-
-> FormStateWriter.setValue()
|
|
780
|
-
-> compute dirty from defaultValues
|
|
781
|
-
-> optionally mark modified
|
|
782
|
-
-> optionally start change validation
|
|
783
|
-
```
|
|
784
|
-
|
|
785
|
-
`dirty` and `modified` are intentionally separate. Programmatic writes can make a field dirty without marking it as user-modified.
|
|
821
|
+
### Field commands
|
|
786
822
|
|
|
787
|
-
|
|
823
|
+
Start with `src/core/form/CreateForm.ts`. It shows the complete orchestration:
|
|
788
824
|
|
|
789
825
|
```txt
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
->
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
-> otherwise return true
|
|
826
|
+
setValue -> normalize path -> store.setValue -> optional change validation
|
|
827
|
+
blur -> normalize path -> unfocus -> touch -> optional blur validation
|
|
828
|
+
trigger -> normalize paths -> selected-field or full validation
|
|
829
|
+
array -> create controller with the form's shared key generator
|
|
830
|
+
submit -> submit queue -> validation -> callback -> completion
|
|
796
831
|
```
|
|
797
832
|
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
form.trigger(...paths)
|
|
802
|
-
-> no paths: validateRegisteredFields('manual')
|
|
803
|
-
-> with paths: validateFields(keys, 'manual')
|
|
804
|
-
-> field-local schema exists: validate that field value and replace that field's errors
|
|
805
|
-
-> otherwise: StandardSchemaValidator.validate(values)
|
|
806
|
-
-> issue paths become PathKeys
|
|
807
|
-
-> selected field errors are replaced
|
|
808
|
-
```
|
|
833
|
+
Writes notify synchronously. Blur retains separate unfocus and touch notifications.
|
|
834
|
+
`dirty` compares against the default value with `Object.is`; `modified` records
|
|
835
|
+
user-originated writes. These flags have different meanings.
|
|
809
836
|
|
|
810
|
-
|
|
837
|
+
### Validation and submission
|
|
811
838
|
|
|
812
|
-
|
|
839
|
+
Selected-field validation runs local schemas first, then the form schema for
|
|
840
|
+
remaining targets. Full validation runs the form schema first, then registered
|
|
841
|
+
local schemas. A local result, including an empty error list, takes precedence.
|
|
842
|
+
Without a local schema, selected-field validation executes the whole form schema
|
|
843
|
+
but writes only the selected errors.
|
|
813
844
|
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
-> invalid: onInvalid(fields), return undefined
|
|
819
|
-
-> valid: onValid(getValues())
|
|
820
|
-
```
|
|
845
|
+
Every asynchronous phase checks both its validation revision and captured values
|
|
846
|
+
before writing errors. Independent field validations may finish in either order;
|
|
847
|
+
overlapping work must not overwrite newer results. Errors are applied through the
|
|
848
|
+
existing per-field write boundaries, not a new batch update.
|
|
821
849
|
|
|
822
|
-
`
|
|
850
|
+
`FormSubmitter` queues concurrent submissions. It increments the attempt count
|
|
851
|
+
immediately, retries stale validation, calls `onInvalid` for invalid results or
|
|
852
|
+
`onValid` with current values, and completes submit state when the queue drains.
|
|
823
853
|
|
|
824
|
-
### Array
|
|
854
|
+
### Array mutations
|
|
825
855
|
|
|
826
856
|
```txt
|
|
827
|
-
|
|
828
|
-
->
|
|
829
|
-
->
|
|
830
|
-
->
|
|
831
|
-
-> next values
|
|
832
|
-
-> next keys
|
|
833
|
-
-> previous-index to next-index mapper
|
|
834
|
-
-> FormArrayRebaser.rebase()
|
|
835
|
-
-> write next array into values
|
|
836
|
-
-> initialize a fresh FormState from next values
|
|
837
|
-
-> preserve non-array fields
|
|
838
|
-
-> move child metadata through the index mapper
|
|
839
|
-
-> write next array keys
|
|
857
|
+
controller reads values and keys
|
|
858
|
+
-> static planner computes { values, keys, mapPreviousIndex }
|
|
859
|
+
-> store.replaceState(previousState => rebase(previousState, path, mutation))
|
|
860
|
+
-> one next snapshot
|
|
840
861
|
```
|
|
841
862
|
|
|
842
|
-
|
|
863
|
+
Rebasing reconstructs values from the supplied snapshot, initializes the next
|
|
864
|
+
state, and moves surviving child metadata and nested array keys to their new
|
|
865
|
+
paths. Errors, touched, dirty, modified and focus follow the item. Unrelated
|
|
866
|
+
fields and submit state are preserved. Replace intentionally drops old child
|
|
867
|
+
metadata and creates fresh keys. The existing defaultValues behavior is retained.
|
|
843
868
|
|
|
844
869
|
## Internal architecture
|
|
845
870
|
|
|
846
871
|
```txt
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
->
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
-> array/ArrayKeyGenerator.ts
|
|
865
|
-
-> path/FormPath.ts
|
|
866
|
-
-> value/ValueHelper.ts
|
|
867
|
-
-> types.ts
|
|
868
|
-
-> src/adapters/
|
|
869
|
-
-> dom/FieldValue.ts
|
|
870
|
-
-> dom/RegisterBinding.ts
|
|
871
|
-
-> FormStateSummary.ts
|
|
872
|
-
-> src/react/index.ts
|
|
873
|
-
-> React hook adapter over shared adapter logic
|
|
874
|
-
-> src/vue/index.ts
|
|
875
|
-
-> Vue composable adapter over shared adapter logic
|
|
876
|
-
-> src/solid/index.ts
|
|
877
|
-
-> Solid helper adapter over shared adapter logic
|
|
878
|
-
-> src/svelte/index.ts
|
|
879
|
-
-> Svelte action adapter over shared adapter logic
|
|
880
|
-
```
|
|
881
|
-
|
|
882
|
-
Responsibility summary:
|
|
883
|
-
|
|
884
|
-
| Area | Responsibility |
|
|
885
|
-
| --- | --- |
|
|
886
|
-
| `form/` | Public orchestration: field commands, submit flow, array controller access. |
|
|
887
|
-
| `state/` | Normalized state initialization, reading, writing, and store facade. |
|
|
888
|
-
| `path/` | Conversion between public path input, tuple paths, and string path keys. |
|
|
889
|
-
| `value/` | Immutable nested get/set and reconstruction of values from field states. |
|
|
890
|
-
| `validation/` | Standard Schema execution and error normalization. |
|
|
891
|
-
| `array/` | Array item key management, mutation planning, and child field metadata rebasing. |
|
|
892
|
-
| `adapters/` | Internal shared adapter logic: DOM value extraction/binding and form-state aggregation. |
|
|
893
|
-
| `react/` | React hook adapter around the public `Form` interface. |
|
|
894
|
-
| `vue/` | Vue composable adapter around the public `Form` interface. |
|
|
895
|
-
| `solid/` | Solid helper adapter around the public `Form` interface. |
|
|
896
|
-
| `svelte/` | Svelte action adapter around the public `Form` interface. |
|
|
897
|
-
| `types.ts` | Public and internal TypeScript contracts. |
|
|
872
|
+
CreateForm public command order and shared array keys
|
|
873
|
+
FormStateStore state ownership and writes
|
|
874
|
+
FormStateInitializer nested defaults -> normalized snapshot
|
|
875
|
+
FormStateReader snapshot -> fields and nested values
|
|
876
|
+
ValidationEngine schema registration and validation phases
|
|
877
|
+
StandardSchemaValidator schema results -> field errors
|
|
878
|
+
ValidationSequencer overlapping validation revisions
|
|
879
|
+
ValidationSnapshot captured values and array-key comparison
|
|
880
|
+
FormSubmitter submit queue and callback lifecycle
|
|
881
|
+
FormArrayController array reads and state application
|
|
882
|
+
FormArrayMutationPlanner pure array mutation calculation
|
|
883
|
+
FormArrayRebaser previous snapshot + mutation -> next snapshot
|
|
884
|
+
```
|
|
885
|
+
|
|
886
|
+
React, Vue, Solid and Svelte adapters depend on the public `Form` contract.
|
|
887
|
+
`adapters/` shares DOM value/binding and aggregate-state logic; subscriptions and
|
|
888
|
+
component lifecycle ownership remain in each framework directory.
|
|
898
889
|
|
|
899
890
|
## Core walkthrough
|
|
900
891
|
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
`types.ts`
|
|
925
|
-
|
|
926
|
-
- public path type: `FieldPathInput`; internal path concepts: `FieldPathSegment`, `FieldPath`, `PathKey`
|
|
927
|
-
- exported validation types: `FormError`, `StandardSchemaV1`; internal validation trigger type: `ValidationTrigger`
|
|
928
|
-
- internal state types: `FieldState`, `ArrayKeys`, `FormState`
|
|
929
|
-
- public API types: `CreateFormOptions`, `Form`; internal command helper types: `FormArray`, `SetValueOptions`
|
|
930
|
-
|
|
931
|
-
The most important design in this file is that public string paths are literal field names. Nested fields require tuple paths.
|
|
932
|
-
|
|
933
|
-
### `src/core/form/CreateForm.ts`
|
|
934
|
-
|
|
935
|
-
`CreateForm` is the top-level controller and the main public class.
|
|
936
|
-
|
|
937
|
-
It owns four collaborators:
|
|
938
|
-
|
|
939
|
-
```txt
|
|
940
|
-
store -> state storage and state operations
|
|
941
|
-
fields -> setValue, blur, errors, trigger
|
|
942
|
-
arrays -> array(path) controller creation
|
|
943
|
-
submitter -> submitCount, validation, callbacks
|
|
944
|
-
```
|
|
945
|
-
|
|
946
|
-
The constructor creates one `FormStateStore`, then passes it to all collaborators. That makes every command operate on the same snapshot.
|
|
947
|
-
|
|
948
|
-
Most methods are thin delegations:
|
|
949
|
-
|
|
950
|
-
- `getState()`, `getFieldState()`, `getValue()`, `getValues()`, `reset()` delegate to `FormStateStore`.
|
|
951
|
-
- `setValue()`, `blur()`, `focus()`, `setErrors()`, `clearErrors()`, `trigger()` delegate to `FormFieldCommands`.
|
|
952
|
-
- `array()` converts the path to a tuple and asks `FormArrayFactory` for a controller.
|
|
953
|
-
- `submit()` delegates to `FormSubmitter`.
|
|
954
|
-
|
|
955
|
-
This keeps the public API stable while implementation responsibilities stay separated.
|
|
956
|
-
|
|
957
|
-
### `src/core/form/FormFieldCommands.ts`
|
|
958
|
-
|
|
959
|
-
`FormFieldCommands` groups field-level operations.
|
|
960
|
-
|
|
961
|
-
`setValue()`:
|
|
962
|
-
|
|
963
|
-
1. Converts the path to a tuple through `FormPath.toFieldPath()`.
|
|
964
|
-
2. Writes the value through `store.setValue()`.
|
|
965
|
-
3. Receives the resulting `PathKey`.
|
|
966
|
-
4. Starts change validation if `options.validate` is true or `validateOn` contains `'change'`.
|
|
967
|
-
|
|
968
|
-
`blur()`:
|
|
969
|
-
|
|
970
|
-
1. Converts the public path to a `PathKey`.
|
|
971
|
-
2. Marks the field as touched.
|
|
972
|
-
3. Runs blur validation only when configured.
|
|
973
|
-
|
|
974
|
-
`focus()` is currently a no-op because the minimal core state does not track focus. The method exists for adapter-level consistency.
|
|
975
|
-
|
|
976
|
-
`setErrors()`, `clearErrors()`, and `trigger()` normalize paths to keys and delegate to the store or validation engine.
|
|
977
|
-
|
|
978
|
-
### `src/core/form/FormSubmitter.ts`
|
|
979
|
-
|
|
980
|
-
`FormSubmitter` isolates submit-specific sequencing:
|
|
981
|
-
|
|
982
|
-
```txt
|
|
983
|
-
increment submitCount
|
|
984
|
-
validate all registered fields
|
|
985
|
-
if invalid -> call onInvalid(fields) and return undefined
|
|
986
|
-
if valid -> call onValid(getValues())
|
|
987
|
-
```
|
|
988
|
-
|
|
989
|
-
The class exists because submit is a multi-step command and should not make `CreateForm` responsible for validation branching.
|
|
990
|
-
|
|
991
|
-
### `src/core/state/FieldStateFactory.ts`
|
|
992
|
-
|
|
993
|
-
`FieldStateFactory` centralizes the default field state shape:
|
|
994
|
-
|
|
995
|
-
```ts
|
|
996
|
-
{
|
|
997
|
-
value: undefined,
|
|
998
|
-
errors: [],
|
|
999
|
-
touched: false,
|
|
1000
|
-
dirty: false,
|
|
1001
|
-
modified: false,
|
|
1002
|
-
}
|
|
1003
|
-
```
|
|
1004
|
-
|
|
1005
|
-
It creates a new `errors` array each time to avoid shared mutable references. The same factory is used for initial leaf fields and missing-field fallbacks.
|
|
1006
|
-
|
|
1007
|
-
### `src/core/state/FormStateInitializer.ts`
|
|
1008
|
-
|
|
1009
|
-
`FormStateInitializer.initialize(defaultValues)` converts nested values into normalized `FormState`.
|
|
1010
|
-
|
|
1011
|
-
Rules:
|
|
1012
|
-
|
|
1013
|
-
1. Arrays are containers, not leaf fields.
|
|
1014
|
-
2. Array containers receive `arrayKeys` such as `initial-0`, `initial-1`.
|
|
1015
|
-
3. Array items are visited recursively.
|
|
1016
|
-
4. Plain objects are traversed.
|
|
1017
|
-
5. Empty plain objects are stored as leaf values because there is no deeper field.
|
|
1018
|
-
6. Non-plain objects such as `Date` and class instances are treated as leaf values.
|
|
1019
|
-
7. Primitives and `null` are leaf values.
|
|
1020
|
-
|
|
1021
|
-
This is why `getState().fields` contains leaf paths rather than the original nested object.
|
|
1022
|
-
|
|
1023
|
-
### `src/core/state/FormStateReader.ts`
|
|
1024
|
-
|
|
1025
|
-
`FormStateReader` handles read-only derived operations.
|
|
1026
|
-
|
|
1027
|
-
- `getKnownFieldPaths()` converts every stored `PathKey` back to a tuple path.
|
|
1028
|
-
- `getFieldStateByKey()` returns an existing field or a default `FieldState`.
|
|
1029
|
-
- `getFieldState()` and `getValue()` are path-input convenience methods.
|
|
1030
|
-
- `getValues()` reconstructs the full nested value tree with `ValueHelper.getValuesFromFields()`.
|
|
1031
|
-
- `getValueAtPath()` reconstructs values first, then reads a nested path from them.
|
|
1032
|
-
|
|
1033
|
-
The reader receives a snapshot getter rather than owning the store directly, so it always reads the latest state.
|
|
1034
|
-
|
|
1035
|
-
### `src/core/state/FormStateWriter.ts`
|
|
1036
|
-
|
|
1037
|
-
`FormStateWriter` performs all state mutations through `immer`.
|
|
1038
|
-
|
|
1039
|
-
`setValue()`:
|
|
1040
|
-
|
|
1041
|
-
- Converts the tuple path to a `PathKey`.
|
|
1042
|
-
- Reads the initial value at that tuple path.
|
|
1043
|
-
- Preserves existing field metadata when possible.
|
|
1044
|
-
- Replaces `value`.
|
|
1045
|
-
- Computes `dirty` with `Object.is`.
|
|
1046
|
-
- Sets `modified` only for `source: 'user'`.
|
|
1047
|
-
- Returns the `PathKey` so validation can reuse it.
|
|
1048
|
-
|
|
1049
|
-
Other methods:
|
|
1050
|
-
|
|
1051
|
-
- `touchField()` sets `touched: true`.
|
|
1052
|
-
- `setErrorsByKey()` replaces the error array.
|
|
1053
|
-
- `clearErrors()` clears target fields or all existing fields.
|
|
1054
|
-
- `reset()` reinitializes state from the current or replacement initial values.
|
|
1055
|
-
- `incrementSubmitCount()` increments attempts.
|
|
1056
|
-
- `replaceState()` is an escape hatch for whole-state operations such as array rebasing.
|
|
1057
|
-
|
|
1058
|
-
### `src/core/state/FormStateStore.ts`
|
|
1059
|
-
|
|
1060
|
-
`FormStateStore` is the facade used by the rest of the core.
|
|
1061
|
-
|
|
1062
|
-
It owns:
|
|
1063
|
-
|
|
1064
|
-
- `Store<FormState<TValues>>` from `@ilokesto/store`
|
|
1065
|
-
- `FormStateReader`
|
|
1066
|
-
- `FormStateWriter`
|
|
1067
|
-
|
|
1068
|
-
Collaborators do not need to know whether an operation is implemented by the reader, writer, or underlying store. They call the facade.
|
|
1069
|
-
|
|
1070
|
-
### `src/core/path/FormPath.ts`
|
|
1071
|
-
|
|
1072
|
-
`FormPath` defines path conversion rules.
|
|
1073
|
-
|
|
1074
|
-
Key methods:
|
|
1075
|
-
|
|
1076
|
-
- `path(...segments)`: helper for tuple paths.
|
|
1077
|
-
- `toFieldPath(input)`: string becomes `[input]`; tuple path passes through.
|
|
1078
|
-
- `pathInputToKey(input)`: public path input to `PathKey`.
|
|
1079
|
-
- `pathToKey(path)`: `[]` becomes `$`, otherwise JSON stringifies the tuple.
|
|
1080
|
-
- `keyToPath(key)`: parses and validates a key back into tuple segments.
|
|
1081
|
-
|
|
1082
|
-
The JSON encoding is what prevents path collisions.
|
|
1083
|
-
|
|
1084
|
-
### `src/core/value/ValueHelper.ts`
|
|
1085
|
-
|
|
1086
|
-
`ValueHelper` provides immutable nested value operations.
|
|
1087
|
-
|
|
1088
|
-
`getValueAtPath(source, path)` walks an object/array safely. If an intermediate value is nullish or primitive, it returns `undefined`.
|
|
1089
|
-
|
|
1090
|
-
`setValueAtPath(source, path, value)` returns a new root value and shallow-clones containers along the path. Missing containers are inferred from the next segment: a number creates an array, a string creates an object.
|
|
1091
|
-
|
|
1092
|
-
`getValuesFromFields(state, fieldPaths)` reconstructs public values in two phases:
|
|
1093
|
-
|
|
1094
|
-
1. Create empty array containers from `arrayKeys`.
|
|
1095
|
-
2. Write every leaf field value into its tuple path.
|
|
1096
|
-
|
|
1097
|
-
This lets normalized `fields` become a nested object again.
|
|
1098
|
-
|
|
1099
|
-
### `src/core/validation/ValidationEngine.ts`
|
|
1100
|
-
|
|
1101
|
-
`ValidationEngine` coordinates validation triggers and store writes.
|
|
1102
|
-
|
|
1103
|
-
- Default `validateOn` is `['submit']`.
|
|
1104
|
-
- If no schema is provided, validation succeeds with no errors.
|
|
1105
|
-
- `validateField(key, trigger)` runs the full schema and applies only that field's errors.
|
|
1106
|
-
- `validateFields(keys, trigger)` runs the full schema and applies only selected fields' errors.
|
|
1107
|
-
- `validateRegisteredFields(trigger)` runs the full schema and updates all current fields plus all schema error keys.
|
|
1108
|
-
|
|
1109
|
-
The engine deliberately ignores the trigger value when calling the schema. The trigger controls when the engine runs, not the schema API.
|
|
1110
|
-
|
|
1111
|
-
### `src/core/validation/StandardSchemaValidator.ts`
|
|
1112
|
-
|
|
1113
|
-
`StandardSchemaValidator` adapts Standard Schema results to core errors.
|
|
1114
|
-
|
|
1115
|
-
Success:
|
|
1116
|
-
|
|
1117
|
-
```ts
|
|
1118
|
-
{ valid: true, errorsByKey: {} }
|
|
1119
|
-
```
|
|
1120
|
-
|
|
1121
|
-
Failure:
|
|
1122
|
-
|
|
1123
|
-
```ts
|
|
1124
|
-
{
|
|
1125
|
-
valid: false,
|
|
1126
|
-
errorsByKey: {
|
|
1127
|
-
'["email"]': [{ type: 'standard_schema', message: 'Email is invalid' }],
|
|
1128
|
-
},
|
|
1129
|
-
}
|
|
1130
|
-
```
|
|
1131
|
-
|
|
1132
|
-
Issue path conversion rules:
|
|
1133
|
-
|
|
1134
|
-
- Missing or empty path becomes the root path `[]` and key `$`.
|
|
1135
|
-
- String and number path segments are preserved.
|
|
1136
|
-
- Object path segments use their `.key` property.
|
|
1137
|
-
- Unsupported keys such as symbols fall back to root errors.
|
|
1138
|
-
|
|
1139
|
-
### `src/core/array/ArrayKeyGenerator.ts`
|
|
1140
|
-
|
|
1141
|
-
`ArrayKeyGenerator` creates runtime item keys:
|
|
1142
|
-
|
|
1143
|
-
```txt
|
|
1144
|
-
item-1
|
|
1145
|
-
item-2
|
|
1146
|
-
item-3
|
|
1147
|
-
```
|
|
1148
|
-
|
|
1149
|
-
A single `FormArrayFactory` shares one generator per form instance, reducing accidental key collisions across controllers.
|
|
1150
|
-
|
|
1151
|
-
### `src/core/array/ArrayItemReorder.ts`
|
|
1152
|
-
|
|
1153
|
-
`ArrayItemReorder` is a pure helper for array order operations.
|
|
1154
|
-
|
|
1155
|
-
- `moveItem(items, from, to)` returns a reordered copy.
|
|
1156
|
-
- `swapItems(items, left, right)` returns a swapped copy.
|
|
1157
|
-
- `createIndexMapper(previousLength, nextOrder)` returns a function that maps a previous index to its next index.
|
|
1158
|
-
|
|
1159
|
-
`nextOrder` is expressed as "for each next position, which previous index is here?" New items use `-1`, and removed items simply do not appear.
|
|
1160
|
-
|
|
1161
|
-
### `src/core/array/FormArrayMutationPlanner.ts`
|
|
1162
|
-
|
|
1163
|
-
`FormArrayMutationPlanner` calculates array mutations without knowing about the store.
|
|
1164
|
-
|
|
1165
|
-
Each mutation returns:
|
|
1166
|
-
|
|
1167
|
-
```ts
|
|
1168
|
-
type FormArrayMutation = {
|
|
1169
|
-
values: readonly unknown[];
|
|
1170
|
-
keys: readonly string[];
|
|
1171
|
-
mapPreviousIndex: (index: number) => number | undefined;
|
|
1172
|
-
};
|
|
1173
|
-
```
|
|
1174
|
-
|
|
1175
|
-
Command behavior:
|
|
1176
|
-
|
|
1177
|
-
- `insert()` bounds the target index and inserts a new value/key. The new item maps from `-1`, so no previous metadata is attached to it.
|
|
1178
|
-
- `push()` appends a value/key.
|
|
1179
|
-
- `remove()` drops a value/key and maps removed indexes to `undefined`.
|
|
1180
|
-
- `move()` moves values and keys with the same rule.
|
|
1181
|
-
- `swap()` swaps values and keys with the same rule.
|
|
1182
|
-
- `replace()` returns all new values/keys and maps every previous index to `undefined`.
|
|
1183
|
-
|
|
1184
|
-
Because this planner is pure, it is easy to test independently from the store.
|
|
1185
|
-
|
|
1186
|
-
### `src/core/array/FormArrayPath.ts`
|
|
1187
|
-
|
|
1188
|
-
`FormArrayPath` contains path helpers used during rebasing.
|
|
1189
|
-
|
|
1190
|
-
- `hasPathPrefix(fieldPath, prefix)` checks tuple prefix equality.
|
|
1191
|
-
- `isArrayChildPath(fieldPath, arrayPath)` checks whether a field is below an array item, such as `['items', 0, 'name']` below `['items']`.
|
|
1192
|
-
- `replaceArrayIndex(fieldPath, arrayPath, nextIndex)` creates a new child path with the item index replaced.
|
|
1193
|
-
|
|
1194
|
-
### `src/core/array/FormArrayController.ts`
|
|
1195
|
-
|
|
1196
|
-
`FormArrayController` is the public command object returned by `form.array(path)`.
|
|
1197
|
-
|
|
1198
|
-
It holds:
|
|
1199
|
-
|
|
1200
|
-
- the shared `FormStateStore`
|
|
1201
|
-
- the shared `ArrayKeyGenerator`
|
|
1202
|
-
- the controlled array `FieldPath`
|
|
1203
|
-
- a `FormArrayMutationPlanner`
|
|
1204
|
-
|
|
1205
|
-
Each command follows the same pattern:
|
|
1206
|
-
|
|
1207
|
-
```txt
|
|
1208
|
-
read current array value
|
|
1209
|
-
read current keys
|
|
1210
|
-
ask planner for next values, keys, and index mapper
|
|
1211
|
-
if mutation exists, rebase the whole FormState
|
|
1212
|
-
```
|
|
1213
|
-
|
|
1214
|
-
`getArray()` reads the current value at the array path. If the value is not an array, it uses an empty array.
|
|
1215
|
-
|
|
1216
|
-
`getKeys()` reads stored `arrayKeys`; if none exist, it creates keys for the current array length.
|
|
1217
|
-
|
|
1218
|
-
### `src/core/array/FormArrayFactory.ts`
|
|
1219
|
-
|
|
1220
|
-
`FormArrayFactory` creates `FormArrayController` instances and shares one `ArrayKeyGenerator` for the form instance.
|
|
1221
|
-
|
|
1222
|
-
This allows repeated calls such as `form.array('items')` to create fresh controllers without resetting the key sequence.
|
|
1223
|
-
|
|
1224
|
-
### `src/core/array/FormArrayRebaser.ts`
|
|
1225
|
-
|
|
1226
|
-
`FormArrayRebaser` is the most important array component. It aligns values, field states, array keys, and submit count after an array mutation.
|
|
1227
|
-
|
|
1228
|
-
Detailed sequence:
|
|
1229
|
-
|
|
1230
|
-
1. Read known field paths from the current store.
|
|
1231
|
-
2. Reconstruct current values with `store.getValues()`.
|
|
1232
|
-
3. Write the next array into those values with `ValueHelper.setValueAtPath()`.
|
|
1233
|
-
4. Initialize a fresh `FormState` from the next values.
|
|
1234
|
-
5. Start with the freshly initialized fields.
|
|
1235
|
-
6. Iterate over previous fields.
|
|
1236
|
-
7. Preserve fields outside the changed array.
|
|
1237
|
-
8. For child fields inside the changed array, map the previous item index to the next item index.
|
|
1238
|
-
9. If the item still exists, replace the index in the child path and copy metadata to the new field.
|
|
1239
|
-
10. Write the next `arrayKeys` for the array path.
|
|
1240
|
-
11. Preserve `submitCount`.
|
|
1241
|
-
|
|
1242
|
-
Only metadata is copied for rebased child fields:
|
|
1243
|
-
|
|
1244
|
-
```txt
|
|
1245
|
-
errors
|
|
1246
|
-
touched
|
|
1247
|
-
dirty
|
|
1248
|
-
modified
|
|
1249
|
-
```
|
|
1250
|
-
|
|
1251
|
-
The value itself comes from the freshly initialized state based on the new values. This prevents stale values while preserving user interaction metadata.
|
|
892
|
+
Read in this order rather than following every import:
|
|
893
|
+
|
|
894
|
+
| Question | File under `src/core/` | Responsibility |
|
|
895
|
+
| --- | --- | --- |
|
|
896
|
+
| What happens after a public command? | `form/CreateForm.ts` | Normalize paths, write state, request validation. |
|
|
897
|
+
| Where does state change? | `state/FormStateStore.ts` | Own the store and apply writes with immer, including reset and submit flags. |
|
|
898
|
+
| How are values reconstructed? | `state/FormStateReader.ts` | Static calculations over an explicit snapshot; no store or getter ownership. |
|
|
899
|
+
| How is initial state built? | `state/FormStateInitializer.ts` | Traverse arrays/plain objects; keep empty objects, non-plain objects and primitive values as leaves. |
|
|
900
|
+
| What is an absent field? | `state/FieldStateFactory.ts` | Fresh metadata with undefined value, empty errors and false flags, including isFocused. |
|
|
901
|
+
| How do paths work? | `path/FormPath.ts` | Literal strings become one segment; tuple paths encode as JSON keys, with root key `$`. |
|
|
902
|
+
| How are nested values changed? | `value/ValueHelper.ts` | Immutable nested get/set and reconstruction from fields and array containers. |
|
|
903
|
+
| When may validation apply? | `validation/ValidationEngine.ts` | Local-schema precedence, selected/full execution and stale-result checks. |
|
|
904
|
+
| Which validation supersedes which? | `validation/ValidationSequencer.ts` | Full and per-field revisions, including schema registration invalidation. |
|
|
905
|
+
| Did values change during validation? | `validation/ValidationSnapshot.ts` | Capture and compare field values and array-key identities. |
|
|
906
|
+
| How are schema issues converted? | `validation/StandardSchemaValidator.ts` | Normalize Standard Schema issues; missing/unsupported paths become root errors. |
|
|
907
|
+
| How are submissions serialized? | `form/FormSubmitter.ts` | Queue submissions, retry stale validation and settle lifecycle state. |
|
|
908
|
+
| What is the next array order? | `array/FormArrayMutationPlanner.ts` | Static insert/push/remove/move/swap/replace calculations; no instance or store. |
|
|
909
|
+
| How does metadata follow items? | `array/FormArrayRebaser.ts` | Pure snapshot transformation with no store access or notifications. |
|
|
910
|
+
| Who applies an array mutation? | `array/FormArrayController.ts` | Read current array state and pass the updater snapshot to the rebaser. |
|
|
911
|
+
| Where do array keys come from? | `array/ArrayKeyGenerator.ts` | One generator per CreateForm, shared across newly created controllers. |
|
|
912
|
+
| How are indexes and paths mapped? | `array/ArrayItemReorder.ts`, `array/FormArrayPath.ts` | Pure ordering/index mapping and child-path replacement. |
|
|
913
|
+
|
|
914
|
+
`src/index.ts` and the framework entry points define the published surface.
|
|
915
|
+
`core/types.ts` describes the existing path, state, schema and command contracts.
|
|
916
|
+
Internal modules are implementation details, not additional package exports.
|
|
1252
917
|
|
|
1253
918
|
## Design decisions
|
|
1254
919
|
|
|
@@ -1287,7 +952,7 @@ pnpm typecheck
|
|
|
1287
952
|
pnpm test
|
|
1288
953
|
```
|
|
1289
954
|
|
|
1290
|
-
`pnpm build`
|
|
955
|
+
`pnpm build` uses tsup to bundle ESM JavaScript and declaration files for the core and four framework entry points. `pnpm test:pack` checks the packed package's runtime imports and TypeScript contracts in a separate consumer.
|
|
1291
956
|
|
|
1292
957
|
`pnpm test` runs the Vitest suite.
|
|
1293
958
|
|