@railway-ts/use-form 0.1.27 → 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 +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 +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>;
|
|
@@ -645,18 +596,10 @@ const accountSchema = chain(
|
|
|
645
596
|
when<AccountData>(
|
|
646
597
|
(d) => d.accountType === 'business',
|
|
647
598
|
chain(
|
|
648
|
-
refineAt(
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
),
|
|
653
|
-
refineAt(
|
|
654
|
-
'taxId',
|
|
655
|
-
(d) => !!d.taxId,
|
|
656
|
-
'Tax ID is required for business accounts'
|
|
657
|
-
)
|
|
658
|
-
)
|
|
659
|
-
)
|
|
599
|
+
refineAt('companyName', (d) => !!d.companyName, 'Company name is required for business accounts'),
|
|
600
|
+
refineAt('taxId', (d) => !!d.taxId, 'Tax ID is required for business accounts'),
|
|
601
|
+
),
|
|
602
|
+
),
|
|
660
603
|
);
|
|
661
604
|
|
|
662
605
|
function AccountForm() {
|
|
@@ -675,18 +618,11 @@ function AccountForm() {
|
|
|
675
618
|
|
|
676
619
|
{isBiz && (
|
|
677
620
|
<>
|
|
678
|
-
<input
|
|
679
|
-
|
|
680
|
-
placeholder="Company Name"
|
|
681
|
-
/>
|
|
682
|
-
{form.getFieldError('companyName') && (
|
|
683
|
-
<span>{form.getFieldError('companyName')}</span>
|
|
684
|
-
)}
|
|
621
|
+
<input {...form.getFieldProps('companyName')} placeholder="Company Name" />
|
|
622
|
+
{form.getFieldError('companyName') && <span>{form.getFieldError('companyName')}</span>}
|
|
685
623
|
|
|
686
624
|
<input {...form.getFieldProps('taxId')} placeholder="Tax ID" />
|
|
687
|
-
{form.getFieldError('taxId') && (
|
|
688
|
-
<span>{form.getFieldError('taxId')}</span>
|
|
689
|
-
)}
|
|
625
|
+
{form.getFieldError('taxId') && <span>{form.getFieldError('taxId')}</span>}
|
|
690
626
|
</>
|
|
691
627
|
)}
|
|
692
628
|
|
|
@@ -699,22 +635,14 @@ function AccountForm() {
|
|
|
699
635
|
Each `refineAt` inside `when` only checks its own field — the `when` wrapper already gates execution to business accounts. For async conditional checks, use `whenAsync` with `chainAsync`:
|
|
700
636
|
|
|
701
637
|
```typescript
|
|
702
|
-
import {
|
|
703
|
-
chainAsync,
|
|
704
|
-
whenAsync,
|
|
705
|
-
refineAtAsync,
|
|
706
|
-
} from '@railway-ts/pipelines/schema';
|
|
638
|
+
import { chainAsync, whenAsync, refineAtAsync } from '@railway-ts/pipelines/schema';
|
|
707
639
|
|
|
708
640
|
const staffSchema = chainAsync(
|
|
709
641
|
baseStaffSchema,
|
|
710
642
|
whenAsync<StaffData>(
|
|
711
643
|
(d) => d.role === 'doctor',
|
|
712
|
-
refineAtAsync(
|
|
713
|
-
|
|
714
|
-
async (d) => await verifyMedicalLicense(d.license),
|
|
715
|
-
'Invalid medical license'
|
|
716
|
-
)
|
|
717
|
-
)
|
|
644
|
+
refineAtAsync('license', async (d) => await verifyMedicalLicense(d.license), 'Invalid medical license'),
|
|
645
|
+
),
|
|
718
646
|
);
|
|
719
647
|
```
|
|
720
648
|
|
|
@@ -770,29 +698,18 @@ function PaymentForm() {
|
|
|
770
698
|
|
|
771
699
|
{method === 'card' && (
|
|
772
700
|
<>
|
|
773
|
-
<input
|
|
774
|
-
{...form.getFieldProps('cardNumber')}
|
|
775
|
-
placeholder="Card Number"
|
|
776
|
-
/>
|
|
701
|
+
<input {...form.getFieldProps('cardNumber')} placeholder="Card Number" />
|
|
777
702
|
<input {...form.getFieldProps('expiryDate')} placeholder="MM/YY" />
|
|
778
703
|
<input {...form.getFieldProps('cvv')} placeholder="CVV" />
|
|
779
704
|
</>
|
|
780
705
|
)}
|
|
781
706
|
|
|
782
|
-
{method === 'paypal' && (
|
|
783
|
-
<input {...form.getFieldProps('email')} placeholder="PayPal Email" />
|
|
784
|
-
)}
|
|
707
|
+
{method === 'paypal' && <input {...form.getFieldProps('email')} placeholder="PayPal Email" />}
|
|
785
708
|
|
|
786
709
|
{method === 'bank' && (
|
|
787
710
|
<>
|
|
788
|
-
<input
|
|
789
|
-
|
|
790
|
-
placeholder="Account Number"
|
|
791
|
-
/>
|
|
792
|
-
<input
|
|
793
|
-
{...form.getFieldProps('routingNumber')}
|
|
794
|
-
placeholder="Routing Number"
|
|
795
|
-
/>
|
|
711
|
+
<input {...form.getFieldProps('accountNumber')} placeholder="Account Number" />
|
|
712
|
+
<input {...form.getFieldProps('routingNumber')} placeholder="Routing Number" />
|
|
796
713
|
</>
|
|
797
714
|
)}
|
|
798
715
|
|
|
@@ -1046,9 +963,7 @@ function ZodForm() {
|
|
|
1046
963
|
return (
|
|
1047
964
|
<form onSubmit={(e) => void form.handleSubmit(e)}>
|
|
1048
965
|
<input {...form.getFieldProps('username')} />
|
|
1049
|
-
{form.getFieldError('username') && (
|
|
1050
|
-
<span>{form.getFieldError('username')}</span>
|
|
1051
|
-
)}
|
|
966
|
+
{form.getFieldError('username') && <span>{form.getFieldError('username')}</span>}
|
|
1052
967
|
{/* ... other fields ... */}
|
|
1053
968
|
<button type="submit">Register</button>
|
|
1054
969
|
</form>
|
|
@@ -1063,21 +978,10 @@ import * as v from 'valibot';
|
|
|
1063
978
|
import { useForm } from '@railway-ts/use-form';
|
|
1064
979
|
|
|
1065
980
|
const userSchema = v.object({
|
|
1066
|
-
username: v.pipe(
|
|
1067
|
-
v.string(),
|
|
1068
|
-
v.minLength(3, 'Username must be at least 3 characters')
|
|
1069
|
-
),
|
|
981
|
+
username: v.pipe(v.string(), v.minLength(3, 'Username must be at least 3 characters')),
|
|
1070
982
|
email: v.pipe(v.string(), v.email('Invalid email address')),
|
|
1071
|
-
password: v.pipe(
|
|
1072
|
-
|
|
1073
|
-
v.minLength(8, 'Password must be at least 8 characters')
|
|
1074
|
-
),
|
|
1075
|
-
age: v.pipe(
|
|
1076
|
-
v.unknown(),
|
|
1077
|
-
v.transform(Number),
|
|
1078
|
-
v.number(),
|
|
1079
|
-
v.minValue(18, 'Must be at least 18')
|
|
1080
|
-
),
|
|
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')),
|
|
1081
985
|
role: v.picklist(['admin', 'user']),
|
|
1082
986
|
});
|
|
1083
987
|
|
|
@@ -1147,12 +1051,8 @@ function RegistrationForm() {
|
|
|
1147
1051
|
<form onSubmit={(e) => void form.handleSubmit(e)}>
|
|
1148
1052
|
<div>
|
|
1149
1053
|
<input {...form.getFieldProps('username')} />
|
|
1150
|
-
{form.validatingFields.username &&
|
|
1151
|
-
|
|
1152
|
-
)}
|
|
1153
|
-
{form.getFieldError('username') && (
|
|
1154
|
-
<span className="error">{form.getFieldError('username')}</span>
|
|
1155
|
-
)}
|
|
1054
|
+
{form.validatingFields.username && <span className="validating">Checking username...</span>}
|
|
1055
|
+
{form.getFieldError('username') && <span className="error">{form.getFieldError('username')}</span>}
|
|
1156
1056
|
</div>
|
|
1157
1057
|
|
|
1158
1058
|
{/* ... other fields ... */}
|
|
@@ -1253,20 +1153,8 @@ match(result, {
|
|
|
1253
1153
|
**Solution:** Extract a reusable `Field` component that takes the form instance, field name, and label.
|
|
1254
1154
|
|
|
1255
1155
|
```tsx
|
|
1256
|
-
import {
|
|
1257
|
-
|
|
1258
|
-
type UseFormReturn,
|
|
1259
|
-
type ExtractFieldPaths,
|
|
1260
|
-
} from '@railway-ts/use-form';
|
|
1261
|
-
import {
|
|
1262
|
-
object,
|
|
1263
|
-
required,
|
|
1264
|
-
chain,
|
|
1265
|
-
string,
|
|
1266
|
-
email,
|
|
1267
|
-
minLength,
|
|
1268
|
-
type InferSchemaType,
|
|
1269
|
-
} 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';
|
|
1270
1158
|
|
|
1271
1159
|
const loginSchema = object({
|
|
1272
1160
|
email: required(chain(string(), email())),
|
|
@@ -1324,17 +1212,8 @@ Wrap field components in `React.memo` so they only re-render when their specific
|
|
|
1324
1212
|
|
|
1325
1213
|
```tsx
|
|
1326
1214
|
import { memo, useMemo } from 'react';
|
|
1327
|
-
import {
|
|
1328
|
-
|
|
1329
|
-
type UseFormReturn,
|
|
1330
|
-
type ExtractFieldPaths,
|
|
1331
|
-
} from '@railway-ts/use-form';
|
|
1332
|
-
import {
|
|
1333
|
-
object,
|
|
1334
|
-
required,
|
|
1335
|
-
string,
|
|
1336
|
-
type InferSchemaType,
|
|
1337
|
-
} 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';
|
|
1338
1217
|
|
|
1339
1218
|
const largeFormSchema = object({
|
|
1340
1219
|
field1: required(string()),
|
|
@@ -1423,15 +1302,7 @@ UI libraries like Material-UI and Chakra UI use different prop names than native
|
|
|
1423
1302
|
```tsx
|
|
1424
1303
|
import { TextField, Button } from '@mui/material';
|
|
1425
1304
|
import { useForm } from '@railway-ts/use-form';
|
|
1426
|
-
import {
|
|
1427
|
-
object,
|
|
1428
|
-
required,
|
|
1429
|
-
chain,
|
|
1430
|
-
string,
|
|
1431
|
-
email,
|
|
1432
|
-
minLength,
|
|
1433
|
-
type InferSchemaType,
|
|
1434
|
-
} from '@railway-ts/pipelines/schema';
|
|
1305
|
+
import { object, required, chain, string, email, minLength, type InferSchemaType } from '@railway-ts/pipelines/schema';
|
|
1435
1306
|
|
|
1436
1307
|
const loginSchema = object({
|
|
1437
1308
|
email: required(chain(string(), email())),
|
|
@@ -1472,11 +1343,7 @@ function MuiForm() {
|
|
|
1472
1343
|
margin="normal"
|
|
1473
1344
|
/>
|
|
1474
1345
|
|
|
1475
|
-
<Button
|
|
1476
|
-
type="submit"
|
|
1477
|
-
variant="contained"
|
|
1478
|
-
disabled={form.isSubmitting || !form.isValid}
|
|
1479
|
-
>
|
|
1346
|
+
<Button type="submit" variant="contained" disabled={form.isSubmitting || !form.isValid}>
|
|
1480
1347
|
Submit
|
|
1481
1348
|
</Button>
|
|
1482
1349
|
</form>
|
|
@@ -1489,15 +1356,7 @@ function MuiForm() {
|
|
|
1489
1356
|
```tsx
|
|
1490
1357
|
import { Field, Input, Button } from '@chakra-ui/react';
|
|
1491
1358
|
import { useForm } from '@railway-ts/use-form';
|
|
1492
|
-
import {
|
|
1493
|
-
object,
|
|
1494
|
-
required,
|
|
1495
|
-
chain,
|
|
1496
|
-
string,
|
|
1497
|
-
email,
|
|
1498
|
-
minLength,
|
|
1499
|
-
type InferSchemaType,
|
|
1500
|
-
} from '@railway-ts/pipelines/schema';
|
|
1359
|
+
import { object, required, chain, string, email, minLength, type InferSchemaType } from '@railway-ts/pipelines/schema';
|
|
1501
1360
|
|
|
1502
1361
|
const loginSchema = object({
|
|
1503
1362
|
email: required(chain(string(), email())),
|
|
@@ -1524,11 +1383,7 @@ function ChakraForm() {
|
|
|
1524
1383
|
<Field.ErrorText>{form.errors.email}</Field.ErrorText>
|
|
1525
1384
|
</Field.Root>
|
|
1526
1385
|
|
|
1527
|
-
<Button
|
|
1528
|
-
type="submit"
|
|
1529
|
-
loading={form.isSubmitting}
|
|
1530
|
-
disabled={!form.isValid}
|
|
1531
|
-
>
|
|
1386
|
+
<Button type="submit" loading={form.isSubmitting} disabled={!form.isValid}>
|
|
1532
1387
|
Submit
|
|
1533
1388
|
</Button>
|
|
1534
1389
|
</form>
|
|
@@ -1545,15 +1400,7 @@ import { Input } from '@/components/ui/input';
|
|
|
1545
1400
|
import { Label } from '@/components/ui/label';
|
|
1546
1401
|
import { Button } from '@/components/ui/button';
|
|
1547
1402
|
import { useForm } from '@railway-ts/use-form';
|
|
1548
|
-
import {
|
|
1549
|
-
object,
|
|
1550
|
-
required,
|
|
1551
|
-
chain,
|
|
1552
|
-
string,
|
|
1553
|
-
email,
|
|
1554
|
-
minLength,
|
|
1555
|
-
type InferSchemaType,
|
|
1556
|
-
} from '@railway-ts/pipelines/schema';
|
|
1403
|
+
import { object, required, chain, string, email, minLength, type InferSchemaType } from '@railway-ts/pipelines/schema';
|
|
1557
1404
|
|
|
1558
1405
|
const loginSchema = object({
|
|
1559
1406
|
email: required(chain(string(), email())),
|
|
@@ -1572,21 +1419,13 @@ function ShadcnForm() {
|
|
|
1572
1419
|
<div>
|
|
1573
1420
|
<Label htmlFor={form.getFieldId('email')}>Email</Label>
|
|
1574
1421
|
<Input {...form.getFieldProps('email')} />
|
|
1575
|
-
{form.getFieldError('email') && (
|
|
1576
|
-
<p className="text-sm text-destructive">
|
|
1577
|
-
{form.getFieldError('email')}
|
|
1578
|
-
</p>
|
|
1579
|
-
)}
|
|
1422
|
+
{form.getFieldError('email') && <p className="text-sm text-destructive">{form.getFieldError('email')}</p>}
|
|
1580
1423
|
</div>
|
|
1581
1424
|
|
|
1582
1425
|
<div>
|
|
1583
1426
|
<Label htmlFor={form.getFieldId('password')}>Password</Label>
|
|
1584
1427
|
<Input type="password" {...form.getFieldProps('password')} />
|
|
1585
|
-
{form.getFieldError('password') && (
|
|
1586
|
-
<p className="text-sm text-destructive">
|
|
1587
|
-
{form.getFieldError('password')}
|
|
1588
|
-
</p>
|
|
1589
|
-
)}
|
|
1428
|
+
{form.getFieldError('password') && <p className="text-sm text-destructive">{form.getFieldError('password')}</p>}
|
|
1590
1429
|
</div>
|
|
1591
1430
|
|
|
1592
1431
|
<Button type="submit" disabled={form.isSubmitting || !form.isValid}>
|
|
@@ -1750,13 +1589,7 @@ This recipe combines everything: schema validation, per-field async validators,
|
|
|
1750
1589
|
|
|
1751
1590
|
```tsx
|
|
1752
1591
|
import { useForm } from '@railway-ts/use-form';
|
|
1753
|
-
import {
|
|
1754
|
-
ok,
|
|
1755
|
-
err,
|
|
1756
|
-
match,
|
|
1757
|
-
fromPromise,
|
|
1758
|
-
type Result,
|
|
1759
|
-
} from '@railway-ts/pipelines/result';
|
|
1592
|
+
import { ok, err, match, fromPromise, type Result } from '@railway-ts/pipelines/result';
|
|
1760
1593
|
import {
|
|
1761
1594
|
object,
|
|
1762
1595
|
string,
|
|
@@ -1779,11 +1612,7 @@ const schema = chain(
|
|
|
1779
1612
|
password: required(chain(string(), nonEmpty(), minLength(8))),
|
|
1780
1613
|
confirmPassword: required(chain(string(), nonEmpty())),
|
|
1781
1614
|
}),
|
|
1782
|
-
refineAt(
|
|
1783
|
-
'confirmPassword',
|
|
1784
|
-
(d) => d.password === d.confirmPassword,
|
|
1785
|
-
'Passwords must match'
|
|
1786
|
-
)
|
|
1615
|
+
refineAt('confirmPassword', (d) => d.password === d.confirmPassword, 'Passwords must match'),
|
|
1787
1616
|
);
|
|
1788
1617
|
|
|
1789
1618
|
type Registration = InferSchemaType<typeof schema>;
|
|
@@ -1792,24 +1621,21 @@ type Registration = InferSchemaType<typeof schema>;
|
|
|
1792
1621
|
|
|
1793
1622
|
const checkUsername = (username: string) =>
|
|
1794
1623
|
fromPromise<{ available: boolean }>(
|
|
1795
|
-
fetch(`/api/check-username?u=${encodeURIComponent(username)}`).then(
|
|
1796
|
-
|
|
1797
|
-
)
|
|
1624
|
+
fetch(`/api/check-username?u=${encodeURIComponent(username)}`).then((res) =>
|
|
1625
|
+
res.ok ? res.json() : Promise.reject(`HTTP ${res.status}`),
|
|
1626
|
+
),
|
|
1798
1627
|
);
|
|
1799
1628
|
|
|
1800
|
-
const registerUser = async (
|
|
1801
|
-
values: Registration
|
|
1802
|
-
): Promise<Result<void, Record<string, string>>> => {
|
|
1629
|
+
const registerUser = async (values: Registration): Promise<Result<void, Record<string, string>>> => {
|
|
1803
1630
|
const result = await fromPromise(
|
|
1804
1631
|
fetch('/api/register', {
|
|
1805
1632
|
method: 'POST',
|
|
1806
1633
|
headers: { 'Content-Type': 'application/json' },
|
|
1807
1634
|
body: JSON.stringify(values),
|
|
1808
|
-
})
|
|
1635
|
+
}),
|
|
1809
1636
|
);
|
|
1810
1637
|
|
|
1811
|
-
if (!result.ok)
|
|
1812
|
-
return err({ [ROOT_ERROR_KEY]: 'Network error. Please try again.' });
|
|
1638
|
+
if (!result.ok) return err({ [ROOT_ERROR_KEY]: 'Network error. Please try again.' });
|
|
1813
1639
|
|
|
1814
1640
|
const res = result.value;
|
|
1815
1641
|
if (!res.ok) return err((await res.json()) as Record<string, string>);
|
|
@@ -1831,8 +1657,7 @@ export function RegistrationForm() {
|
|
|
1831
1657
|
username: async (value) => {
|
|
1832
1658
|
const result = await checkUsername(value);
|
|
1833
1659
|
return match(result, {
|
|
1834
|
-
ok: ({ available }) =>
|
|
1835
|
-
available ? undefined : 'Username is already taken',
|
|
1660
|
+
ok: ({ available }) => (available ? undefined : 'Username is already taken'),
|
|
1836
1661
|
err: () => 'Unable to check username availability',
|
|
1837
1662
|
});
|
|
1838
1663
|
},
|
|
@@ -1852,28 +1677,18 @@ export function RegistrationForm() {
|
|
|
1852
1677
|
<form onSubmit={(e) => void form.handleSubmit(e)}>
|
|
1853
1678
|
<input {...form.getFieldProps('username')} />
|
|
1854
1679
|
{form.validatingFields.username && <span>Checking...</span>}
|
|
1855
|
-
{form.getFieldError('username') && (
|
|
1856
|
-
<span>{form.getFieldError('username')}</span>
|
|
1857
|
-
)}
|
|
1680
|
+
{form.getFieldError('username') && <span>{form.getFieldError('username')}</span>}
|
|
1858
1681
|
|
|
1859
1682
|
<input type="email" {...form.getFieldProps('email')} />
|
|
1860
|
-
{form.getFieldError('email') && (
|
|
1861
|
-
<span>{form.getFieldError('email')}</span>
|
|
1862
|
-
)}
|
|
1683
|
+
{form.getFieldError('email') && <span>{form.getFieldError('email')}</span>}
|
|
1863
1684
|
|
|
1864
1685
|
<input type="password" {...form.getFieldProps('password')} />
|
|
1865
|
-
{form.getFieldError('password') && (
|
|
1866
|
-
<span>{form.getFieldError('password')}</span>
|
|
1867
|
-
)}
|
|
1686
|
+
{form.getFieldError('password') && <span>{form.getFieldError('password')}</span>}
|
|
1868
1687
|
|
|
1869
1688
|
<input type="password" {...form.getFieldProps('confirmPassword')} />
|
|
1870
|
-
{form.getFieldError('confirmPassword') && (
|
|
1871
|
-
<span>{form.getFieldError('confirmPassword')}</span>
|
|
1872
|
-
)}
|
|
1689
|
+
{form.getFieldError('confirmPassword') && <span>{form.getFieldError('confirmPassword')}</span>}
|
|
1873
1690
|
|
|
1874
|
-
{form.errors[ROOT_ERROR_KEY] &&
|
|
1875
|
-
<span>{form.errors[ROOT_ERROR_KEY]}</span>
|
|
1876
|
-
)}
|
|
1691
|
+
{form.errors[ROOT_ERROR_KEY] && <span>{form.errors[ROOT_ERROR_KEY]}</span>}
|
|
1877
1692
|
|
|
1878
1693
|
<button type="submit" disabled={form.isSubmitting || form.isValidating}>
|
|
1879
1694
|
{form.isSubmitting ? 'Registering...' : 'Create Account'}
|
|
@@ -1924,11 +1739,7 @@ const schema = chain(
|
|
|
1924
1739
|
password: required(chain(string(), nonEmpty(), minLength(8))),
|
|
1925
1740
|
confirmPassword: required(chain(string(), nonEmpty())),
|
|
1926
1741
|
}),
|
|
1927
|
-
refineAt(
|
|
1928
|
-
'confirmPassword',
|
|
1929
|
-
(d) => d.password === d.confirmPassword,
|
|
1930
|
-
'Passwords must match'
|
|
1931
|
-
)
|
|
1742
|
+
refineAt('confirmPassword', (d) => d.password === d.confirmPassword, 'Passwords must match'),
|
|
1932
1743
|
);
|
|
1933
1744
|
|
|
1934
1745
|
type Registration = InferSchemaType<typeof schema>;
|
|
@@ -1937,7 +1748,7 @@ type Registration = InferSchemaType<typeof schema>;
|
|
|
1937
1748
|
|
|
1938
1749
|
const checkUsername = (username: string): Promise<{ available: boolean }> =>
|
|
1939
1750
|
fetch(`/api/check-username?u=${encodeURIComponent(username)}`).then((res) =>
|
|
1940
|
-
res.ok ? res.json() : Promise.reject(`HTTP ${res.status}`)
|
|
1751
|
+
res.ok ? res.json() : Promise.reject(`HTTP ${res.status}`),
|
|
1941
1752
|
);
|
|
1942
1753
|
|
|
1943
1754
|
class ApiValidationError extends Error {
|
|
@@ -2004,28 +1815,18 @@ export function RegistrationForm() {
|
|
|
2004
1815
|
<form onSubmit={(e) => void form.handleSubmit(e)}>
|
|
2005
1816
|
<input {...form.getFieldProps('username')} />
|
|
2006
1817
|
{form.validatingFields.username && <span>Checking...</span>}
|
|
2007
|
-
{form.getFieldError('username') && (
|
|
2008
|
-
<span>{form.getFieldError('username')}</span>
|
|
2009
|
-
)}
|
|
1818
|
+
{form.getFieldError('username') && <span>{form.getFieldError('username')}</span>}
|
|
2010
1819
|
|
|
2011
1820
|
<input type="email" {...form.getFieldProps('email')} />
|
|
2012
|
-
{form.getFieldError('email') && (
|
|
2013
|
-
<span>{form.getFieldError('email')}</span>
|
|
2014
|
-
)}
|
|
1821
|
+
{form.getFieldError('email') && <span>{form.getFieldError('email')}</span>}
|
|
2015
1822
|
|
|
2016
1823
|
<input type="password" {...form.getFieldProps('password')} />
|
|
2017
|
-
{form.getFieldError('password') && (
|
|
2018
|
-
<span>{form.getFieldError('password')}</span>
|
|
2019
|
-
)}
|
|
1824
|
+
{form.getFieldError('password') && <span>{form.getFieldError('password')}</span>}
|
|
2020
1825
|
|
|
2021
1826
|
<input type="password" {...form.getFieldProps('confirmPassword')} />
|
|
2022
|
-
{form.getFieldError('confirmPassword') && (
|
|
2023
|
-
<span>{form.getFieldError('confirmPassword')}</span>
|
|
2024
|
-
)}
|
|
1827
|
+
{form.getFieldError('confirmPassword') && <span>{form.getFieldError('confirmPassword')}</span>}
|
|
2025
1828
|
|
|
2026
|
-
{form.errors[ROOT_ERROR_KEY] &&
|
|
2027
|
-
<span>{form.errors[ROOT_ERROR_KEY]}</span>
|
|
2028
|
-
)}
|
|
1829
|
+
{form.errors[ROOT_ERROR_KEY] && <span>{form.errors[ROOT_ERROR_KEY]}</span>}
|
|
2029
1830
|
|
|
2030
1831
|
<button type="submit" disabled={mutation.isPending || form.isValidating}>
|
|
2031
1832
|
{mutation.isPending ? 'Registering...' : 'Create Account'}
|