@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/RECIPES.md
CHANGED
|
@@ -91,17 +91,13 @@ function CreateUserForm() {
|
|
|
91
91
|
<div>
|
|
92
92
|
<label htmlFor={form.getFieldId('username')}>Username</label>
|
|
93
93
|
<input {...form.getFieldProps('username')} />
|
|
94
|
-
{form.getFieldError('username') && (
|
|
95
|
-
<span className="error">{form.getFieldError('username')}</span>
|
|
96
|
-
)}
|
|
94
|
+
{form.getFieldError('username') && <span className="error">{form.getFieldError('username')}</span>}
|
|
97
95
|
</div>
|
|
98
96
|
|
|
99
97
|
<div>
|
|
100
98
|
<label htmlFor={form.getFieldId('email')}>Email</label>
|
|
101
99
|
<input type="email" {...form.getFieldProps('email')} />
|
|
102
|
-
{form.getFieldError('email') && (
|
|
103
|
-
<span className="error">{form.getFieldError('email')}</span>
|
|
104
|
-
)}
|
|
100
|
+
{form.getFieldError('email') && <span className="error">{form.getFieldError('email')}</span>}
|
|
105
101
|
</div>
|
|
106
102
|
|
|
107
103
|
<div>
|
|
@@ -110,19 +106,13 @@ function CreateUserForm() {
|
|
|
110
106
|
</div>
|
|
111
107
|
|
|
112
108
|
{/* Form-level errors (not tied to a field) */}
|
|
113
|
-
{form.errors[ROOT_ERROR_KEY] &&
|
|
114
|
-
<div className="form-error">{form.errors[ROOT_ERROR_KEY]}</div>
|
|
115
|
-
)}
|
|
109
|
+
{form.errors[ROOT_ERROR_KEY] && <div className="form-error">{form.errors[ROOT_ERROR_KEY]}</div>}
|
|
116
110
|
|
|
117
111
|
<button type="submit" disabled={form.isSubmitting}>
|
|
118
112
|
{form.isSubmitting ? 'Saving...' : 'Create User'}
|
|
119
113
|
</button>
|
|
120
114
|
|
|
121
|
-
<button
|
|
122
|
-
type="button"
|
|
123
|
-
onClick={() => form.resetForm()}
|
|
124
|
-
disabled={!form.isDirty}
|
|
125
|
-
>
|
|
115
|
+
<button type="button" onClick={() => form.resetForm()} disabled={!form.isDirty}>
|
|
126
116
|
Reset
|
|
127
117
|
</button>
|
|
128
118
|
</form>
|
|
@@ -205,7 +195,7 @@ const todosSchema = object({
|
|
|
205
195
|
text: required(chain(string(), nonEmpty('Task is required'))),
|
|
206
196
|
done: required(boolean()),
|
|
207
197
|
priority: required(stringEnum(['low', 'medium', 'high'])),
|
|
208
|
-
})
|
|
198
|
+
}),
|
|
209
199
|
),
|
|
210
200
|
});
|
|
211
201
|
|
|
@@ -224,9 +214,7 @@ function TodoList() {
|
|
|
224
214
|
<div key={index}>
|
|
225
215
|
<input type="checkbox" {...helpers.getCheckboxProps(index, 'done')} />
|
|
226
216
|
<input {...helpers.getFieldProps(index, 'text')} />
|
|
227
|
-
{helpers.getFieldError(index, 'text') && (
|
|
228
|
-
<span>{helpers.getFieldError(index, 'text')}</span>
|
|
229
|
-
)}
|
|
217
|
+
{helpers.getFieldError(index, 'text') && <span>{helpers.getFieldError(index, 'text')}</span>}
|
|
230
218
|
|
|
231
219
|
<select {...helpers.getSelectFieldProps(index, 'priority')}>
|
|
232
220
|
<option value="low">Low</option>
|
|
@@ -234,17 +222,11 @@ function TodoList() {
|
|
|
234
222
|
<option value="high">High</option>
|
|
235
223
|
</select>
|
|
236
224
|
|
|
237
|
-
<button
|
|
238
|
-
onClick={() => index > 0 && helpers.swap(index, index - 1)}
|
|
239
|
-
disabled={index === 0}
|
|
240
|
-
>
|
|
225
|
+
<button onClick={() => index > 0 && helpers.swap(index, index - 1)} disabled={index === 0}>
|
|
241
226
|
Up
|
|
242
227
|
</button>
|
|
243
228
|
<button
|
|
244
|
-
onClick={() =>
|
|
245
|
-
index < helpers.values.length - 1 &&
|
|
246
|
-
helpers.swap(index, index + 1)
|
|
247
|
-
}
|
|
229
|
+
onClick={() => index < helpers.values.length - 1 && helpers.swap(index, index + 1)}
|
|
248
230
|
disabled={index === helpers.values.length - 1}
|
|
249
231
|
>
|
|
250
232
|
Down
|
|
@@ -253,13 +235,7 @@ function TodoList() {
|
|
|
253
235
|
</div>
|
|
254
236
|
))}
|
|
255
237
|
|
|
256
|
-
<button
|
|
257
|
-
onClick={() =>
|
|
258
|
-
helpers.push({ text: '', done: false, priority: 'medium' })
|
|
259
|
-
}
|
|
260
|
-
>
|
|
261
|
-
Add Todo
|
|
262
|
-
</button>
|
|
238
|
+
<button onClick={() => helpers.push({ text: '', done: false, priority: 'medium' })}>Add Todo</button>
|
|
263
239
|
</div>
|
|
264
240
|
);
|
|
265
241
|
}
|
|
@@ -319,10 +295,7 @@ function PreferencesForm() {
|
|
|
319
295
|
<legend>Notify me via</legend>
|
|
320
296
|
{(['email', 'sms', 'push'] as const).map((method) => (
|
|
321
297
|
<label key={method}>
|
|
322
|
-
<input
|
|
323
|
-
type="checkbox"
|
|
324
|
-
{...form.getCheckboxGroupOptionProps('notifications', method)}
|
|
325
|
-
/>
|
|
298
|
+
<input type="checkbox" {...form.getCheckboxGroupOptionProps('notifications', method)} />
|
|
326
299
|
{method.charAt(0).toUpperCase() + method.slice(1)}
|
|
327
300
|
</label>
|
|
328
301
|
))}
|
|
@@ -332,10 +305,7 @@ function PreferencesForm() {
|
|
|
332
305
|
<legend>Theme</legend>
|
|
333
306
|
{(['light', 'dark', 'auto'] as const).map((theme) => (
|
|
334
307
|
<label key={theme}>
|
|
335
|
-
<input
|
|
336
|
-
type="radio"
|
|
337
|
-
{...form.getRadioGroupOptionProps('theme', theme)}
|
|
338
|
-
/>
|
|
308
|
+
<input type="radio" {...form.getRadioGroupOptionProps('theme', theme)} />
|
|
339
309
|
{theme.charAt(0).toUpperCase() + theme.slice(1)}
|
|
340
310
|
</label>
|
|
341
311
|
))}
|
|
@@ -361,7 +331,7 @@ import { chain, refine } from '@railway-ts/pipelines/schema';
|
|
|
361
331
|
const fileValidator = chain(
|
|
362
332
|
refine<File>((value) => value instanceof File, 'Must be a file'),
|
|
363
333
|
refine<File>((value) => value.size <= 5_000_000, 'File too large (max 5MB)'),
|
|
364
|
-
refine<File>((value) => value.type.startsWith('image/'), 'Must be an image')
|
|
334
|
+
refine<File>((value) => value.type.startsWith('image/'), 'Must be an image'),
|
|
365
335
|
);
|
|
366
336
|
|
|
367
337
|
const uploadSchema = object({
|
|
@@ -382,14 +352,8 @@ function AvatarUpload() {
|
|
|
382
352
|
|
|
383
353
|
return (
|
|
384
354
|
<form onSubmit={(e) => void form.handleSubmit(e)}>
|
|
385
|
-
<input
|
|
386
|
-
|
|
387
|
-
accept="image/*"
|
|
388
|
-
{...form.getFileFieldProps('avatar')}
|
|
389
|
-
/>
|
|
390
|
-
{form.getFieldError('avatar') && (
|
|
391
|
-
<span>{form.getFieldError('avatar')}</span>
|
|
392
|
-
)}
|
|
355
|
+
<input type="file" accept="image/*" {...form.getFileFieldProps('avatar')} />
|
|
356
|
+
{form.getFieldError('avatar') && <span>{form.getFieldError('avatar')}</span>}
|
|
393
357
|
<button type="submit">Upload</button>
|
|
394
358
|
</form>
|
|
395
359
|
);
|
|
@@ -519,10 +483,7 @@ function MultiStepForm() {
|
|
|
519
483
|
<div>
|
|
520
484
|
<h2>Personal Info</h2>
|
|
521
485
|
<input {...form.getFieldProps('personal.name')} placeholder="Name" />
|
|
522
|
-
<input
|
|
523
|
-
{...form.getFieldProps('personal.email')}
|
|
524
|
-
placeholder="Email"
|
|
525
|
-
/>
|
|
486
|
+
<input {...form.getFieldProps('personal.email')} placeholder="Email" />
|
|
526
487
|
<button type="button" onClick={handleNext}>
|
|
527
488
|
Next
|
|
528
489
|
</button>
|
|
@@ -532,10 +493,7 @@ function MultiStepForm() {
|
|
|
532
493
|
{step === 2 && (
|
|
533
494
|
<div>
|
|
534
495
|
<h2>Address</h2>
|
|
535
|
-
<input
|
|
536
|
-
{...form.getFieldProps('address.street')}
|
|
537
|
-
placeholder="Street"
|
|
538
|
-
/>
|
|
496
|
+
<input {...form.getFieldProps('address.street')} placeholder="Street" />
|
|
539
497
|
<input {...form.getFieldProps('address.city')} placeholder="City" />
|
|
540
498
|
<button type="button" onClick={() => setStep(1)}>
|
|
541
499
|
Back
|
|
@@ -549,10 +507,7 @@ function MultiStepForm() {
|
|
|
549
507
|
{step === 3 && (
|
|
550
508
|
<div>
|
|
551
509
|
<h2>Payment</h2>
|
|
552
|
-
<input
|
|
553
|
-
{...form.getFieldProps('payment.cardNumber')}
|
|
554
|
-
placeholder="Card Number"
|
|
555
|
-
/>
|
|
510
|
+
<input {...form.getFieldProps('payment.cardNumber')} placeholder="Card Number" />
|
|
556
511
|
<button type="button" onClick={() => setStep(2)}>
|
|
557
512
|
Back
|
|
558
513
|
</button>
|
|
@@ -590,19 +545,15 @@ const registrationSchema = chain(
|
|
|
590
545
|
startDate: required(string()),
|
|
591
546
|
endDate: required(string()),
|
|
592
547
|
}),
|
|
593
|
-
refineAt(
|
|
594
|
-
'confirmPassword',
|
|
595
|
-
(data) => data.password === data.confirmPassword,
|
|
596
|
-
'Passwords must match'
|
|
597
|
-
),
|
|
548
|
+
refineAt('confirmPassword', (data) => data.password === data.confirmPassword, 'Passwords must match'),
|
|
598
549
|
refineAt(
|
|
599
550
|
'endDate',
|
|
600
551
|
(data) => {
|
|
601
552
|
if (!data.startDate || !data.endDate) return true;
|
|
602
553
|
return new Date(data.endDate) > new Date(data.startDate);
|
|
603
554
|
},
|
|
604
|
-
'End date must be after start date'
|
|
605
|
-
)
|
|
555
|
+
'End date must be after start date',
|
|
556
|
+
),
|
|
606
557
|
);
|
|
607
558
|
|
|
608
559
|
type RegistrationData = InferSchemaType<typeof registrationSchema>;
|
|
@@ -667,18 +618,11 @@ function AccountForm() {
|
|
|
667
618
|
|
|
668
619
|
{isBiz && (
|
|
669
620
|
<>
|
|
670
|
-
<input
|
|
671
|
-
|
|
672
|
-
placeholder="Company Name"
|
|
673
|
-
/>
|
|
674
|
-
{form.getFieldError('companyName') && (
|
|
675
|
-
<span>{form.getFieldError('companyName')}</span>
|
|
676
|
-
)}
|
|
621
|
+
<input {...form.getFieldProps('companyName')} placeholder="Company Name" />
|
|
622
|
+
{form.getFieldError('companyName') && <span>{form.getFieldError('companyName')}</span>}
|
|
677
623
|
|
|
678
624
|
<input {...form.getFieldProps('taxId')} placeholder="Tax ID" />
|
|
679
|
-
{form.getFieldError('taxId') && (
|
|
680
|
-
<span>{form.getFieldError('taxId')}</span>
|
|
681
|
-
)}
|
|
625
|
+
{form.getFieldError('taxId') && <span>{form.getFieldError('taxId')}</span>}
|
|
682
626
|
</>
|
|
683
627
|
)}
|
|
684
628
|
|
|
@@ -754,29 +698,18 @@ function PaymentForm() {
|
|
|
754
698
|
|
|
755
699
|
{method === 'card' && (
|
|
756
700
|
<>
|
|
757
|
-
<input
|
|
758
|
-
{...form.getFieldProps('cardNumber')}
|
|
759
|
-
placeholder="Card Number"
|
|
760
|
-
/>
|
|
701
|
+
<input {...form.getFieldProps('cardNumber')} placeholder="Card Number" />
|
|
761
702
|
<input {...form.getFieldProps('expiryDate')} placeholder="MM/YY" />
|
|
762
703
|
<input {...form.getFieldProps('cvv')} placeholder="CVV" />
|
|
763
704
|
</>
|
|
764
705
|
)}
|
|
765
706
|
|
|
766
|
-
{method === 'paypal' && (
|
|
767
|
-
<input {...form.getFieldProps('email')} placeholder="PayPal Email" />
|
|
768
|
-
)}
|
|
707
|
+
{method === 'paypal' && <input {...form.getFieldProps('email')} placeholder="PayPal Email" />}
|
|
769
708
|
|
|
770
709
|
{method === 'bank' && (
|
|
771
710
|
<>
|
|
772
|
-
<input
|
|
773
|
-
|
|
774
|
-
placeholder="Account Number"
|
|
775
|
-
/>
|
|
776
|
-
<input
|
|
777
|
-
{...form.getFieldProps('routingNumber')}
|
|
778
|
-
placeholder="Routing Number"
|
|
779
|
-
/>
|
|
711
|
+
<input {...form.getFieldProps('accountNumber')} placeholder="Account Number" />
|
|
712
|
+
<input {...form.getFieldProps('routingNumber')} placeholder="Routing Number" />
|
|
780
713
|
</>
|
|
781
714
|
)}
|
|
782
715
|
|
|
@@ -1030,9 +963,7 @@ function ZodForm() {
|
|
|
1030
963
|
return (
|
|
1031
964
|
<form onSubmit={(e) => void form.handleSubmit(e)}>
|
|
1032
965
|
<input {...form.getFieldProps('username')} />
|
|
1033
|
-
{form.getFieldError('username') && (
|
|
1034
|
-
<span>{form.getFieldError('username')}</span>
|
|
1035
|
-
)}
|
|
966
|
+
{form.getFieldError('username') && <span>{form.getFieldError('username')}</span>}
|
|
1036
967
|
{/* ... other fields ... */}
|
|
1037
968
|
<button type="submit">Register</button>
|
|
1038
969
|
</form>
|
|
@@ -1047,21 +978,10 @@ import * as v from 'valibot';
|
|
|
1047
978
|
import { useForm } from '@railway-ts/use-form';
|
|
1048
979
|
|
|
1049
980
|
const userSchema = v.object({
|
|
1050
|
-
username: v.pipe(
|
|
1051
|
-
v.string(),
|
|
1052
|
-
v.minLength(3, 'Username must be at least 3 characters')
|
|
1053
|
-
),
|
|
981
|
+
username: v.pipe(v.string(), v.minLength(3, 'Username must be at least 3 characters')),
|
|
1054
982
|
email: v.pipe(v.string(), v.email('Invalid email address')),
|
|
1055
|
-
password: v.pipe(
|
|
1056
|
-
|
|
1057
|
-
v.minLength(8, 'Password must be at least 8 characters')
|
|
1058
|
-
),
|
|
1059
|
-
age: v.pipe(
|
|
1060
|
-
v.unknown(),
|
|
1061
|
-
v.transform(Number),
|
|
1062
|
-
v.number(),
|
|
1063
|
-
v.minValue(18, 'Must be at least 18')
|
|
1064
|
-
),
|
|
983
|
+
password: v.pipe(v.string(), v.minLength(8, 'Password must be at least 8 characters')),
|
|
984
|
+
age: v.pipe(v.unknown(), v.transform(Number), v.number(), v.minValue(18, 'Must be at least 18')),
|
|
1065
985
|
role: v.picklist(['admin', 'user']),
|
|
1066
986
|
});
|
|
1067
987
|
|
|
@@ -1131,12 +1051,8 @@ function RegistrationForm() {
|
|
|
1131
1051
|
<form onSubmit={(e) => void form.handleSubmit(e)}>
|
|
1132
1052
|
<div>
|
|
1133
1053
|
<input {...form.getFieldProps('username')} />
|
|
1134
|
-
{form.validatingFields.username &&
|
|
1135
|
-
|
|
1136
|
-
)}
|
|
1137
|
-
{form.getFieldError('username') && (
|
|
1138
|
-
<span className="error">{form.getFieldError('username')}</span>
|
|
1139
|
-
)}
|
|
1054
|
+
{form.validatingFields.username && <span className="validating">Checking username...</span>}
|
|
1055
|
+
{form.getFieldError('username') && <span className="error">{form.getFieldError('username')}</span>}
|
|
1140
1056
|
</div>
|
|
1141
1057
|
|
|
1142
1058
|
{/* ... other fields ... */}
|
|
@@ -1237,20 +1153,8 @@ match(result, {
|
|
|
1237
1153
|
**Solution:** Extract a reusable `Field` component that takes the form instance, field name, and label.
|
|
1238
1154
|
|
|
1239
1155
|
```tsx
|
|
1240
|
-
import {
|
|
1241
|
-
|
|
1242
|
-
type UseFormReturn,
|
|
1243
|
-
type ExtractFieldPaths,
|
|
1244
|
-
} from '@railway-ts/use-form';
|
|
1245
|
-
import {
|
|
1246
|
-
object,
|
|
1247
|
-
required,
|
|
1248
|
-
chain,
|
|
1249
|
-
string,
|
|
1250
|
-
email,
|
|
1251
|
-
minLength,
|
|
1252
|
-
type InferSchemaType,
|
|
1253
|
-
} from '@railway-ts/pipelines/schema';
|
|
1156
|
+
import { useForm, type UseFormReturn, type ExtractFieldPaths } from '@railway-ts/use-form';
|
|
1157
|
+
import { object, required, chain, string, email, minLength, type InferSchemaType } from '@railway-ts/pipelines/schema';
|
|
1254
1158
|
|
|
1255
1159
|
const loginSchema = object({
|
|
1256
1160
|
email: required(chain(string(), email())),
|
|
@@ -1308,17 +1212,8 @@ Wrap field components in `React.memo` so they only re-render when their specific
|
|
|
1308
1212
|
|
|
1309
1213
|
```tsx
|
|
1310
1214
|
import { memo, useMemo } from 'react';
|
|
1311
|
-
import {
|
|
1312
|
-
|
|
1313
|
-
type UseFormReturn,
|
|
1314
|
-
type ExtractFieldPaths,
|
|
1315
|
-
} from '@railway-ts/use-form';
|
|
1316
|
-
import {
|
|
1317
|
-
object,
|
|
1318
|
-
required,
|
|
1319
|
-
string,
|
|
1320
|
-
type InferSchemaType,
|
|
1321
|
-
} from '@railway-ts/pipelines/schema';
|
|
1215
|
+
import { useForm, type UseFormReturn, type ExtractFieldPaths } from '@railway-ts/use-form';
|
|
1216
|
+
import { object, required, string, type InferSchemaType } from '@railway-ts/pipelines/schema';
|
|
1322
1217
|
|
|
1323
1218
|
const largeFormSchema = object({
|
|
1324
1219
|
field1: required(string()),
|
|
@@ -1407,15 +1302,7 @@ UI libraries like Material-UI and Chakra UI use different prop names than native
|
|
|
1407
1302
|
```tsx
|
|
1408
1303
|
import { TextField, Button } from '@mui/material';
|
|
1409
1304
|
import { useForm } from '@railway-ts/use-form';
|
|
1410
|
-
import {
|
|
1411
|
-
object,
|
|
1412
|
-
required,
|
|
1413
|
-
chain,
|
|
1414
|
-
string,
|
|
1415
|
-
email,
|
|
1416
|
-
minLength,
|
|
1417
|
-
type InferSchemaType,
|
|
1418
|
-
} from '@railway-ts/pipelines/schema';
|
|
1305
|
+
import { object, required, chain, string, email, minLength, type InferSchemaType } from '@railway-ts/pipelines/schema';
|
|
1419
1306
|
|
|
1420
1307
|
const loginSchema = object({
|
|
1421
1308
|
email: required(chain(string(), email())),
|
|
@@ -1456,11 +1343,7 @@ function MuiForm() {
|
|
|
1456
1343
|
margin="normal"
|
|
1457
1344
|
/>
|
|
1458
1345
|
|
|
1459
|
-
<Button
|
|
1460
|
-
type="submit"
|
|
1461
|
-
variant="contained"
|
|
1462
|
-
disabled={form.isSubmitting || !form.isValid}
|
|
1463
|
-
>
|
|
1346
|
+
<Button type="submit" variant="contained" disabled={form.isSubmitting || !form.isValid}>
|
|
1464
1347
|
Submit
|
|
1465
1348
|
</Button>
|
|
1466
1349
|
</form>
|
|
@@ -1473,15 +1356,7 @@ function MuiForm() {
|
|
|
1473
1356
|
```tsx
|
|
1474
1357
|
import { Field, Input, Button } from '@chakra-ui/react';
|
|
1475
1358
|
import { useForm } from '@railway-ts/use-form';
|
|
1476
|
-
import {
|
|
1477
|
-
object,
|
|
1478
|
-
required,
|
|
1479
|
-
chain,
|
|
1480
|
-
string,
|
|
1481
|
-
email,
|
|
1482
|
-
minLength,
|
|
1483
|
-
type InferSchemaType,
|
|
1484
|
-
} from '@railway-ts/pipelines/schema';
|
|
1359
|
+
import { object, required, chain, string, email, minLength, type InferSchemaType } from '@railway-ts/pipelines/schema';
|
|
1485
1360
|
|
|
1486
1361
|
const loginSchema = object({
|
|
1487
1362
|
email: required(chain(string(), email())),
|
|
@@ -1508,11 +1383,7 @@ function ChakraForm() {
|
|
|
1508
1383
|
<Field.ErrorText>{form.errors.email}</Field.ErrorText>
|
|
1509
1384
|
</Field.Root>
|
|
1510
1385
|
|
|
1511
|
-
<Button
|
|
1512
|
-
type="submit"
|
|
1513
|
-
loading={form.isSubmitting}
|
|
1514
|
-
disabled={!form.isValid}
|
|
1515
|
-
>
|
|
1386
|
+
<Button type="submit" loading={form.isSubmitting} disabled={!form.isValid}>
|
|
1516
1387
|
Submit
|
|
1517
1388
|
</Button>
|
|
1518
1389
|
</form>
|
|
@@ -1529,15 +1400,7 @@ import { Input } from '@/components/ui/input';
|
|
|
1529
1400
|
import { Label } from '@/components/ui/label';
|
|
1530
1401
|
import { Button } from '@/components/ui/button';
|
|
1531
1402
|
import { useForm } from '@railway-ts/use-form';
|
|
1532
|
-
import {
|
|
1533
|
-
object,
|
|
1534
|
-
required,
|
|
1535
|
-
chain,
|
|
1536
|
-
string,
|
|
1537
|
-
email,
|
|
1538
|
-
minLength,
|
|
1539
|
-
type InferSchemaType,
|
|
1540
|
-
} from '@railway-ts/pipelines/schema';
|
|
1403
|
+
import { object, required, chain, string, email, minLength, type InferSchemaType } from '@railway-ts/pipelines/schema';
|
|
1541
1404
|
|
|
1542
1405
|
const loginSchema = object({
|
|
1543
1406
|
email: required(chain(string(), email())),
|
|
@@ -1556,21 +1419,13 @@ function ShadcnForm() {
|
|
|
1556
1419
|
<div>
|
|
1557
1420
|
<Label htmlFor={form.getFieldId('email')}>Email</Label>
|
|
1558
1421
|
<Input {...form.getFieldProps('email')} />
|
|
1559
|
-
{form.getFieldError('email') && (
|
|
1560
|
-
<p className="text-sm text-destructive">
|
|
1561
|
-
{form.getFieldError('email')}
|
|
1562
|
-
</p>
|
|
1563
|
-
)}
|
|
1422
|
+
{form.getFieldError('email') && <p className="text-sm text-destructive">{form.getFieldError('email')}</p>}
|
|
1564
1423
|
</div>
|
|
1565
1424
|
|
|
1566
1425
|
<div>
|
|
1567
1426
|
<Label htmlFor={form.getFieldId('password')}>Password</Label>
|
|
1568
1427
|
<Input type="password" {...form.getFieldProps('password')} />
|
|
1569
|
-
{form.getFieldError('password') && (
|
|
1570
|
-
<p className="text-sm text-destructive">
|
|
1571
|
-
{form.getFieldError('password')}
|
|
1572
|
-
</p>
|
|
1573
|
-
)}
|
|
1428
|
+
{form.getFieldError('password') && <p className="text-sm text-destructive">{form.getFieldError('password')}</p>}
|
|
1574
1429
|
</div>
|
|
1575
1430
|
|
|
1576
1431
|
<Button type="submit" disabled={form.isSubmitting || !form.isValid}>
|
|
@@ -1734,13 +1589,7 @@ This recipe combines everything: schema validation, per-field async validators,
|
|
|
1734
1589
|
|
|
1735
1590
|
```tsx
|
|
1736
1591
|
import { useForm } from '@railway-ts/use-form';
|
|
1737
|
-
import {
|
|
1738
|
-
ok,
|
|
1739
|
-
err,
|
|
1740
|
-
match,
|
|
1741
|
-
fromPromise,
|
|
1742
|
-
type Result,
|
|
1743
|
-
} from '@railway-ts/pipelines/result';
|
|
1592
|
+
import { ok, err, match, fromPromise, type Result } from '@railway-ts/pipelines/result';
|
|
1744
1593
|
import {
|
|
1745
1594
|
object,
|
|
1746
1595
|
string,
|
|
@@ -1763,11 +1612,7 @@ const schema = chain(
|
|
|
1763
1612
|
password: required(chain(string(), nonEmpty(), minLength(8))),
|
|
1764
1613
|
confirmPassword: required(chain(string(), nonEmpty())),
|
|
1765
1614
|
}),
|
|
1766
|
-
refineAt(
|
|
1767
|
-
'confirmPassword',
|
|
1768
|
-
(d) => d.password === d.confirmPassword,
|
|
1769
|
-
'Passwords must match'
|
|
1770
|
-
)
|
|
1615
|
+
refineAt('confirmPassword', (d) => d.password === d.confirmPassword, 'Passwords must match'),
|
|
1771
1616
|
);
|
|
1772
1617
|
|
|
1773
1618
|
type Registration = InferSchemaType<typeof schema>;
|
|
@@ -1776,24 +1621,21 @@ type Registration = InferSchemaType<typeof schema>;
|
|
|
1776
1621
|
|
|
1777
1622
|
const checkUsername = (username: string) =>
|
|
1778
1623
|
fromPromise<{ available: boolean }>(
|
|
1779
|
-
fetch(`/api/check-username?u=${encodeURIComponent(username)}`).then(
|
|
1780
|
-
|
|
1781
|
-
)
|
|
1624
|
+
fetch(`/api/check-username?u=${encodeURIComponent(username)}`).then((res) =>
|
|
1625
|
+
res.ok ? res.json() : Promise.reject(`HTTP ${res.status}`),
|
|
1626
|
+
),
|
|
1782
1627
|
);
|
|
1783
1628
|
|
|
1784
|
-
const registerUser = async (
|
|
1785
|
-
values: Registration
|
|
1786
|
-
): Promise<Result<void, Record<string, string>>> => {
|
|
1629
|
+
const registerUser = async (values: Registration): Promise<Result<void, Record<string, string>>> => {
|
|
1787
1630
|
const result = await fromPromise(
|
|
1788
1631
|
fetch('/api/register', {
|
|
1789
1632
|
method: 'POST',
|
|
1790
1633
|
headers: { 'Content-Type': 'application/json' },
|
|
1791
1634
|
body: JSON.stringify(values),
|
|
1792
|
-
})
|
|
1635
|
+
}),
|
|
1793
1636
|
);
|
|
1794
1637
|
|
|
1795
|
-
if (!result.ok)
|
|
1796
|
-
return err({ [ROOT_ERROR_KEY]: 'Network error. Please try again.' });
|
|
1638
|
+
if (!result.ok) return err({ [ROOT_ERROR_KEY]: 'Network error. Please try again.' });
|
|
1797
1639
|
|
|
1798
1640
|
const res = result.value;
|
|
1799
1641
|
if (!res.ok) return err((await res.json()) as Record<string, string>);
|
|
@@ -1815,8 +1657,7 @@ export function RegistrationForm() {
|
|
|
1815
1657
|
username: async (value) => {
|
|
1816
1658
|
const result = await checkUsername(value);
|
|
1817
1659
|
return match(result, {
|
|
1818
|
-
ok: ({ available }) =>
|
|
1819
|
-
available ? undefined : 'Username is already taken',
|
|
1660
|
+
ok: ({ available }) => (available ? undefined : 'Username is already taken'),
|
|
1820
1661
|
err: () => 'Unable to check username availability',
|
|
1821
1662
|
});
|
|
1822
1663
|
},
|
|
@@ -1836,28 +1677,18 @@ export function RegistrationForm() {
|
|
|
1836
1677
|
<form onSubmit={(e) => void form.handleSubmit(e)}>
|
|
1837
1678
|
<input {...form.getFieldProps('username')} />
|
|
1838
1679
|
{form.validatingFields.username && <span>Checking...</span>}
|
|
1839
|
-
{form.getFieldError('username') && (
|
|
1840
|
-
<span>{form.getFieldError('username')}</span>
|
|
1841
|
-
)}
|
|
1680
|
+
{form.getFieldError('username') && <span>{form.getFieldError('username')}</span>}
|
|
1842
1681
|
|
|
1843
1682
|
<input type="email" {...form.getFieldProps('email')} />
|
|
1844
|
-
{form.getFieldError('email') && (
|
|
1845
|
-
<span>{form.getFieldError('email')}</span>
|
|
1846
|
-
)}
|
|
1683
|
+
{form.getFieldError('email') && <span>{form.getFieldError('email')}</span>}
|
|
1847
1684
|
|
|
1848
1685
|
<input type="password" {...form.getFieldProps('password')} />
|
|
1849
|
-
{form.getFieldError('password') && (
|
|
1850
|
-
<span>{form.getFieldError('password')}</span>
|
|
1851
|
-
)}
|
|
1686
|
+
{form.getFieldError('password') && <span>{form.getFieldError('password')}</span>}
|
|
1852
1687
|
|
|
1853
1688
|
<input type="password" {...form.getFieldProps('confirmPassword')} />
|
|
1854
|
-
{form.getFieldError('confirmPassword') && (
|
|
1855
|
-
<span>{form.getFieldError('confirmPassword')}</span>
|
|
1856
|
-
)}
|
|
1689
|
+
{form.getFieldError('confirmPassword') && <span>{form.getFieldError('confirmPassword')}</span>}
|
|
1857
1690
|
|
|
1858
|
-
{form.errors[ROOT_ERROR_KEY] &&
|
|
1859
|
-
<span>{form.errors[ROOT_ERROR_KEY]}</span>
|
|
1860
|
-
)}
|
|
1691
|
+
{form.errors[ROOT_ERROR_KEY] && <span>{form.errors[ROOT_ERROR_KEY]}</span>}
|
|
1861
1692
|
|
|
1862
1693
|
<button type="submit" disabled={form.isSubmitting || form.isValidating}>
|
|
1863
1694
|
{form.isSubmitting ? 'Registering...' : 'Create Account'}
|
|
@@ -1908,11 +1739,7 @@ const schema = chain(
|
|
|
1908
1739
|
password: required(chain(string(), nonEmpty(), minLength(8))),
|
|
1909
1740
|
confirmPassword: required(chain(string(), nonEmpty())),
|
|
1910
1741
|
}),
|
|
1911
|
-
refineAt(
|
|
1912
|
-
'confirmPassword',
|
|
1913
|
-
(d) => d.password === d.confirmPassword,
|
|
1914
|
-
'Passwords must match'
|
|
1915
|
-
)
|
|
1742
|
+
refineAt('confirmPassword', (d) => d.password === d.confirmPassword, 'Passwords must match'),
|
|
1916
1743
|
);
|
|
1917
1744
|
|
|
1918
1745
|
type Registration = InferSchemaType<typeof schema>;
|
|
@@ -1921,7 +1748,7 @@ type Registration = InferSchemaType<typeof schema>;
|
|
|
1921
1748
|
|
|
1922
1749
|
const checkUsername = (username: string): Promise<{ available: boolean }> =>
|
|
1923
1750
|
fetch(`/api/check-username?u=${encodeURIComponent(username)}`).then((res) =>
|
|
1924
|
-
res.ok ? res.json() : Promise.reject(`HTTP ${res.status}`)
|
|
1751
|
+
res.ok ? res.json() : Promise.reject(`HTTP ${res.status}`),
|
|
1925
1752
|
);
|
|
1926
1753
|
|
|
1927
1754
|
class ApiValidationError extends Error {
|
|
@@ -1988,28 +1815,18 @@ export function RegistrationForm() {
|
|
|
1988
1815
|
<form onSubmit={(e) => void form.handleSubmit(e)}>
|
|
1989
1816
|
<input {...form.getFieldProps('username')} />
|
|
1990
1817
|
{form.validatingFields.username && <span>Checking...</span>}
|
|
1991
|
-
{form.getFieldError('username') && (
|
|
1992
|
-
<span>{form.getFieldError('username')}</span>
|
|
1993
|
-
)}
|
|
1818
|
+
{form.getFieldError('username') && <span>{form.getFieldError('username')}</span>}
|
|
1994
1819
|
|
|
1995
1820
|
<input type="email" {...form.getFieldProps('email')} />
|
|
1996
|
-
{form.getFieldError('email') && (
|
|
1997
|
-
<span>{form.getFieldError('email')}</span>
|
|
1998
|
-
)}
|
|
1821
|
+
{form.getFieldError('email') && <span>{form.getFieldError('email')}</span>}
|
|
1999
1822
|
|
|
2000
1823
|
<input type="password" {...form.getFieldProps('password')} />
|
|
2001
|
-
{form.getFieldError('password') && (
|
|
2002
|
-
<span>{form.getFieldError('password')}</span>
|
|
2003
|
-
)}
|
|
1824
|
+
{form.getFieldError('password') && <span>{form.getFieldError('password')}</span>}
|
|
2004
1825
|
|
|
2005
1826
|
<input type="password" {...form.getFieldProps('confirmPassword')} />
|
|
2006
|
-
{form.getFieldError('confirmPassword') && (
|
|
2007
|
-
<span>{form.getFieldError('confirmPassword')}</span>
|
|
2008
|
-
)}
|
|
1827
|
+
{form.getFieldError('confirmPassword') && <span>{form.getFieldError('confirmPassword')}</span>}
|
|
2009
1828
|
|
|
2010
|
-
{form.errors[ROOT_ERROR_KEY] &&
|
|
2011
|
-
<span>{form.errors[ROOT_ERROR_KEY]}</span>
|
|
2012
|
-
)}
|
|
1829
|
+
{form.errors[ROOT_ERROR_KEY] && <span>{form.errors[ROOT_ERROR_KEY]}</span>}
|
|
2013
1830
|
|
|
2014
1831
|
<button type="submit" disabled={mutation.isPending || form.isValidating}>
|
|
2015
1832
|
{mutation.isPending ? 'Registering...' : 'Create Account'}
|
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.28",
|
|
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",
|
|
@@ -71,32 +71,32 @@
|
|
|
71
71
|
}
|
|
72
72
|
],
|
|
73
73
|
"peerDependencies": {
|
|
74
|
-
"@railway-ts/pipelines": "^0.1.
|
|
74
|
+
"@railway-ts/pipelines": "^0.1.27",
|
|
75
75
|
"react": "^18.0.0 || ^19.0.0",
|
|
76
76
|
"typescript": "^5"
|
|
77
77
|
},
|
|
78
78
|
"devDependencies": {
|
|
79
|
-
"@happy-dom/global-registrator": "^20.0
|
|
80
|
-
"@railway-ts/pipelines": "^0.1.
|
|
79
|
+
"@happy-dom/global-registrator": "^20.7.0",
|
|
80
|
+
"@railway-ts/pipelines": "^0.1.27",
|
|
81
81
|
"@testing-library/dom": "^10.4.1",
|
|
82
82
|
"@testing-library/jest-dom": "^6.9.1",
|
|
83
|
-
"@testing-library/react": "^16.3.
|
|
83
|
+
"@testing-library/react": "^16.3.2",
|
|
84
84
|
"@testing-library/user-event": "^14.6.1",
|
|
85
|
-
"@types/bun": "^1.3.
|
|
86
|
-
"@types/react": "^19.2.
|
|
87
|
-
"@types/react-dom": "^19.2.
|
|
88
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
89
|
-
"@typescript-eslint/parser": "^8.
|
|
90
|
-
"eslint": "^
|
|
85
|
+
"@types/bun": "^1.3.9",
|
|
86
|
+
"@types/react": "^19.2.14",
|
|
87
|
+
"@types/react-dom": "^19.2.3",
|
|
88
|
+
"@typescript-eslint/eslint-plugin": "^8.56.1",
|
|
89
|
+
"@typescript-eslint/parser": "^8.56.1",
|
|
90
|
+
"eslint": "^10.0.2",
|
|
91
91
|
"eslint-config-prettier": "^10.1.8",
|
|
92
|
-
"eslint-plugin
|
|
92
|
+
"@eslint-react/eslint-plugin": "^2.13.0",
|
|
93
93
|
"eslint-plugin-react-hooks": "^7.0.1",
|
|
94
|
-
"prettier": "^3.
|
|
95
|
-
"react": "^19.2.
|
|
96
|
-
"react-dom": "^19.2.
|
|
94
|
+
"prettier": "^3.8.1",
|
|
95
|
+
"react": "^19.2.4",
|
|
96
|
+
"react-dom": "^19.2.4",
|
|
97
97
|
"@size-limit/preset-small-lib": "^12.0.0",
|
|
98
98
|
"size-limit": "^12.0.0",
|
|
99
|
-
"tsup": "^8.5.
|
|
99
|
+
"tsup": "^8.5.1",
|
|
100
100
|
"typescript": "^5.0.0",
|
|
101
101
|
"valibot": "^1.2.0",
|
|
102
102
|
"zod": "^4.3.6"
|