@railway-ts/use-form 0.1.26 → 0.1.28
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 +11 -37
- package/dist/index.d.ts +3 -1
- package/dist/index.mjs +46 -164
- package/dist/index.mjs.map +1 -1
- package/docs/API.md +39 -40
- package/docs/GETTING_STARTED.md +34 -84
- package/docs/RECIPES.md +65 -248
- package/package.json +16 -16
package/docs/API.md
CHANGED
|
@@ -10,10 +10,8 @@ Main hook for form management.
|
|
|
10
10
|
|
|
11
11
|
```typescript
|
|
12
12
|
function useForm<TValues extends Record<string, unknown>>(
|
|
13
|
-
validator:
|
|
14
|
-
|
|
15
|
-
| StandardSchemaV1<unknown, TValues>,
|
|
16
|
-
options: FormOptions<TValues>
|
|
13
|
+
validator: MaybeAsyncValidator<unknown, TValues> | StandardSchemaV1<unknown, TValues>,
|
|
14
|
+
options: FormOptions<TValues>,
|
|
17
15
|
): UseFormReturn<TValues>;
|
|
18
16
|
```
|
|
19
17
|
|
|
@@ -1132,9 +1130,7 @@ getFieldError<TField extends ExtractFieldPaths<TItem>>(
|
|
|
1132
1130
|
|
|
1133
1131
|
```tsx
|
|
1134
1132
|
{
|
|
1135
|
-
helpers.getFieldError(0, 'name') && (
|
|
1136
|
-
<span>{helpers.getFieldError(0, 'name')}</span>
|
|
1137
|
-
);
|
|
1133
|
+
helpers.getFieldError(0, 'name') && <span>{helpers.getFieldError(0, 'name')}</span>;
|
|
1138
1134
|
}
|
|
1139
1135
|
```
|
|
1140
1136
|
|
|
@@ -1166,10 +1162,7 @@ is dirty, valid, and values have changed since the last validation.
|
|
|
1166
1162
|
### Signature
|
|
1167
1163
|
|
|
1168
1164
|
```typescript
|
|
1169
|
-
function useFormAutoSubmission<T>(
|
|
1170
|
-
form: FormWithAutoSubmit<T>,
|
|
1171
|
-
delay?: number
|
|
1172
|
-
): null;
|
|
1165
|
+
function useFormAutoSubmission<T>(form: FormWithAutoSubmit<T>, delay?: number): null;
|
|
1173
1166
|
```
|
|
1174
1167
|
|
|
1175
1168
|
### Parameters
|
|
@@ -1207,10 +1200,7 @@ Hook for debouncing a callback function.
|
|
|
1207
1200
|
### Signature
|
|
1208
1201
|
|
|
1209
1202
|
```typescript
|
|
1210
|
-
function useDebounce<T extends (...args: Param) => Return>(
|
|
1211
|
-
callback: T,
|
|
1212
|
-
delay: number
|
|
1213
|
-
): (...args: Param) => void;
|
|
1203
|
+
function useDebounce<T extends (...args: Param) => Return>(callback: T, delay: number): (...args: Param) => void;
|
|
1214
1204
|
```
|
|
1215
1205
|
|
|
1216
1206
|
### Parameters
|
|
@@ -1242,11 +1232,7 @@ Functions from `@railway-ts/pipelines/schema` for cross-field validation.
|
|
|
1242
1232
|
Create validators that target specific fields with errors while accessing the entire object.
|
|
1243
1233
|
|
|
1244
1234
|
```typescript
|
|
1245
|
-
function refineAt<T>(
|
|
1246
|
-
targetPath: string | string[],
|
|
1247
|
-
predicate: (value: T) => boolean,
|
|
1248
|
-
message: string
|
|
1249
|
-
): Validator<T, T>;
|
|
1235
|
+
function refineAt<T>(targetPath: string | string[], predicate: (value: T) => boolean, message: string): Validator<T, T>;
|
|
1250
1236
|
```
|
|
1251
1237
|
|
|
1252
1238
|
**Parameters:**
|
|
@@ -1268,11 +1254,7 @@ const validator = chain(
|
|
|
1268
1254
|
password: required(string()),
|
|
1269
1255
|
confirmPassword: required(string()),
|
|
1270
1256
|
}),
|
|
1271
|
-
refineAt(
|
|
1272
|
-
'confirmPassword',
|
|
1273
|
-
(data) => data.password === data.confirmPassword,
|
|
1274
|
-
'Passwords must match'
|
|
1275
|
-
)
|
|
1257
|
+
refineAt('confirmPassword', (data) => data.password === data.confirmPassword, 'Passwords must match'),
|
|
1276
1258
|
);
|
|
1277
1259
|
```
|
|
1278
1260
|
|
|
@@ -1292,8 +1274,8 @@ const accountValidator = chain(
|
|
|
1292
1274
|
refineAt<AccountData>(
|
|
1293
1275
|
'taxId',
|
|
1294
1276
|
(data) => data.accountType === 'personal' || !!data.taxId,
|
|
1295
|
-
'Tax ID is required for business accounts'
|
|
1296
|
-
)
|
|
1277
|
+
'Tax ID is required for business accounts',
|
|
1278
|
+
),
|
|
1297
1279
|
);
|
|
1298
1280
|
```
|
|
1299
1281
|
|
|
@@ -1308,11 +1290,7 @@ const bookingValidator = chain(
|
|
|
1308
1290
|
checkIn: required(parseDate()),
|
|
1309
1291
|
checkOut: required(parseDate()),
|
|
1310
1292
|
}),
|
|
1311
|
-
refineAt(
|
|
1312
|
-
'checkOut',
|
|
1313
|
-
(data) => data.checkOut > data.checkIn,
|
|
1314
|
-
'Check-out must be after check-in'
|
|
1315
|
-
)
|
|
1293
|
+
refineAt('checkOut', (data) => data.checkOut > data.checkIn, 'Check-out must be after check-in'),
|
|
1316
1294
|
);
|
|
1317
1295
|
```
|
|
1318
1296
|
|
|
@@ -1341,7 +1319,17 @@ function when<T>(
|
|
|
1341
1319
|
**Example:**
|
|
1342
1320
|
|
|
1343
1321
|
```typescript
|
|
1344
|
-
import {
|
|
1322
|
+
import {
|
|
1323
|
+
chain,
|
|
1324
|
+
object,
|
|
1325
|
+
required,
|
|
1326
|
+
optional,
|
|
1327
|
+
string,
|
|
1328
|
+
stringEnum,
|
|
1329
|
+
refineAt,
|
|
1330
|
+
when,
|
|
1331
|
+
type InferSchemaType,
|
|
1332
|
+
} from '@railway-ts/pipelines/schema';
|
|
1345
1333
|
|
|
1346
1334
|
const baseSchema = object({
|
|
1347
1335
|
accountType: required(stringEnum(['personal', 'business'])),
|
|
@@ -1388,7 +1376,16 @@ function whenAsync<T>(
|
|
|
1388
1376
|
**Example:**
|
|
1389
1377
|
|
|
1390
1378
|
```typescript
|
|
1391
|
-
import {
|
|
1379
|
+
import {
|
|
1380
|
+
chainAsync,
|
|
1381
|
+
object,
|
|
1382
|
+
required,
|
|
1383
|
+
optional,
|
|
1384
|
+
string,
|
|
1385
|
+
stringEnum,
|
|
1386
|
+
refineAtAsync,
|
|
1387
|
+
whenAsync,
|
|
1388
|
+
} from '@railway-ts/pipelines/schema';
|
|
1392
1389
|
|
|
1393
1390
|
const staffValidator = chainAsync(
|
|
1394
1391
|
object({
|
|
@@ -1429,7 +1426,7 @@ const strongPassword = refine<string>((value) => {
|
|
|
1429
1426
|
// Cross-field: error lands on the specified target
|
|
1430
1427
|
const schema = chain(
|
|
1431
1428
|
object({ password: required(string()), confirm: required(string()) }),
|
|
1432
|
-
refineAt('confirm', (d) => d.password === d.confirm, 'Must match') // target = 'confirm'
|
|
1429
|
+
refineAt('confirm', (d) => d.password === d.confirm, 'Must match'), // target = 'confirm'
|
|
1433
1430
|
);
|
|
1434
1431
|
```
|
|
1435
1432
|
|
|
@@ -1492,7 +1489,7 @@ type UserAddresses = User['addresses']; // Address[]
|
|
|
1492
1489
|
|
|
1493
1490
|
### ExtractFieldPaths
|
|
1494
1491
|
|
|
1495
|
-
Extract all valid dot-notation field paths from a type.
|
|
1492
|
+
Extract all valid dot-notation field paths from a type. Dates and variable-length arrays are terminal values. Fixed-length tuples emit indexed sub-paths.
|
|
1496
1493
|
|
|
1497
1494
|
```typescript
|
|
1498
1495
|
import { type ExtractFieldPaths } from '@railway-ts/use-form';
|
|
@@ -1508,6 +1505,11 @@ type User = {
|
|
|
1508
1505
|
|
|
1509
1506
|
type Paths = ExtractFieldPaths<User>;
|
|
1510
1507
|
// "name" | "address" | "address.city" | "address.zip" | "tags"
|
|
1508
|
+
|
|
1509
|
+
// With a tuple field:
|
|
1510
|
+
type Form = { position: [number, number, number]; tags: string[] };
|
|
1511
|
+
type FormPaths = ExtractFieldPaths<Form>;
|
|
1512
|
+
// "position" | "position.0" | "position.1" | "position.2" | "tags"
|
|
1511
1513
|
```
|
|
1512
1514
|
|
|
1513
1515
|
### FieldPath
|
|
@@ -1539,10 +1541,7 @@ Railway result type representing success or failure.
|
|
|
1539
1541
|
### Validator
|
|
1540
1542
|
|
|
1541
1543
|
```typescript
|
|
1542
|
-
type Validator<TInput, TOutput> = (
|
|
1543
|
-
value: TInput,
|
|
1544
|
-
path?: string[]
|
|
1545
|
-
) => Result<TOutput, ValidationError[]>;
|
|
1544
|
+
type Validator<TInput, TOutput> = (value: TInput, path?: string[]) => Result<TOutput, ValidationError[]>;
|
|
1546
1545
|
```
|
|
1547
1546
|
|
|
1548
1547
|
Function that validates input and returns typed output or errors.
|
package/docs/GETTING_STARTED.md
CHANGED
|
@@ -10,14 +10,7 @@ Every `useForm` starts with a **schema** -- an object that describes what your f
|
|
|
10
10
|
|
|
11
11
|
```tsx
|
|
12
12
|
import { useForm } from '@railway-ts/use-form';
|
|
13
|
-
import {
|
|
14
|
-
object,
|
|
15
|
-
string,
|
|
16
|
-
required,
|
|
17
|
-
chain,
|
|
18
|
-
nonEmpty,
|
|
19
|
-
type InferSchemaType,
|
|
20
|
-
} from '@railway-ts/pipelines/schema';
|
|
13
|
+
import { object, string, required, chain, nonEmpty, type InferSchemaType } from '@railway-ts/pipelines/schema';
|
|
21
14
|
|
|
22
15
|
// 1. Define a schema
|
|
23
16
|
const loginSchema = object({
|
|
@@ -85,17 +78,13 @@ function LoginForm() {
|
|
|
85
78
|
<div>
|
|
86
79
|
<label htmlFor={form.getFieldId('email')}>Email</label>
|
|
87
80
|
<input type="email" {...form.getFieldProps('email')} />
|
|
88
|
-
{form.getFieldError('email') && (
|
|
89
|
-
<span className="error">{form.getFieldError('email')}</span>
|
|
90
|
-
)}
|
|
81
|
+
{form.getFieldError('email') && <span className="error">{form.getFieldError('email')}</span>}
|
|
91
82
|
</div>
|
|
92
83
|
|
|
93
84
|
<div>
|
|
94
85
|
<label htmlFor={form.getFieldId('password')}>Password</label>
|
|
95
86
|
<input type="password" {...form.getFieldProps('password')} />
|
|
96
|
-
{form.getFieldError('password') && (
|
|
97
|
-
<span className="error">{form.getFieldError('password')}</span>
|
|
98
|
-
)}
|
|
87
|
+
{form.getFieldError('password') && <span className="error">{form.getFieldError('password')}</span>}
|
|
99
88
|
</div>
|
|
100
89
|
|
|
101
90
|
<button type="submit" disabled={form.isSubmitting}>
|
|
@@ -142,20 +131,8 @@ import {
|
|
|
142
131
|
} from '@railway-ts/pipelines/schema';
|
|
143
132
|
|
|
144
133
|
const signupSchema = object({
|
|
145
|
-
email: required(
|
|
146
|
-
|
|
147
|
-
string(),
|
|
148
|
-
nonEmpty('Email is required'),
|
|
149
|
-
email('Must be a valid email')
|
|
150
|
-
)
|
|
151
|
-
),
|
|
152
|
-
password: required(
|
|
153
|
-
chain(
|
|
154
|
-
string(),
|
|
155
|
-
nonEmpty('Password is required'),
|
|
156
|
-
minLength(8, 'Must be at least 8 characters')
|
|
157
|
-
)
|
|
158
|
-
),
|
|
134
|
+
email: required(chain(string(), nonEmpty('Email is required'), email('Must be a valid email'))),
|
|
135
|
+
password: required(chain(string(), nonEmpty('Password is required'), minLength(8, 'Must be at least 8 characters'))),
|
|
159
136
|
});
|
|
160
137
|
```
|
|
161
138
|
|
|
@@ -237,19 +214,13 @@ function RegistrationForm() {
|
|
|
237
214
|
return (
|
|
238
215
|
<form onSubmit={(e) => void form.handleSubmit(e)}>
|
|
239
216
|
<input {...form.getFieldProps('username')} />
|
|
240
|
-
{form.getFieldError('username') && (
|
|
241
|
-
<span>{form.getFieldError('username')}</span>
|
|
242
|
-
)}
|
|
217
|
+
{form.getFieldError('username') && <span>{form.getFieldError('username')}</span>}
|
|
243
218
|
|
|
244
219
|
<input type="email" {...form.getFieldProps('email')} />
|
|
245
|
-
{form.getFieldError('email') && (
|
|
246
|
-
<span>{form.getFieldError('email')}</span>
|
|
247
|
-
)}
|
|
220
|
+
{form.getFieldError('email') && <span>{form.getFieldError('email')}</span>}
|
|
248
221
|
|
|
249
222
|
<input type="password" {...form.getFieldProps('password')} />
|
|
250
|
-
{form.getFieldError('password') && (
|
|
251
|
-
<span>{form.getFieldError('password')}</span>
|
|
252
|
-
)}
|
|
223
|
+
{form.getFieldError('password') && <span>{form.getFieldError('password')}</span>}
|
|
253
224
|
|
|
254
225
|
<button type="submit" disabled={form.isSubmitting}>
|
|
255
226
|
{form.isSubmitting ? 'Registering...' : 'Register'}
|
|
@@ -285,7 +256,7 @@ const profileSchema = object({
|
|
|
285
256
|
street: required(string()),
|
|
286
257
|
city: required(chain(string(), nonEmpty('City is required'))),
|
|
287
258
|
zip: required(chain(string(), nonEmpty('ZIP is required'))),
|
|
288
|
-
})
|
|
259
|
+
}),
|
|
289
260
|
),
|
|
290
261
|
});
|
|
291
262
|
|
|
@@ -307,13 +278,9 @@ function ProfileForm() {
|
|
|
307
278
|
<legend>Address</legend>
|
|
308
279
|
<input {...form.getFieldProps('address.street')} placeholder="Street" />
|
|
309
280
|
<input {...form.getFieldProps('address.city')} placeholder="City" />
|
|
310
|
-
{form.getFieldError('address.city') && (
|
|
311
|
-
<span>{form.getFieldError('address.city')}</span>
|
|
312
|
-
)}
|
|
281
|
+
{form.getFieldError('address.city') && <span>{form.getFieldError('address.city')}</span>}
|
|
313
282
|
<input {...form.getFieldProps('address.zip')} placeholder="ZIP" />
|
|
314
|
-
{form.getFieldError('address.zip') && (
|
|
315
|
-
<span>{form.getFieldError('address.zip')}</span>
|
|
316
|
-
)}
|
|
283
|
+
{form.getFieldError('address.zip') && <span>{form.getFieldError('address.zip')}</span>}
|
|
317
284
|
</fieldset>
|
|
318
285
|
|
|
319
286
|
<button type="submit">Save</button>
|
|
@@ -331,14 +298,7 @@ The field path autocomplete works through nested objects -- type `'address.'` an
|
|
|
331
298
|
Use `arrayHelpers` for dynamic lists. It gives you `push`, `remove`, `swap`, `insert`, `replace`, and its own `getFieldProps` that takes an index:
|
|
332
299
|
|
|
333
300
|
```tsx
|
|
334
|
-
import {
|
|
335
|
-
object,
|
|
336
|
-
string,
|
|
337
|
-
required,
|
|
338
|
-
email,
|
|
339
|
-
array,
|
|
340
|
-
type InferSchemaType,
|
|
341
|
-
} from '@railway-ts/pipelines/schema';
|
|
301
|
+
import { object, string, required, email, array, type InferSchemaType } from '@railway-ts/pipelines/schema';
|
|
342
302
|
|
|
343
303
|
const contactSchema = object({
|
|
344
304
|
name: required(string()),
|
|
@@ -365,17 +325,10 @@ function ContactsForm() {
|
|
|
365
325
|
<h3>Contact {index + 1}</h3>
|
|
366
326
|
|
|
367
327
|
<input {...helpers.getFieldProps(index, 'name')} placeholder="Name" />
|
|
368
|
-
{helpers.getFieldError(index, 'name') && (
|
|
369
|
-
|
|
370
|
-
)}
|
|
371
|
-
|
|
372
|
-
<input
|
|
373
|
-
{...helpers.getFieldProps(index, 'email')}
|
|
374
|
-
placeholder="Email"
|
|
375
|
-
/>
|
|
376
|
-
{helpers.getFieldError(index, 'email') && (
|
|
377
|
-
<span>{helpers.getFieldError(index, 'email')}</span>
|
|
378
|
-
)}
|
|
328
|
+
{helpers.getFieldError(index, 'name') && <span>{helpers.getFieldError(index, 'name')}</span>}
|
|
329
|
+
|
|
330
|
+
<input {...helpers.getFieldProps(index, 'email')} placeholder="Email" />
|
|
331
|
+
{helpers.getFieldError(index, 'email') && <span>{helpers.getFieldError(index, 'email')}</span>}
|
|
379
332
|
|
|
380
333
|
<button type="button" onClick={() => helpers.remove(index)}>
|
|
381
334
|
Remove
|
|
@@ -383,10 +336,7 @@ function ContactsForm() {
|
|
|
383
336
|
</div>
|
|
384
337
|
))}
|
|
385
338
|
|
|
386
|
-
<button
|
|
387
|
-
type="button"
|
|
388
|
-
onClick={() => helpers.push({ name: '', email: '' })}
|
|
389
|
-
>
|
|
339
|
+
<button type="button" onClick={() => helpers.push({ name: '', email: '' })}>
|
|
390
340
|
Add Contact
|
|
391
341
|
</button>
|
|
392
342
|
|
|
@@ -400,23 +350,23 @@ Array helpers provide full type safety -- `helpers.getFieldProps(index, 'name')`
|
|
|
400
350
|
|
|
401
351
|
**All array operations:**
|
|
402
352
|
|
|
403
|
-
| Member | What it does
|
|
404
|
-
| ----------------------------------------------- |
|
|
405
|
-
| `values`
|
|
406
|
-
| `push(item)`
|
|
407
|
-
| `remove(index)`
|
|
408
|
-
| `insert(index, item)`
|
|
409
|
-
| `swap(indexA, indexB)`
|
|
410
|
-
| `replace(index, item)`
|
|
411
|
-
| `getFieldProps(index, field)`
|
|
412
|
-
| `getSelectFieldProps(index, field)`
|
|
413
|
-
| `getSliderProps(index, field)`
|
|
414
|
-
| `getCheckboxProps(index, field)`
|
|
415
|
-
| `getSwitchProps(index, field)`
|
|
416
|
-
| `getFileFieldProps(index, field)`
|
|
417
|
-
| `getRadioGroupOptionProps(index, field, value)`
|
|
418
|
-
| `getFieldError(index, field)`
|
|
419
|
-
| `getFieldId(index, field, optionValue?)`
|
|
353
|
+
| Member | What it does |
|
|
354
|
+
| ----------------------------------------------- | -------------------------------------- |
|
|
355
|
+
| `values` | Current array items |
|
|
356
|
+
| `push(item)` | Add to end |
|
|
357
|
+
| `remove(index)` | Remove by index |
|
|
358
|
+
| `insert(index, item)` | Insert at position |
|
|
359
|
+
| `swap(indexA, indexB)` | Swap two items |
|
|
360
|
+
| `replace(index, item)` | Replace item at index |
|
|
361
|
+
| `getFieldProps(index, field)` | Get text/number input props |
|
|
362
|
+
| `getSelectFieldProps(index, field)` | Get select element props |
|
|
363
|
+
| `getSliderProps(index, field)` | Get range input props |
|
|
364
|
+
| `getCheckboxProps(index, field)` | Get checkbox input props |
|
|
365
|
+
| `getSwitchProps(index, field)` | Get switch/toggle props |
|
|
366
|
+
| `getFileFieldProps(index, field)` | Get file input props |
|
|
367
|
+
| `getRadioGroupOptionProps(index, field, value)` | Get radio button props |
|
|
368
|
+
| `getFieldError(index, field)` | Get error for item field |
|
|
369
|
+
| `getFieldId(index, field, optionValue?)` | Get unique ID for labels/accessibility |
|
|
420
370
|
|
|
421
371
|
---
|
|
422
372
|
|