@os-design/form 1.0.53 → 1.0.55
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 +133 -59
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Create forms in React and React Native much faster and easier.
|
|
|
6
6
|
|
|
7
7
|
- 🤓 Rerenders only updated fields, not the whole form.
|
|
8
8
|
- 📱 Use with any design system or native components. Supports React Native.
|
|
9
|
-
-
|
|
9
|
+
- 2️⃣ Tiny size (~2 KB). Zero dependencies.
|
|
10
10
|
- 📙 Lots of useful features.
|
|
11
11
|
|
|
12
12
|
## Installation
|
|
@@ -25,10 +25,15 @@ Let's assume that we want to build a form for creating blog posts. To create a f
|
|
|
25
25
|
import { useForm } from '@os-design/form';
|
|
26
26
|
|
|
27
27
|
const Form: React.FC = () => {
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
const initValues = useMemo(
|
|
29
|
+
() => ({
|
|
30
|
+
title: '',
|
|
31
|
+
content: '',
|
|
32
|
+
}),
|
|
33
|
+
[]
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const { Field, form } = useForm(initValues);
|
|
32
37
|
|
|
33
38
|
const onSubmit = useCallback(() => {
|
|
34
39
|
// To get all the form data
|
|
@@ -78,10 +83,15 @@ To solve this problem and update only those options that have changed, we need t
|
|
|
78
83
|
|
|
79
84
|
```tsx
|
|
80
85
|
const Form: React.FC = () => {
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
86
|
+
const initValues = useMemo<ComplexFormData>(
|
|
87
|
+
() => ({
|
|
88
|
+
name: '',
|
|
89
|
+
options: { min: null, max: null },
|
|
90
|
+
}),
|
|
91
|
+
[]
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const { Field, form } = useForm(initValues);
|
|
85
95
|
|
|
86
96
|
const onSubmit = useCallback(() => {
|
|
87
97
|
console.log(form.values.getAll());
|
|
@@ -112,10 +122,15 @@ If the user entered incorrect values, the form should display errors. To support
|
|
|
112
122
|
|
|
113
123
|
```tsx
|
|
114
124
|
const Form: React.FC = () => {
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
125
|
+
const initValues = useMemo(
|
|
126
|
+
() => ({
|
|
127
|
+
title: '',
|
|
128
|
+
content: '',
|
|
129
|
+
}),
|
|
130
|
+
[]
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const { Field, form } = useForm(initValues);
|
|
119
134
|
|
|
120
135
|
const onSubmit = useCallback(() => {
|
|
121
136
|
form.errors.set('title', 'The title is too long 😔');
|
|
@@ -156,10 +171,15 @@ const Error: React.FC = () => {
|
|
|
156
171
|
};
|
|
157
172
|
|
|
158
173
|
const Form: React.FC = () => {
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
174
|
+
const initValues = useMemo(
|
|
175
|
+
() => ({
|
|
176
|
+
title: '',
|
|
177
|
+
content: '',
|
|
178
|
+
}),
|
|
179
|
+
[]
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
const { Field, form } = useForm(initValues);
|
|
163
183
|
|
|
164
184
|
const onSubmit = useCallback(() => {
|
|
165
185
|
// Set the error
|
|
@@ -230,10 +250,15 @@ const BaseForm: React.FC<BaseFormProps> = ({ children }) => {
|
|
|
230
250
|
};
|
|
231
251
|
|
|
232
252
|
const FormCreate: React.FC = () => {
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
253
|
+
const initValues = useMemo<FormData>(
|
|
254
|
+
() => ({
|
|
255
|
+
title: '',
|
|
256
|
+
content: '',
|
|
257
|
+
}),
|
|
258
|
+
[]
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
const { form } = useForm(initValues);
|
|
237
262
|
|
|
238
263
|
const onCreate = useCallback(() => {
|
|
239
264
|
console.log('Creating...', form.values.getAll());
|
|
@@ -249,10 +274,15 @@ const FormCreate: React.FC = () => {
|
|
|
249
274
|
};
|
|
250
275
|
|
|
251
276
|
const FormUpdate: React.FC = () => {
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
277
|
+
const initValues = useMemo<FormData>(
|
|
278
|
+
() => ({
|
|
279
|
+
title: 'Title',
|
|
280
|
+
content: 'Content',
|
|
281
|
+
}),
|
|
282
|
+
[]
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
const { form } = useForm(initValues);
|
|
256
286
|
|
|
257
287
|
const onUpdate = useCallback(() => {
|
|
258
288
|
console.log('Updating...', form.values.getAll());
|
|
@@ -274,10 +304,15 @@ Let's upgrade our form and allow the user to click on the `Save` button only if
|
|
|
274
304
|
|
|
275
305
|
```tsx
|
|
276
306
|
const Form: React.FC = () => {
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
307
|
+
const initValues = useMemo(
|
|
308
|
+
() => ({
|
|
309
|
+
title: 'Title',
|
|
310
|
+
content: 'Content',
|
|
311
|
+
}),
|
|
312
|
+
[]
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
const { Field, form, modified } = useForm(initValues);
|
|
281
316
|
|
|
282
317
|
const onSubmit = useCallback(() => {
|
|
283
318
|
console.log(form.values.getAll());
|
|
@@ -303,10 +338,15 @@ If the form has many fields, it is better to send only the changed values to the
|
|
|
303
338
|
|
|
304
339
|
```tsx
|
|
305
340
|
const Form: React.FC = () => {
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
341
|
+
const initValues = useMemo(
|
|
342
|
+
() => ({
|
|
343
|
+
title: '',
|
|
344
|
+
content: '',
|
|
345
|
+
}),
|
|
346
|
+
[]
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
const { Field, form, modifiedFields } = useForm(initValues);
|
|
310
350
|
|
|
311
351
|
const onSubmit = useCallback(() => {
|
|
312
352
|
const onlyChangedValues = modifiedFields.reduce(
|
|
@@ -342,10 +382,15 @@ const ResetButton: React.FC<ButtonProps> = (props) => (
|
|
|
342
382
|
);
|
|
343
383
|
|
|
344
384
|
const Form: React.FC = () => {
|
|
345
|
-
const
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
385
|
+
const initValues = useMemo(
|
|
386
|
+
() => ({
|
|
387
|
+
name: 'Kate',
|
|
388
|
+
password: 'secret',
|
|
389
|
+
}),
|
|
390
|
+
[]
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
const { Field, form } = useForm(initValues);
|
|
349
394
|
|
|
350
395
|
const onSubmit = useCallback(() => {
|
|
351
396
|
console.log(form.values.getAll());
|
|
@@ -381,10 +426,15 @@ To reset the entire form (e.g. when the modal with the form is closed), call `fo
|
|
|
381
426
|
|
|
382
427
|
```tsx
|
|
383
428
|
const Form: React.FC = () => {
|
|
384
|
-
const
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
429
|
+
const initValues = useMemo(
|
|
430
|
+
() => ({
|
|
431
|
+
title: 'Title',
|
|
432
|
+
content: 'Content',
|
|
433
|
+
}),
|
|
434
|
+
[]
|
|
435
|
+
);
|
|
436
|
+
|
|
437
|
+
const { Field, form } = useForm(initValues);
|
|
388
438
|
|
|
389
439
|
const onReset = useCallback(() => form.reset(), [form]);
|
|
390
440
|
|
|
@@ -406,7 +456,9 @@ The fields are rerendered ONLY when the value or error has been changed. If your
|
|
|
406
456
|
// ❌ Incorrect because the input will not be rerendered when the `number` is updated.
|
|
407
457
|
// The text in the input will always be `Number is 0`.
|
|
408
458
|
const Form: React.FC = () => {
|
|
409
|
-
const
|
|
459
|
+
const initValues = useMemo(() => ({ name: '' }), []);
|
|
460
|
+
|
|
461
|
+
const { Field } = useForm(initValues);
|
|
410
462
|
|
|
411
463
|
const [number, setNumber] = useState(0);
|
|
412
464
|
|
|
@@ -432,7 +484,9 @@ To solve this issue, you need to pass additional data on which the field depends
|
|
|
432
484
|
```tsx
|
|
433
485
|
// ✅ Correct
|
|
434
486
|
const Form: React.FC = () => {
|
|
435
|
-
const
|
|
487
|
+
const initValues = useMemo(() => ({ name: '' }), []);
|
|
488
|
+
|
|
489
|
+
const { Field } = useForm(initValues);
|
|
436
490
|
|
|
437
491
|
const [number, setNumber] = useState(0);
|
|
438
492
|
|
|
@@ -460,10 +514,15 @@ The entered value in the field can be transformed. For example, the email addres
|
|
|
460
514
|
|
|
461
515
|
```tsx
|
|
462
516
|
const Form: React.FC = () => {
|
|
463
|
-
const
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
517
|
+
const initValues = useMemo(
|
|
518
|
+
() => ({
|
|
519
|
+
email: '',
|
|
520
|
+
password: '',
|
|
521
|
+
}),
|
|
522
|
+
[]
|
|
523
|
+
);
|
|
524
|
+
|
|
525
|
+
const { Field, form } = useForm(initValues);
|
|
467
526
|
|
|
468
527
|
const onSubmit = useCallback(() => {
|
|
469
528
|
console.log(form.values.getAll());
|
|
@@ -494,10 +553,15 @@ Let's consider the first case. To implement it, use the `useTransformer` hook.
|
|
|
494
553
|
|
|
495
554
|
```tsx
|
|
496
555
|
const Form: React.FC = () => {
|
|
497
|
-
const
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
556
|
+
const initValues = useMemo(
|
|
557
|
+
() => ({
|
|
558
|
+
title: '',
|
|
559
|
+
metaTitle: '',
|
|
560
|
+
}),
|
|
561
|
+
[]
|
|
562
|
+
);
|
|
563
|
+
|
|
564
|
+
const { Field, form, useTransformer } = useForm(initValues);
|
|
501
565
|
|
|
502
566
|
const onSubmit = useCallback(() => {
|
|
503
567
|
console.log(form.values.getAll());
|
|
@@ -527,10 +591,15 @@ For example, if the url slug is generated by the title on the server side, you n
|
|
|
527
591
|
|
|
528
592
|
```tsx
|
|
529
593
|
const Form: React.FC = () => {
|
|
530
|
-
const
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
594
|
+
const initValues = useMemo(
|
|
595
|
+
() => ({
|
|
596
|
+
title: '',
|
|
597
|
+
urlSlug: '',
|
|
598
|
+
}),
|
|
599
|
+
[]
|
|
600
|
+
);
|
|
601
|
+
|
|
602
|
+
const { Field, form, useValue } = useForm(initValues);
|
|
534
603
|
|
|
535
604
|
const onSubmit = useCallback(() => {
|
|
536
605
|
console.log(form.values.getAll());
|
|
@@ -561,10 +630,15 @@ You can also track changes to all fields, for example, for logging.
|
|
|
561
630
|
|
|
562
631
|
```tsx
|
|
563
632
|
const Form: React.FC = () => {
|
|
564
|
-
const
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
633
|
+
const initValues = useMemo(
|
|
634
|
+
() => ({
|
|
635
|
+
title: '',
|
|
636
|
+
content: '',
|
|
637
|
+
}),
|
|
638
|
+
[]
|
|
639
|
+
);
|
|
640
|
+
|
|
641
|
+
const { Field, form } = useForm(initValues);
|
|
568
642
|
|
|
569
643
|
const onSubmit = useCallback(() => {
|
|
570
644
|
console.log(form.values.getAll());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@os-design/form",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.55",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"repository": "git@gitlab.com:os-team/libs/os-design.git",
|
|
6
6
|
"main": "dist/cjs/index.js",
|
|
@@ -30,11 +30,11 @@
|
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@os-design/core": "^1.0.
|
|
33
|
+
"@os-design/core": "^1.0.222",
|
|
34
34
|
"@os-design/icons": "^1.0.55"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"react": ">=18"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "ca3e642872fe0be6503d1d6bf354bac2f9d1bc81"
|
|
40
40
|
}
|