@railway-ts/use-form 0.1.29 → 0.1.31
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/README.md +1 -1
- package/dist/index.mjs +47 -34
- package/dist/index.mjs.map +1 -1
- package/docs/API.md +37 -4
- package/docs/GETTING_STARTED.md +6 -0
- package/docs/RECIPES.md +148 -25
- package/package.json +15 -15
package/docs/API.md
CHANGED
|
@@ -1520,11 +1520,39 @@ String type for field paths.
|
|
|
1520
1520
|
type FieldPath = string;
|
|
1521
1521
|
```
|
|
1522
1522
|
|
|
1523
|
-
|
|
1523
|
+
**Canonical form is dot notation.** Form state — `form.touched`, `form.errors`,
|
|
1524
|
+
`form.clientErrors`, `form.serverErrors`, `form.fieldErrors`,
|
|
1525
|
+
`form.validatingFields` — is always keyed in dot form, including numeric array
|
|
1526
|
+
and tuple indices:
|
|
1524
1527
|
|
|
1525
1528
|
- `"user.name"`
|
|
1526
|
-
- `"addresses
|
|
1527
|
-
- `"users
|
|
1529
|
+
- `"addresses.0"`
|
|
1530
|
+
- `"users.2.email"`
|
|
1531
|
+
- `"position.0"` (for a `[number, number, number]` tuple)
|
|
1532
|
+
|
|
1533
|
+
`ExtractFieldPaths<TValues>` only emits dot-form paths, so type-checked
|
|
1534
|
+
callers always pass dot form to `getFieldProps`, `setFieldValue`,
|
|
1535
|
+
`getFieldError`, etc.
|
|
1536
|
+
|
|
1537
|
+
**Bracket notation is accepted at API boundaries** for back-compat. When you
|
|
1538
|
+
pass `"addresses[0]"` to a method (`setFieldValue`, `setFieldTouched`,
|
|
1539
|
+
`getFieldError`, `setServerErrors`, …), it is normalized to `"addresses.0"`
|
|
1540
|
+
before being written to or read from form state. So:
|
|
1541
|
+
|
|
1542
|
+
```typescript
|
|
1543
|
+
form.setFieldTouched('addresses[0]', true); // OK — stored as touched["addresses.0"]
|
|
1544
|
+
form.getFieldError('addresses[0]'); // OK — resolves to errors["addresses.0"]
|
|
1545
|
+
```
|
|
1546
|
+
|
|
1547
|
+
**Direct map reads must use dot form**, because keys are stored canonically:
|
|
1548
|
+
|
|
1549
|
+
```typescript
|
|
1550
|
+
form.errors['addresses.0.city']; // ✓ canonical
|
|
1551
|
+
form.errors['addresses[0].city']; // ✗ undefined — keys aren't stored bracket-form
|
|
1552
|
+
```
|
|
1553
|
+
|
|
1554
|
+
When in doubt, prefer the method-based accessors (`getFieldError`) — they
|
|
1555
|
+
normalize the input — or the dot-form key from `ExtractFieldPaths`.
|
|
1528
1556
|
|
|
1529
1557
|
## Railway Types
|
|
1530
1558
|
|
|
@@ -1555,4 +1583,9 @@ type ValidationError = {
|
|
|
1555
1583
|
};
|
|
1556
1584
|
```
|
|
1557
1585
|
|
|
1558
|
-
Validation error with field path (as array) and message. The `formatErrors`
|
|
1586
|
+
Validation error with field path (as array) and message. The `formatErrors`
|
|
1587
|
+
helper from `@railway-ts/pipelines/schema` joins these path arrays into
|
|
1588
|
+
string keys, using bracket notation for numeric segments
|
|
1589
|
+
(`["users", "0", "name"] → "users[0].name"`). `useForm` normalizes those
|
|
1590
|
+
keys to canonical dot form before storing them in `form.errors`, so what
|
|
1591
|
+
you read off the form is always `"users.0.name"`.
|
package/docs/GETTING_STARTED.md
CHANGED
|
@@ -368,6 +368,12 @@ Array helpers provide full type safety -- `helpers.getFieldProps(index, 'name')`
|
|
|
368
368
|
| `getFieldError(index, field)` | Get error for item field |
|
|
369
369
|
| `getFieldId(index, field, optionValue?)` | Get unique ID for labels/accessibility |
|
|
370
370
|
|
|
371
|
+
For **fixed-length tuples** (e.g. `[number, number, number]` from `tupleOf`),
|
|
372
|
+
skip `arrayHelpers` entirely — `ExtractFieldPaths` emits per-index dot
|
|
373
|
+
paths (`position.0`, `position.1`, …) that you bind directly with
|
|
374
|
+
`getFieldProps('position.0')`. See
|
|
375
|
+
[Recipes › Tuple Fields](./RECIPES.md#tuple-fields-vec3-coordinates-fixed-length-arrays).
|
|
376
|
+
|
|
371
377
|
---
|
|
372
378
|
|
|
373
379
|
## What's Next
|
package/docs/RECIPES.md
CHANGED
|
@@ -8,31 +8,32 @@ Patterns and techniques. Each recipe is self-contained.
|
|
|
8
8
|
|
|
9
9
|
1. [CRUD Form with Server Errors](#crud-form-with-server-errors)
|
|
10
10
|
2. [Nested Objects](#nested-objects)
|
|
11
|
-
3. [
|
|
12
|
-
4. [
|
|
13
|
-
5. [
|
|
14
|
-
6. [
|
|
15
|
-
7. [
|
|
16
|
-
8. [
|
|
17
|
-
9. [
|
|
18
|
-
10. [
|
|
19
|
-
11. [
|
|
20
|
-
12. [
|
|
21
|
-
13. [
|
|
22
|
-
14. [
|
|
23
|
-
15. [
|
|
24
|
-
16. [
|
|
25
|
-
17. [
|
|
26
|
-
18. [
|
|
27
|
-
19. [
|
|
28
|
-
20. [
|
|
29
|
-
21. [
|
|
30
|
-
22. [
|
|
31
|
-
23. [
|
|
32
|
-
24. [
|
|
33
|
-
25. [
|
|
34
|
-
26. [
|
|
35
|
-
27. [Capstone: Full Registration Form (
|
|
11
|
+
3. [Tuple Fields (vec3, coordinates, fixed-length arrays)](#tuple-fields-vec3-coordinates-fixed-length-arrays)
|
|
12
|
+
4. [Dynamic Arrays](#dynamic-arrays)
|
|
13
|
+
5. [Checkbox and Radio Groups](#checkbox-and-radio-groups)
|
|
14
|
+
6. [File Uploads](#file-uploads)
|
|
15
|
+
7. [Auto-Submit and Debounced Search](#auto-submit-and-debounced-search)
|
|
16
|
+
8. [Multi-Step Wizard](#multi-step-wizard)
|
|
17
|
+
9. [Dependent Fields / Cross-Field Validation](#dependent-fields--cross-field-validation)
|
|
18
|
+
10. [Conditional Validation](#conditional-validation)
|
|
19
|
+
11. [Discriminated Unions](#discriminated-unions)
|
|
20
|
+
12. [Custom Validators](#custom-validators)
|
|
21
|
+
13. [Programmatic Field Updates](#programmatic-field-updates)
|
|
22
|
+
14. [Syncing Values to External State](#syncing-values-to-external-state)
|
|
23
|
+
15. [Reacting to Individual Field Changes](#reacting-to-individual-field-changes)
|
|
24
|
+
16. [submitCount Patterns](#submitcount-patterns)
|
|
25
|
+
17. [Understanding Error Priority](#understanding-error-priority)
|
|
26
|
+
18. [Standard Schema: Bring Your Own Validator](#standard-schema-bring-your-own-validator)
|
|
27
|
+
19. [Per-Field Async Validation (fieldValidators)](#per-field-async-validation-fieldvalidators)
|
|
28
|
+
20. [Pattern-Matching Submit Results](#pattern-matching-submit-results)
|
|
29
|
+
21. [Custom Field Components](#custom-field-components)
|
|
30
|
+
22. [Performance Patterns](#performance-patterns)
|
|
31
|
+
23. [UI Library Integration](#ui-library-integration)
|
|
32
|
+
24. [Testing Forms](#testing-forms)
|
|
33
|
+
25. [Form State Persistence](#form-state-persistence)
|
|
34
|
+
26. [Unsaved Changes Warning](#unsaved-changes-warning)
|
|
35
|
+
27. [Capstone: Full Registration Form (Result Pattern)](#capstone-full-registration-form-result-pattern)
|
|
36
|
+
28. [Capstone: Full Registration Form (React Query)](#capstone-full-registration-form-react-query)
|
|
36
37
|
|
|
37
38
|
---
|
|
38
39
|
|
|
@@ -182,6 +183,128 @@ Errors also use dot notation: `form.getFieldError('address.city')`, `form.getFie
|
|
|
182
183
|
|
|
183
184
|
---
|
|
184
185
|
|
|
186
|
+
## Tuple Fields (vec3, coordinates, fixed-length arrays)
|
|
187
|
+
|
|
188
|
+
**Problem:** A field is a fixed-length tuple — three numbers for a position
|
|
189
|
+
vector, two for lat/long, four for a CMYK color — and each element needs its
|
|
190
|
+
own input with its own error message. This is *not* the dynamic-array case
|
|
191
|
+
(no push/remove/reorder); the shape is part of the schema.
|
|
192
|
+
|
|
193
|
+
**Solution:** Use `tupleOf(elementValidator, length)` from
|
|
194
|
+
`@railway-ts/pipelines/schema`. `ExtractFieldPaths<TValues>` automatically
|
|
195
|
+
emits per-index sub-paths in dot form (`r1.0`, `r1.1`, `r1.2`), which you
|
|
196
|
+
bind directly with `getFieldProps`. No `arrayHelpers` involved.
|
|
197
|
+
|
|
198
|
+
```tsx
|
|
199
|
+
import { useForm, type UseFormReturn, type ExtractFieldPaths } from '@railway-ts/use-form';
|
|
200
|
+
import {
|
|
201
|
+
object,
|
|
202
|
+
required,
|
|
203
|
+
tupleOf,
|
|
204
|
+
parseNumber,
|
|
205
|
+
chain,
|
|
206
|
+
positive,
|
|
207
|
+
type InferSchemaType,
|
|
208
|
+
} from '@railway-ts/pipelines/schema';
|
|
209
|
+
|
|
210
|
+
const vec3 = tupleOf(chain(parseNumber(), positive('Must be positive')), 3);
|
|
211
|
+
|
|
212
|
+
const lambertSchema = object({
|
|
213
|
+
r1: required(vec3),
|
|
214
|
+
r2: required(vec3),
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
type LambertValues = InferSchemaType<typeof lambertSchema>;
|
|
218
|
+
|
|
219
|
+
function LambertForm() {
|
|
220
|
+
const form = useForm<LambertValues>(lambertSchema, {
|
|
221
|
+
initialValues: {
|
|
222
|
+
r1: [7000, 0, 0],
|
|
223
|
+
r2: [0, 7000, 0],
|
|
224
|
+
},
|
|
225
|
+
validationMode: 'blur',
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
return (
|
|
229
|
+
<form onSubmit={(e) => void form.handleSubmit(e)} noValidate>
|
|
230
|
+
<VecFieldset
|
|
231
|
+
label="r₁"
|
|
232
|
+
unit="km"
|
|
233
|
+
form={form}
|
|
234
|
+
names={['r1.0', 'r1.1', 'r1.2']}
|
|
235
|
+
/>
|
|
236
|
+
<VecFieldset
|
|
237
|
+
label="r₂"
|
|
238
|
+
unit="km"
|
|
239
|
+
form={form}
|
|
240
|
+
names={['r2.0', 'r2.1', 'r2.2']}
|
|
241
|
+
/>
|
|
242
|
+
|
|
243
|
+
<button type="submit">Solve</button>
|
|
244
|
+
</form>
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
The `VecFieldset` component is a thin wrapper that renders three inputs and
|
|
250
|
+
surfaces the first error. Because `form.errors` is dot-keyed, you read it
|
|
251
|
+
directly with the same path you'd pass to `getFieldProps`:
|
|
252
|
+
|
|
253
|
+
```tsx
|
|
254
|
+
type VecFieldsetProps<TValues extends Record<string, unknown>> = {
|
|
255
|
+
label: React.ReactNode;
|
|
256
|
+
unit?: React.ReactNode;
|
|
257
|
+
form: UseFormReturn<TValues>;
|
|
258
|
+
names: readonly ExtractFieldPaths<TValues>[];
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
function VecFieldset<TValues extends Record<string, unknown>>({
|
|
262
|
+
label,
|
|
263
|
+
unit,
|
|
264
|
+
form,
|
|
265
|
+
names,
|
|
266
|
+
}: VecFieldsetProps<TValues>) {
|
|
267
|
+
const errors = names.map((n) => (form.touched[n] ? form.errors[n] : undefined));
|
|
268
|
+
const firstError = errors.find((e): e is string => Boolean(e));
|
|
269
|
+
|
|
270
|
+
return (
|
|
271
|
+
<fieldset>
|
|
272
|
+
<legend>
|
|
273
|
+
{label}
|
|
274
|
+
{unit && <span> ({unit})</span>}
|
|
275
|
+
</legend>
|
|
276
|
+
<div style={{ display: 'flex', gap: '0.5rem' }}>
|
|
277
|
+
{names.map((name, i) => (
|
|
278
|
+
<input
|
|
279
|
+
key={name}
|
|
280
|
+
type="number"
|
|
281
|
+
step="any"
|
|
282
|
+
aria-invalid={Boolean(errors[i])}
|
|
283
|
+
{...form.getFieldProps(name)}
|
|
284
|
+
/>
|
|
285
|
+
))}
|
|
286
|
+
</div>
|
|
287
|
+
{firstError && <span role="alert">{firstError}</span>}
|
|
288
|
+
</fieldset>
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
**Key points:**
|
|
294
|
+
|
|
295
|
+
- `form.errors['r1.0']` is the canonical key. Pre-0.1.31 builds keyed errors
|
|
296
|
+
in bracket form (`r1[0]`); on those versions you'd have to translate the
|
|
297
|
+
path before reading. Upgrade if you're on an older release.
|
|
298
|
+
- `ExtractFieldPaths<LambertValues>` includes `'r1' | 'r1.0' | 'r1.1' |
|
|
299
|
+
'r1.2' | 'r2' | …`, so the `names` array is type-checked against the
|
|
300
|
+
schema. Renaming a tuple field or changing its arity surfaces as a type
|
|
301
|
+
error at the call site.
|
|
302
|
+
- For dynamic-length arrays (`array(...)`, not `tupleOf`), use
|
|
303
|
+
[`arrayHelpers`](#dynamic-arrays) instead — `ExtractFieldPaths` treats
|
|
304
|
+
variable-length arrays as terminal and won't generate per-index paths.
|
|
305
|
+
|
|
306
|
+
---
|
|
307
|
+
|
|
185
308
|
## Dynamic Arrays
|
|
186
309
|
|
|
187
310
|
**Problem:** You need a list that users can add to, remove from, and reorder -- like contacts, line items, or todo lists.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@railway-ts/use-form",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.31",
|
|
4
4
|
"description": "React form hook that works with @railway-ts/pipelines to manage form state, validation, and submission.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
@@ -77,29 +77,29 @@
|
|
|
77
77
|
},
|
|
78
78
|
"devDependencies": {
|
|
79
79
|
"@eslint/js": "^10.0.1",
|
|
80
|
-
"@happy-dom/global-registrator": "^20.
|
|
81
|
-
"@railway-ts/pipelines": "^0.1.
|
|
80
|
+
"@happy-dom/global-registrator": "^20.9.0",
|
|
81
|
+
"@railway-ts/pipelines": "^0.1.29",
|
|
82
82
|
"@testing-library/dom": "^10.4.1",
|
|
83
83
|
"@testing-library/jest-dom": "^6.9.1",
|
|
84
84
|
"@testing-library/react": "^16.3.2",
|
|
85
85
|
"@testing-library/user-event": "^14.6.1",
|
|
86
|
-
"@types/bun": "^1.3.
|
|
86
|
+
"@types/bun": "^1.3.12",
|
|
87
87
|
"@types/react": "^19.2.14",
|
|
88
88
|
"@types/react-dom": "^19.2.3",
|
|
89
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
90
|
-
"@typescript-eslint/parser": "^8.
|
|
91
|
-
"eslint": "^10.
|
|
89
|
+
"@typescript-eslint/eslint-plugin": "^8.58.2",
|
|
90
|
+
"@typescript-eslint/parser": "^8.58.2",
|
|
91
|
+
"eslint": "^10.2.1",
|
|
92
92
|
"eslint-config-prettier": "^10.1.8",
|
|
93
|
-
"@eslint-react/eslint-plugin": "^2.
|
|
94
|
-
"eslint-plugin-react-hooks": "^7.
|
|
95
|
-
"prettier": "^3.8.
|
|
96
|
-
"react": "^19.2.
|
|
97
|
-
"react-dom": "^19.2.
|
|
98
|
-
"@size-limit/preset-small-lib": "^12.
|
|
99
|
-
"size-limit": "^12.
|
|
93
|
+
"@eslint-react/eslint-plugin": "^4.2.3",
|
|
94
|
+
"eslint-plugin-react-hooks": "^7.1.1",
|
|
95
|
+
"prettier": "^3.8.3",
|
|
96
|
+
"react": "^19.2.5",
|
|
97
|
+
"react-dom": "^19.2.5",
|
|
98
|
+
"@size-limit/preset-small-lib": "^12.1.0",
|
|
99
|
+
"size-limit": "^12.1.0",
|
|
100
100
|
"tsup": "^8.5.1",
|
|
101
101
|
"typescript": "^6.0.0",
|
|
102
|
-
"valibot": "^1.
|
|
102
|
+
"valibot": "^1.3.1",
|
|
103
103
|
"zod": "^4.3.6"
|
|
104
104
|
}
|
|
105
105
|
}
|