formstand 0.12.0 → 0.14.0
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/CHANGELOG.md +279 -11
- package/README.md +86 -382
- package/dist/chunk-N7WPXYCU.js +144 -0
- package/dist/chunk-N7WPXYCU.js.map +1 -0
- package/dist/createForm-C9hZffCQ.d.cts +190 -0
- package/dist/createForm-C9hZffCQ.d.ts +190 -0
- package/dist/devtools/index.cjs +313 -0
- package/dist/devtools/index.cjs.map +1 -0
- package/dist/devtools/index.d.cts +27 -0
- package/dist/devtools/index.d.ts +27 -0
- package/dist/devtools/index.js +276 -0
- package/dist/devtools/index.js.map +1 -0
- package/dist/index.cjs +18 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +31 -189
- package/dist/index.d.ts +31 -189
- package/dist/index.js +12 -133
- package/dist/index.js.map +1 -1
- package/package.json +14 -4
package/README.md
CHANGED
|
@@ -4,13 +4,14 @@
|
|
|
4
4
|
|
|
5
5
|
Zod-schema-first form state for React 19, backed by zustand.
|
|
6
6
|
|
|
7
|
-
**[Documentation](https://scrumrot.github.io/formstand/)** · **[Live playground](https://scrumrot.github.io/formstand/examples/)** · **[API reference](https://scrumrot.github.io/formstand/api/)**
|
|
7
|
+
**[Documentation](https://scrumrot.github.io/formstand/documentation/)** · **[Live playground](https://scrumrot.github.io/formstand/examples/)** · **[CLI](https://scrumrot.github.io/formstand/documentation/cli/)** · **[API reference](https://scrumrot.github.io/formstand/documentation/api/)**
|
|
8
8
|
|
|
9
|
-
- **Typed paths
|
|
10
|
-
- **Per-field subscriptions
|
|
11
|
-
- **
|
|
12
|
-
- **Field arrays** with stable IDs that survive reorders.
|
|
13
|
-
- **Bound input components**
|
|
9
|
+
- **Typed paths**: `useField(form, "users.0.email")` infers the value type from the schema. Writes are typed too, so a typo'd path is a compile error.
|
|
10
|
+
- **Per-field subscriptions**: fields re-render only when their own slice changes, and validation parses one field's subschema per keystroke instead of the whole form.
|
|
11
|
+
- **Two error channels**: validation owns one, your app owns the other, so a background pass cannot wipe a server message.
|
|
12
|
+
- **Field arrays** with stable IDs that survive reorders, inserts, and removes.
|
|
13
|
+
- **Bound input components** with the accessibility wiring already done.
|
|
14
|
+
- **A generator**: [`formstand-cli`](https://scrumrot.github.io/formstand/documentation/cli/) writes the whole component from your schema, for six UI kits.
|
|
14
15
|
|
|
15
16
|
## Install
|
|
16
17
|
|
|
@@ -24,12 +25,7 @@ Peer-dep ranges: `zod ^4.2`, `zustand ^5.0`, `react ^19.0`.
|
|
|
24
25
|
|
|
25
26
|
```tsx
|
|
26
27
|
import { z } from "zod";
|
|
27
|
-
import {
|
|
28
|
-
TextField,
|
|
29
|
-
NumberField,
|
|
30
|
-
useForm,
|
|
31
|
-
useIsSubmitting,
|
|
32
|
-
} from "formstand";
|
|
28
|
+
import { TextField, NumberField, useForm, useIsSubmitting } from "formstand";
|
|
33
29
|
|
|
34
30
|
const schema = z.object({
|
|
35
31
|
name: z.string().min(2, "min 2 chars"),
|
|
@@ -44,11 +40,7 @@ const SignUpForm = () => {
|
|
|
44
40
|
const submitting = useIsSubmitting(form);
|
|
45
41
|
|
|
46
42
|
return (
|
|
47
|
-
<form
|
|
48
|
-
onSubmit={form.handleSubmit((data) => {
|
|
49
|
-
console.log("submit", data);
|
|
50
|
-
})}
|
|
51
|
-
>
|
|
43
|
+
<form onSubmit={form.handleSubmit((data) => console.log("submit", data))}>
|
|
52
44
|
<TextField form={form} path="name" label="Name" />
|
|
53
45
|
<NumberField form={form} path="age" label="Age" />
|
|
54
46
|
<button type="submit" disabled={submitting}>
|
|
@@ -59,382 +51,94 @@ const SignUpForm = () => {
|
|
|
59
51
|
};
|
|
60
52
|
```
|
|
61
53
|
|
|
62
|
-
|
|
54
|
+
`handleSubmit` calls `preventDefault` for you and only runs your handler with parsed,
|
|
55
|
+
`z.output`-typed data. The bound components render the label, wire the accessibility
|
|
56
|
+
attributes, and show the field's error.
|
|
63
57
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
Creates a form instance. React users typically use `useForm` instead, which wraps this.
|
|
67
|
-
|
|
68
|
-
```ts
|
|
69
|
-
const form = createForm(schema, {
|
|
70
|
-
initialValues,
|
|
71
|
-
mode: "onBlur", // "onChange" | "onBlur" | "onSubmit"
|
|
72
|
-
reValidateMode: "onChange", // mode used after the first submit attempt
|
|
73
|
-
validateOnMount: false, // run a validation pass at creation (see below)
|
|
74
|
-
});
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
### `Form<TSchema>` methods
|
|
78
|
-
|
|
79
|
-
| Method | Notes |
|
|
80
|
-
|---|---|
|
|
81
|
-
| `getState()` / `subscribe(listener)` | the underlying zustand store |
|
|
82
|
-
| `setValue(path, value)` | updates one field. Dirtiness is derived, not stored: a field reads as dirty while its value differs structurally from `initialValues` at that path (arrays/plain objects compare deep, Dates by timestamp, `Object.is` otherwise) |
|
|
83
|
-
| `setValues(next)` | replace the entire values object; server errors release only where a value slice actually changed |
|
|
84
|
-
| `setTouched(path, touched?)` | marks a path touched |
|
|
85
|
-
| `setError(path, errors)` / `setErrors(map)` / `clearErrors(path?)` | the app-owned **server error channel** (`state.serverErrors`) — validation never touches it; `setError` accepts a single string or an array; `clearErrors(path)` clears both channels at the path and its descendants (`clearErrors("")` clears just the root entry; `clearErrors()` clears everything); `setErrors` replaces the whole server channel |
|
|
86
|
-
| `setMode` / `setReValidateMode` | switch modes at runtime |
|
|
87
|
-
| `reset(nextInitial?, options?)` | reset to initial; optional partial overrides (shallow-merged for record roots, replaced wholesale otherwise) and `{ keepErrors, keepTouched, keepSubmitCount }` (no `keepDirty` — dirtiness derives from values vs initial, and reset makes them equal) |
|
|
88
|
-
| `resetField(path)` | reset one field to its initial value, clearing its (and descendants') error/touched state (dirtiness clears by definition — the value now equals initial); a path with no initial counterpart (an appended array row) keeps its value and only clears field state |
|
|
89
|
-
| `adoptValues(values)` | mid-session rebase: replaces `values` + `initialValues` and clears `errors` and in-flight validation flags (`isValidating`/`isValidatingForm` — the rebase disowns in-flight passes), but **preserves** interaction state (`touched`, `submitCount`, `isSubmitting`, `mode`). Use `reset()` for a full wipe |
|
|
90
|
-
| `updateState(updater)` | atomic multi-field patch; `errors` is derived from `schemaErrors`/`serverErrors`, so patch the channels — the patch type omits `errors` entirely, and a plain-JS `errors` patch is warned about and ignored |
|
|
91
|
-
| `validate()` / `validateField(path)` / `validateFields(paths)` | sync validation; on an async schema all three transparently start the async pass instead and return `{ kind: "pending", promise }` |
|
|
92
|
-
| `validateAsync()` / `validateFieldAsync(path)` / `validateFieldsAsync(paths)` | async; supports `async .refine` |
|
|
93
|
-
| `submit(onValid, onInvalid?, { force? })` → `Promise<SubmitResult>` | full submit flow; resolves `{ kind: "valid", data }`, `{ kind: "invalid", errors }` (errored fields are also marked touched), `{ kind: "skipped" }` when another submit is in flight, or `{ kind: "error", error }` when `onValid` throws/rejects — submit resolves instead of rejecting, so `handleSubmit` never produces an unhandled rejection from an `onValid` throw |
|
|
94
|
-
| `handleSubmit(onValid, onInvalid?)(event?)` | event handler wrapper that calls `preventDefault` |
|
|
95
|
-
| `getField(path)` | typed one-shot value read |
|
|
96
|
-
| `getFieldState(path)` | typed one-shot read of a field's full slice (value/error/touched/dirty/isValidating) |
|
|
97
|
-
| `watchField` / `watchValue` / `watchValues` | subscriptions; see below |
|
|
98
|
-
| `diff()` / `dirtyFields()` | PATCH-style helpers, derived by comparing `values` against `initialValues`: minimal divergent paths (objects recurse to the changed leaves; arrays report their base path). Reverting a field drops it |
|
|
99
|
-
| `snapshot()` / `restore(snap)` | full state capture/restore for undo/rollback |
|
|
100
|
-
| `arrayPush` / `arrayRemove` / `arrayInsert` / `arrayMove` / `arraySwap` | array ops with meta-key re-keying (errors/touched/server verdicts follow their rows); dirtiness is derived, so push + remove reads clean again |
|
|
101
|
-
|
|
102
|
-
### Validation modes
|
|
103
|
-
|
|
104
|
-
- `mode: "onBlur"` (default): validate on blur and on submit.
|
|
105
|
-
- `mode: "onChange"`: validate on every change and on submit (not on blur).
|
|
106
|
-
- `mode: "onSubmit"`: validate only on submit.
|
|
107
|
-
- `mode: "onTouched"`: validate on blur always; on change only once the field has been touched.
|
|
108
|
-
- `mode: "all"`: validate on every change and blur.
|
|
109
|
-
- `reValidateMode` (default `"onChange"`) kicks in once `submitCount > 0`.
|
|
110
|
-
|
|
111
|
-
For schemas with `async .refine`, sync `validate()` / `validateField()` / `validateFields()` don't throw: they detect the async requirement, start the async pass themselves, and hand you the in-flight work as `{ kind: "pending", promise }` (never a bare truthy `Promise` — a boolean gate can't silently pass). The requirement is also remembered per scope, so later sync calls skip straight to the async pass instead of re-paying a doomed sync parse per keystroke. If the *field being validated* is itself sync, `validateField` still settles synchronously even when other fields carry async refines — field validation parses just that field's subschema when it can (see below).
|
|
112
|
-
|
|
113
|
-
## React hooks
|
|
114
|
-
|
|
115
|
-
### `useForm(schema, options)`
|
|
116
|
-
|
|
117
|
-
Lazy-creates a `Form<TSchema>` and holds it for the component's lifetime. Stable reference across renders.
|
|
118
|
-
|
|
119
|
-
### `useField(form, path, options?)`
|
|
120
|
-
|
|
121
|
-
Subscribes to one field's slice (value/error/touched/dirty/isValidating) and returns helpers:
|
|
122
|
-
|
|
123
|
-
```ts
|
|
124
|
-
const name = useField(form, "name");
|
|
125
|
-
name.value; // typed via FieldPath
|
|
126
|
-
name.error; // readonly string[] | undefined
|
|
127
|
-
name.touched;
|
|
128
|
-
name.dirty;
|
|
129
|
-
name.isValidating;
|
|
130
|
-
name.setValue(v);
|
|
131
|
-
name.setTouched();
|
|
132
|
-
name.setError("taken"); // or ["taken", "reserved"]
|
|
133
|
-
name.clearError();
|
|
134
|
-
name.validate();
|
|
135
|
-
name.validateAsync();
|
|
136
|
-
name.onBlur();
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
Options:
|
|
140
|
-
|
|
141
|
-
```ts
|
|
142
|
-
useField(form, "username", { debounceMs: 300 }); // debounces validation
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
Path can also be a selector:
|
|
146
|
-
|
|
147
|
-
```ts
|
|
148
|
-
useField(form, (state) => `users.${state.values.selectedIdx}.email`);
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
### `useFieldArray(form, path)`
|
|
152
|
-
|
|
153
|
-
Array operations + stable IDs for React keys:
|
|
154
|
-
|
|
155
|
-
```ts
|
|
156
|
-
const users = useFieldArray<UserItem>(form, "users");
|
|
157
|
-
users.fields.map((f, i) => (
|
|
158
|
-
<UserRow key={f.id} item={f.value} index={i} />
|
|
159
|
-
));
|
|
160
|
-
users.push({...});
|
|
161
|
-
users.remove(i);
|
|
162
|
-
users.move(from, to);
|
|
163
|
-
users.swap(a, b);
|
|
164
|
-
users.insert(i, item);
|
|
165
|
-
users.error; // array-level error (e.g. min(1))
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
`fields` IDs are shared per `(form, path)` — every hook on the same array sees
|
|
169
|
-
the same ids. Array ops (hook-issued or imperative `form.arrayMove` etc.)
|
|
170
|
-
record their exact index mapping and id derivation replays it, so even rows
|
|
171
|
-
with equal values keep the right ids through ops; whole-array writes
|
|
172
|
-
(`setValue`, `restore`) reconcile by item identity with a positional fallback,
|
|
173
|
-
so editing a field keeps its row's `id` (the row updates instead of
|
|
174
|
-
remounting). A genuinely new item gets a fresh `id`.
|
|
175
|
-
|
|
176
|
-
### `useFormSelector(form, selector)` / `useFormSelectorShallow(form, selector)`
|
|
177
|
-
|
|
178
|
-
Selector-style subscription. Use `useFormSelectorShallow` for selectors that return objects/arrays.
|
|
179
|
-
|
|
180
|
-
> Formerly `useFormState` / `useFormStateShallow` — renamed because React DOM
|
|
181
|
-
> ships its own (deprecated) `useFormState` and auto-imports regularly grabbed
|
|
182
|
-
> the wrong one. The old names were removed in 0.4.0.
|
|
183
|
-
|
|
184
|
-
### `useFormError(form)`
|
|
185
|
-
|
|
186
|
-
Shortcut for the root-level error (errors at the `""` key from a schema-level `.refine`).
|
|
187
|
-
|
|
188
|
-
### Flag hooks
|
|
189
|
-
|
|
190
|
-
```ts
|
|
191
|
-
useIsDirty(form); // any field dirty
|
|
192
|
-
useIsValid(form); // no errors currently in the error map
|
|
193
|
-
useIsSubmitting(form);
|
|
194
|
-
useSubmitCount(form);
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
> **`useIsValid` reflects the error map, not a fresh validation.** It returns
|
|
198
|
-
> `true` when no errors are currently recorded — covering both schema errors and
|
|
199
|
-
> server errors set via `setError`. But the error map is empty until validation
|
|
200
|
-
> runs, so a never-validated form reads as valid even if its initial values
|
|
201
|
-
> would fail the schema. If you gate a submit button on `!useIsValid(form)`,
|
|
202
|
-
> pass `validateOnMount: true` to `useForm`/`createForm` so the initial values
|
|
203
|
-
> are checked up front. (`submit`/`handleSubmit` always re-validate regardless,
|
|
204
|
-
> so an invalid form can't actually be submitted either way.) `validateOnMount`
|
|
205
|
-
> surfaces errors for untouched fields immediately — gate error display on
|
|
206
|
-
> `touched` if you don't want them shown before interaction.
|
|
207
|
-
|
|
208
|
-
### Bound input components
|
|
209
|
-
|
|
210
|
-
```tsx
|
|
211
|
-
<TextField form={form} path="email" label="Email" type="email" />
|
|
212
|
-
<NumberField form={form} path="age" label="Age" />
|
|
213
|
-
<CheckboxField form={form} path="agree" label="I agree" />
|
|
214
|
-
<SelectField form={form} path="theme" label="Theme" placeholder="Pick a theme"
|
|
215
|
-
options={[{ value: "light", label: "Light" }, { value: "dark", label: "Dark" }]} />
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
All bound components ship with accessibility wiring: `name={path}` (autofill,
|
|
219
|
-
password managers, native form posts), `aria-invalid` while errored,
|
|
220
|
-
`aria-describedby` pointing at the error message, and the error rendered with
|
|
221
|
-
`role="alert"` so it's announced when it appears. Each also accepts a `ref`
|
|
222
|
-
to the underlying `<input>`/`<select>`.
|
|
223
|
-
|
|
224
|
-
`SelectField` stays controlled while the field value is `undefined` by rendering
|
|
225
|
-
a disabled empty option (with your `placeholder` text, if given), so the blank
|
|
226
|
-
state is visible instead of the browser silently showing the first option.
|
|
227
|
-
|
|
228
|
-
`NumberField` renders a `type="text"` input with `inputMode="decimal"` and keeps
|
|
229
|
-
the raw text while you type, so partial entries (`-`, `1.`, `1e`) survive instead
|
|
230
|
-
of being coerced away by a controlled `<input type="number">`. Each keystroke
|
|
231
|
-
that parses to a **finite** number is pushed to the form (whitespace counts as
|
|
232
|
-
empty → `undefined`; `Infinity` is rejected), and the display snaps to the
|
|
233
|
-
canonical value on blur. If something else writes the field while you're typing
|
|
234
|
-
(`reset`, `adoptValues`, another component), the external value wins and the
|
|
235
|
-
input updates immediately.
|
|
236
|
-
|
|
237
|
-
Or roll your own with the prop helpers:
|
|
58
|
+
Prefer your own markup? Every binding is also a plain function over a `useField` result:
|
|
238
59
|
|
|
239
60
|
```tsx
|
|
240
61
|
<input {...textInputProps(useField(form, "name"))} />
|
|
241
|
-
<input {...checkboxProps(useField(form, "agree"))} />
|
|
242
|
-
<select {...selectProps(useField(form, "theme"))}>...</select>
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
`numberInputProps` is also exported as a stateless `<input type="number">` binding
|
|
246
|
-
(native stepper + `step`), at the cost of the intermediate-entry behaviour above:
|
|
247
|
-
|
|
248
|
-
```tsx
|
|
249
|
-
<input {...numberInputProps(useField(form, "age"))} type="number" step="1" />
|
|
250
|
-
```
|
|
251
|
-
|
|
252
|
-
### Focus management
|
|
253
|
-
|
|
254
|
-
`focusFirstError(errors, root?)` focuses the first control (in DOM order) whose
|
|
255
|
-
`name` matches an errored path — the bound components set `name={path}` so this
|
|
256
|
-
works out of the box. Wire it into the invalid-submit handler:
|
|
257
|
-
|
|
258
|
-
```tsx
|
|
259
|
-
<form onSubmit={form.handleSubmit(onValid, (errors) => focusFirstError(errors))}>
|
|
260
|
-
```
|
|
261
|
-
|
|
262
|
-
On a page with several forms, pass the form element (e.g. via a ref) as `root`
|
|
263
|
-
so the search stays inside your form — without it the whole document is
|
|
264
|
-
scanned, and a root-`""` error's first-control fallback refuses to guess
|
|
265
|
-
between forms (it returns `false` when the document holds more than one
|
|
266
|
-
`<form>`):
|
|
267
|
-
|
|
268
|
-
```tsx
|
|
269
|
-
<form ref={formRef} onSubmit={form.handleSubmit(onValid, (errors) => focusFirstError(errors, formRef.current ?? undefined))}>
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
It returns `true` only when a control actually received focus — disabled or
|
|
273
|
-
hidden controls, controls inside a closed `<dialog>`, and controls that refuse
|
|
274
|
-
focus (e.g. `display: none`) are passed over for the next match.
|
|
275
|
-
|
|
276
|
-
For custom focus logic, every bound component also takes a `ref` to its input.
|
|
277
|
-
|
|
278
|
-
## Sharing a form across components
|
|
279
|
-
|
|
280
|
-
`useForm(schema, options)` creates a form bound to the calling component's lifetime. Calling `useForm` twice — even with the same schema — gives you **two independent forms with two independent stores**. The hook uses `useState(() => createForm(...))` under the hood, and `useState` is per-component-instance.
|
|
281
|
-
|
|
282
|
-
To share one form between components, use one of these patterns:
|
|
283
|
-
|
|
284
|
-
### 1. Lift the form up and pass it down as a prop
|
|
285
|
-
|
|
286
|
-
```tsx
|
|
287
|
-
const Parent = () => {
|
|
288
|
-
const form = useForm(schema, { initialValues });
|
|
289
|
-
return (
|
|
290
|
-
<>
|
|
291
|
-
<NameField form={form} />
|
|
292
|
-
<EmailField form={form} />
|
|
293
|
-
</>
|
|
294
|
-
);
|
|
295
|
-
};
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
Each child takes `form: FieldFormApi` (or the typed `Form<typeof schema>`) and calls `useField(form, "...")` on it. Works for small trees; gets noisy if the tree is deep.
|
|
299
|
-
|
|
300
|
-
### 2. `createFormContext` — no prop drilling, full typing
|
|
301
|
-
|
|
302
|
-
```tsx
|
|
303
|
-
import { createFormContext } from "formstand";
|
|
304
|
-
|
|
305
|
-
const { Provider, useFormContext } = createFormContext<typeof schema>();
|
|
306
|
-
|
|
307
|
-
const Parent = () => {
|
|
308
|
-
const form = useForm(schema, { initialValues });
|
|
309
|
-
return (
|
|
310
|
-
<Provider form={form}>
|
|
311
|
-
<NameField />
|
|
312
|
-
<EmailField />
|
|
313
|
-
</Provider>
|
|
314
|
-
);
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
// In NameField.tsx — no form prop:
|
|
318
|
-
const NameField = () => {
|
|
319
|
-
const form = useFormContext(); // typed Form<typeof schema>
|
|
320
|
-
const name = useField(form, "name");
|
|
321
|
-
return <input {...textInputProps(name)} />;
|
|
322
|
-
};
|
|
323
62
|
```
|
|
324
63
|
|
|
325
|
-
|
|
64
|
+
## Or generate the whole thing
|
|
326
65
|
|
|
327
|
-
|
|
66
|
+
[`formstand-cli`](https://scrumrot.github.io/formstand/documentation/cli/) writes the
|
|
67
|
+
component from a schema you already have. It is a dev dependency that produces plain
|
|
68
|
+
code and adds nothing to your bundle.
|
|
328
69
|
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
##
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
plain
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
-
|
|
382
|
-
|
|
383
|
-
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
// setError's path is typed; a server-provided string needs a cast
|
|
396
|
-
form.setError(err.field as FieldPath<z.input<typeof schema>>, [err.message]);
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
});
|
|
400
|
-
```
|
|
401
|
-
|
|
402
|
-
Errors live in two channels. `schemaErrors` is owned by validation: every pass rebuilds it (full passes wholesale, field-scoped passes splice their scope). `serverErrors` is owned by you: `setError`/`setErrors` write it and validation never reads or writes it — which is the entire preservation story. A background `validateAsync()` can't wipe a "username taken" message because it physically can't touch that channel; there are no marks or bookkeeping to get wrong. The `errors` map every hook reads is derived from both on each write: the schema's message wins at a key, server entries show where the schema is silent. The merge is order-independent — `setError` on a key the schema currently rejects stores the verdict (visible in `state.serverErrors`) behind the schema message, and if the schema later clears at that key without the value changing, the stored verdict resurfaces, since the value it judged is unchanged.
|
|
403
|
-
|
|
404
|
-
A server error is released when:
|
|
405
|
-
|
|
406
|
-
- the value on its spine changes (`setValue`/`resetField`/array op on the path, a descendant, or an ancestor — editing `address.street` releases a verdict on `address`; array ops release verdicts on the array itself while row-level entries follow their rows through re-indexing),
|
|
407
|
-
- a `setValues` bulk write changes its value slice (entries on untouched fields survive),
|
|
408
|
-
- a field-scoped validation (`validateField`/`validateFieldAsync`/`validateFields`) targets its path (`validateField("")` counts as a full pass and leaves the server channel alone, like `validate()`), or
|
|
409
|
-
- you call `clearErrors` / `reset` / `adoptValues`.
|
|
410
|
-
|
|
411
|
-
Note `submit` still proceeds when the schema is valid even if server errors are present (the server gets to re-judge); they simply remain in the merged map, so `useIsValid` stays `false` until the user edits the field.
|
|
412
|
-
|
|
413
|
-
## Field arrays with nested arrays
|
|
414
|
-
|
|
415
|
-
Both work — `arrayPush("users.0.tags", tag)` mutates the inner array; outer reorders correctly re-key meta for all nested paths. Inner row ids live per `(form, path)` and reconcile by value when the outer index re-seats a different item at the path — pass a `key` from the outer `fields` so each row's whole subtree moves with its item.
|
|
416
|
-
|
|
417
|
-
## Optimistic UI
|
|
418
|
-
|
|
419
|
-
```ts
|
|
420
|
-
form.handleSubmit(async (data) => {
|
|
421
|
-
const snap = form.snapshot();
|
|
422
|
-
form.adoptValues(data); // optimistic: data becomes the new baseline
|
|
423
|
-
try {
|
|
424
|
-
const saved = await api.save(data);
|
|
425
|
-
form.adoptValues(saved); // confirm with server response
|
|
426
|
-
} catch (e) {
|
|
427
|
-
form.restore(snap); // rollback
|
|
428
|
-
}
|
|
429
|
-
});
|
|
430
|
-
```
|
|
70
|
+
```bash
|
|
71
|
+
npm install -D formstand-cli
|
|
72
|
+
npx formstand-gen src/profileSchema.ts --ui mui --sections panel --columns 2 --out src/ProfileForm.tsx
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
You get typed `initialValues`, a bound control per field, sections for nested objects,
|
|
76
|
+
`useFieldArray` blocks with add and remove buttons, and a wired submit. Point it at a
|
|
77
|
+
TypeScript type instead (`--type Profile`) and it generates the zod schema too. `--ui`
|
|
78
|
+
targets plain formstand components, Material UI 5 through 9, shadcn/ui, Chakra 3,
|
|
79
|
+
Mantine 9, or Ant Design 6.
|
|
80
|
+
|
|
81
|
+
Try it without installing anything in the
|
|
82
|
+
[Schema builder](https://scrumrot.github.io/formstand/examples/#/schema-builder), which
|
|
83
|
+
runs the real emitters in your browser.
|
|
84
|
+
|
|
85
|
+
## How it works
|
|
86
|
+
|
|
87
|
+
Your zod schema is the single source of truth. It defines the value types, the set of
|
|
88
|
+
addressable paths, and the validation rules, and `useForm` builds a form whose entire
|
|
89
|
+
state lives in one plain zustand store. Hooks subscribe to slices of that store, so a
|
|
90
|
+
field re-renders only when its own slice changes. Because the store is ordinary zustand,
|
|
91
|
+
everything works outside React too: read it, subscribe to it, or drive it imperatively.
|
|
92
|
+
|
|
93
|
+
Errors live in two channels that cannot clobber each other. Validation owns
|
|
94
|
+
`schemaErrors` and rebuilds it on every pass; your `setError` calls own `serverErrors`
|
|
95
|
+
and validation never touches them. The map your UI reads is derived from both. That is
|
|
96
|
+
why a background `validateAsync()` cannot wipe a "username already taken" message, and
|
|
97
|
+
why the server verdict is released automatically when the user edits that field, with no
|
|
98
|
+
`clearErrors` bookkeeping in your change handlers.
|
|
99
|
+
|
|
100
|
+
Anything that can be derived is. Dirtiness is computed by comparing values against
|
|
101
|
+
initial values rather than tracked as you write, so it cannot drift out of sync,
|
|
102
|
+
`arrayPush` followed by `arrayRemove` reads clean again, and `diff()` gives you a
|
|
103
|
+
PATCH-ready payload of exactly what changed.
|
|
104
|
+
|
|
105
|
+
## The API, at a glance
|
|
106
|
+
|
|
107
|
+
| Area | What's there | Docs |
|
|
108
|
+
| --- | --- | --- |
|
|
109
|
+
| Form instance | `createForm`, `setValue`/`setValues`, `reset`/`resetField`/`adoptValues`, `submit`/`handleSubmit`, `diff`/`dirtyFields`, `snapshot`/`restore`, array ops | [createForm & Form](https://scrumrot.github.io/formstand/documentation/api/) |
|
|
110
|
+
| Hooks | `useForm`, `useField`, `useFieldArray`, `useVariantField`, `useFormSelector`, `useFormValues`, the flag hooks, `createFormContext`, `createFormHooks` | [Hooks](https://scrumrot.github.io/formstand/documentation/api/hooks) |
|
|
111
|
+
| Components | `TextField`, `NumberField`, `DateField`, `CheckboxField`, `SelectField`, the prop builders, `useNumberInput`, `focusField`/`focusFirstError` | [Components & bindings](https://scrumrot.github.io/formstand/documentation/api/components) |
|
|
112
|
+
| Validation | five modes plus `reValidateMode`, field-scoped passes, async refines with race handling, per-field `debounceMs` | [Validation](https://scrumrot.github.io/formstand/documentation/validation) |
|
|
113
|
+
| Utilities | `parsePath`/`getAtPath`/`setAtPath`, `validateSync`/`validateAsync`, `persistForm`, and the full type list | [Utilities & types](https://scrumrot.github.io/formstand/documentation/api/utilities) |
|
|
114
|
+
|
|
115
|
+
Coming from react-hook-form? The
|
|
116
|
+
[migration guide](https://scrumrot.github.io/formstand/documentation/migrating-from-react-hook-form)
|
|
117
|
+
maps `register`, `watch`, `setError`, `setFocus`, and `useFieldArray` onto their
|
|
118
|
+
counterparts, and is honest about what has none.
|
|
119
|
+
|
|
120
|
+
## Things that catch people out
|
|
121
|
+
|
|
122
|
+
- **Object-returning selectors need `useFormSelectorShallow`.** A selector returning a
|
|
123
|
+
fresh object every call makes React bail with *"Maximum update depth exceeded"*.
|
|
124
|
+
- **`useIsValid` reflects the error map, not a fresh parse.** A never-validated form
|
|
125
|
+
reads as valid. Pass `validateOnMount: true` if you gate a submit button on it.
|
|
126
|
+
- **Sync `validate`/`validateField` return `{ kind: "pending" }` on async schemas.**
|
|
127
|
+
They start the async pass for you, so check `result.kind` rather than assuming.
|
|
128
|
+
- **`z.coerce.*` collapses typed paths**, because in zod v4 its input type is `unknown`.
|
|
129
|
+
Keep the input type honest and parse in a `.transform`/`.pipe`, or use `NumberField`.
|
|
130
|
+
- **Explicit type arguments are a compile error** on a schema-typed form. The value type
|
|
131
|
+
infers from the path; `useField<Values>(form, "email")` fails on purpose, with a
|
|
132
|
+
message telling you to drop it.
|
|
133
|
+
|
|
134
|
+
Each of these is covered properly in the
|
|
135
|
+
[documentation](https://scrumrot.github.io/formstand/documentation/).
|
|
431
136
|
|
|
432
137
|
## Contributing
|
|
433
138
|
|
|
434
|
-
See [CONTRIBUTING.md](./CONTRIBUTING.md)
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
[ROADMAP.md](./ROADMAP.md).
|
|
139
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md). Note the repo has three independent npm roots
|
|
140
|
+
(`.`, `examples/`, `cli/`), and the root test suite needs `examples/` installed. Where
|
|
141
|
+
the project is headed lives in [ROADMAP.md](./ROADMAP.md).
|
|
438
142
|
|
|
439
143
|
## License
|
|
440
144
|
|