@railway-ts/use-form 0.1.27 → 0.1.29
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 +2 -1
- package/dist/index.mjs +46 -164
- package/dist/index.mjs.map +1 -1
- package/docs/API.md +20 -57
- package/docs/GETTING_STARTED.md +17 -67
- package/docs/RECIPES.md +72 -271
- package/package.json +19 -18
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
|
|
|
@@ -1326,7 +1304,7 @@ Conditionally apply validators based on a runtime predicate. When the predicate
|
|
|
1326
1304
|
function when<T>(
|
|
1327
1305
|
predicate: (data: T) => boolean,
|
|
1328
1306
|
thenValidator: Validator<T, T>,
|
|
1329
|
-
elseValidator?: Validator<T, T
|
|
1307
|
+
elseValidator?: Validator<T, T>,
|
|
1330
1308
|
): Validator<T, T>;
|
|
1331
1309
|
```
|
|
1332
1310
|
|
|
@@ -1366,18 +1344,10 @@ const accountValidator = chain(
|
|
|
1366
1344
|
when<AccountData>(
|
|
1367
1345
|
(d) => d.accountType === 'business',
|
|
1368
1346
|
chain(
|
|
1369
|
-
refineAt(
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
),
|
|
1374
|
-
refineAt(
|
|
1375
|
-
'taxId',
|
|
1376
|
-
(d) => !!d.taxId,
|
|
1377
|
-
'Tax ID is required for business accounts'
|
|
1378
|
-
)
|
|
1379
|
-
)
|
|
1380
|
-
)
|
|
1347
|
+
refineAt('companyName', (d) => !!d.companyName, 'Company name is required for business accounts'),
|
|
1348
|
+
refineAt('taxId', (d) => !!d.taxId, 'Tax ID is required for business accounts'),
|
|
1349
|
+
),
|
|
1350
|
+
),
|
|
1381
1351
|
);
|
|
1382
1352
|
```
|
|
1383
1353
|
|
|
@@ -1391,7 +1361,7 @@ Async version of `when`. Use when the branch validators involve async operations
|
|
|
1391
1361
|
function whenAsync<T>(
|
|
1392
1362
|
predicate: (data: T) => boolean,
|
|
1393
1363
|
thenValidator: MaybeAsyncValidator<T, T>,
|
|
1394
|
-
elseValidator?: MaybeAsyncValidator<T, T
|
|
1364
|
+
elseValidator?: MaybeAsyncValidator<T, T>,
|
|
1395
1365
|
): AsyncValidator<T, T>;
|
|
1396
1366
|
```
|
|
1397
1367
|
|
|
@@ -1424,12 +1394,8 @@ const staffValidator = chainAsync(
|
|
|
1424
1394
|
}),
|
|
1425
1395
|
whenAsync<StaffData>(
|
|
1426
1396
|
(d) => d.role === 'doctor',
|
|
1427
|
-
refineAtAsync(
|
|
1428
|
-
|
|
1429
|
-
async (d) => await verifyMedicalLicense(d.license),
|
|
1430
|
-
'Invalid medical license'
|
|
1431
|
-
)
|
|
1432
|
-
)
|
|
1397
|
+
refineAtAsync('license', async (d) => await verifyMedicalLicense(d.license), 'Invalid medical license'),
|
|
1398
|
+
),
|
|
1433
1399
|
);
|
|
1434
1400
|
```
|
|
1435
1401
|
|
|
@@ -1460,7 +1426,7 @@ const strongPassword = refine<string>((value) => {
|
|
|
1460
1426
|
// Cross-field: error lands on the specified target
|
|
1461
1427
|
const schema = chain(
|
|
1462
1428
|
object({ password: required(string()), confirm: required(string()) }),
|
|
1463
|
-
refineAt('confirm', (d) => d.password === d.confirm, 'Must match') // target = 'confirm'
|
|
1429
|
+
refineAt('confirm', (d) => d.password === d.confirm, 'Must match'), // target = 'confirm'
|
|
1464
1430
|
);
|
|
1465
1431
|
```
|
|
1466
1432
|
|
|
@@ -1575,10 +1541,7 @@ Railway result type representing success or failure.
|
|
|
1575
1541
|
### Validator
|
|
1576
1542
|
|
|
1577
1543
|
```typescript
|
|
1578
|
-
type Validator<TInput, TOutput> = (
|
|
1579
|
-
value: TInput,
|
|
1580
|
-
path?: string[]
|
|
1581
|
-
) => Result<TOutput, ValidationError[]>;
|
|
1544
|
+
type Validator<TInput, TOutput> = (value: TInput, path?: string[]) => Result<TOutput, ValidationError[]>;
|
|
1582
1545
|
```
|
|
1583
1546
|
|
|
1584
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
|
|