@os-design/form 1.0.0 → 1.0.1
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 +42 -10
- package/package.json +2 -2
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
|
-
- 0️⃣
|
|
9
|
+
- 0️⃣ Tiny size (~1.5 KB). Zero dependencies.
|
|
10
10
|
- 📙 Lots of useful features.
|
|
11
11
|
|
|
12
12
|
## Installation
|
|
@@ -30,10 +30,14 @@ interface FormData {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
const Form: React.FC = () => {
|
|
33
|
-
const { Field, form } = useForm<FormData>({
|
|
33
|
+
const { Field, form } = useForm<FormData>({
|
|
34
|
+
title: '',
|
|
35
|
+
content: '',
|
|
36
|
+
});
|
|
34
37
|
|
|
35
38
|
const onSubmit = useCallback(() => {
|
|
36
|
-
|
|
39
|
+
// To get all the form data
|
|
40
|
+
console.log(form.values.getAll());
|
|
37
41
|
}, []);
|
|
38
42
|
|
|
39
43
|
return (
|
|
@@ -54,7 +58,10 @@ If the user entered incorrect values, the form should display errors. To support
|
|
|
54
58
|
|
|
55
59
|
```tsx
|
|
56
60
|
const Form: React.FC = () => {
|
|
57
|
-
const { Field, form } = useForm<FormData>({
|
|
61
|
+
const { Field, form } = useForm<FormData>({
|
|
62
|
+
title: '',
|
|
63
|
+
content: '',
|
|
64
|
+
});
|
|
58
65
|
|
|
59
66
|
const onSubmit = useCallback(() => {
|
|
60
67
|
form.errors.set('title', 'The title is too long 😔');
|
|
@@ -100,14 +107,18 @@ const Error: React.FC = () => {
|
|
|
100
107
|
};
|
|
101
108
|
|
|
102
109
|
const Form: React.FC = () => {
|
|
103
|
-
const { Field, form } = useForm<FormData>({
|
|
110
|
+
const { Field, form } = useForm<FormData>({
|
|
111
|
+
title: '',
|
|
112
|
+
content: '',
|
|
113
|
+
});
|
|
104
114
|
|
|
105
115
|
const onSubmit = useCallback(() => {
|
|
106
116
|
form.errors.set('_error', 'The server went on vacation 🌴'); // Set the error
|
|
107
117
|
console.log(form.values.getAll()); // { title: '', content: '' } - data only, without the `_error` prop
|
|
108
118
|
}, []);
|
|
109
119
|
|
|
110
|
-
// Wrap your form in a `FormProvider` to pass the `form`
|
|
120
|
+
// Wrap your form in a `FormProvider` to pass the `form`
|
|
121
|
+
// to the child components (to the `Error` component in our case)
|
|
111
122
|
return (
|
|
112
123
|
<FormProvider form={form}>
|
|
113
124
|
<>
|
|
@@ -161,7 +172,10 @@ const BaseForm: React.FC<BaseFormProps> = ({ children }) => {
|
|
|
161
172
|
};
|
|
162
173
|
|
|
163
174
|
const FormCreate: React.FC = () => {
|
|
164
|
-
const { form } = useForm<FormData>({
|
|
175
|
+
const { form } = useForm<FormData>({
|
|
176
|
+
title: '',
|
|
177
|
+
content: '',
|
|
178
|
+
});
|
|
165
179
|
|
|
166
180
|
const onCreate = useCallback(() => {
|
|
167
181
|
console.log('Creating...', form.values.getAll());
|
|
@@ -177,7 +191,10 @@ const FormCreate: React.FC = () => {
|
|
|
177
191
|
};
|
|
178
192
|
|
|
179
193
|
const FormUpdate: React.FC = () => {
|
|
180
|
-
const { form } = useForm<FormData>({
|
|
194
|
+
const { form } = useForm<FormData>({
|
|
195
|
+
title: 'Title',
|
|
196
|
+
content: 'Content',
|
|
197
|
+
});
|
|
181
198
|
|
|
182
199
|
const onUpdate = useCallback(() => {
|
|
183
200
|
console.log('Updating...', form.values.getAll());
|
|
@@ -325,7 +342,10 @@ interface PersonData {
|
|
|
325
342
|
}
|
|
326
343
|
|
|
327
344
|
const Form: React.FC = () => {
|
|
328
|
-
const { Field, form } = useForm<PersonData>({
|
|
345
|
+
const { Field, form } = useForm<PersonData>({
|
|
346
|
+
name: 'Kate',
|
|
347
|
+
age: 18,
|
|
348
|
+
});
|
|
329
349
|
|
|
330
350
|
const onSubmit = useCallback(() => {
|
|
331
351
|
console.log(form.values.getAll());
|
|
@@ -385,6 +405,15 @@ const Form: React.FC = () => {
|
|
|
385
405
|
};
|
|
386
406
|
```
|
|
387
407
|
|
|
408
|
+
You can update any number of fields, not just one. For example, every time the title changes, we can set `metaTitle` and `metaDescription` to an empty string.
|
|
409
|
+
|
|
410
|
+
```tsx
|
|
411
|
+
useTransformer('title', () => ({
|
|
412
|
+
metaTitle: '',
|
|
413
|
+
metaDescription: '',
|
|
414
|
+
}));
|
|
415
|
+
```
|
|
416
|
+
|
|
388
417
|
## 👀 Tracking field changes
|
|
389
418
|
|
|
390
419
|
You can track changes for a specific field using the `useValue` hook.
|
|
@@ -410,7 +439,8 @@ const Form: React.FC = () => {
|
|
|
410
439
|
const title = useValue('title');
|
|
411
440
|
|
|
412
441
|
useEffect(() => {
|
|
413
|
-
// Send a request to the server
|
|
442
|
+
// Send a request to the server
|
|
443
|
+
// to generate the url slug by title
|
|
414
444
|
setTimeout(() => {
|
|
415
445
|
// Update the url slug field
|
|
416
446
|
form.values.set('urlSlug', title.length.toString());
|
|
@@ -456,3 +486,5 @@ const Form: React.FC = () => {
|
|
|
456
486
|
);
|
|
457
487
|
};
|
|
458
488
|
```
|
|
489
|
+
|
|
490
|
+
That's all for now. I hope this library will help you create any forms faster. 😉
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@os-design/form",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"repository": "git@gitlab.com:os-team/libs/os-design.git",
|
|
6
6
|
"main": "dist/cjs/index.js",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"react": ">=18"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "0ee51ebd7e24197af32f68d5354e7dc9a8645af1"
|
|
38
38
|
}
|