react-simple-formkit 2.0.3 → 2.0.4
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 +75 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# React Simple FormKit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Supports managing forms as uncontrolled. State updates only when watched. Simple and quick to configure with outstanding efficiency.
|
|
4
4
|
|
|
5
5
|
# Table of Contents
|
|
6
6
|
|
|
@@ -40,9 +40,13 @@ Support manage forms as uncontrolled. State updates only when watched. Simple an
|
|
|
40
40
|
|
|
41
41
|
# Features
|
|
42
42
|
|
|
43
|
-
-
|
|
44
|
-
-
|
|
45
|
-
-
|
|
43
|
+
- **Lightweight**: only ~**15KB**
|
|
44
|
+
- **Easy setup**: simple and quick configuration
|
|
45
|
+
- **Optimized for uncontrolled forms**: no unnecessary re-renders
|
|
46
|
+
- Comprehensive form management:
|
|
47
|
+
- [Values](#manage-values): get/set values, defaults, reset, ...
|
|
48
|
+
- [States](#manage-states): dirty, touched, custom states, ...
|
|
49
|
+
- [Errors](#manage-errors): form-level and field-level errors, ...
|
|
46
50
|
|
|
47
51
|
# Quick start
|
|
48
52
|
|
|
@@ -69,9 +73,67 @@ return (
|
|
|
69
73
|
|
|
70
74
|
## Modes of input field
|
|
71
75
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
### `Uncontrolled`:
|
|
77
|
+
|
|
78
|
+
Basic HTML form elements whose value is natively managed by the browser. Such as
|
|
79
|
+
`<input type="text">`, `<input type="checkbox">`, `<select>`, or `<textarea>`.
|
|
80
|
+
|
|
81
|
+
> **Note:** Or also **UI library components** that are simple wrappers around these native elements that **still follow the browser’s standard behavior**
|
|
82
|
+
|
|
83
|
+
### `Controlled`:
|
|
84
|
+
|
|
85
|
+
Advanced UI components with complex **INPUT AND OUTPUT** . For example, a multiple select component expects an array as its input/output, but your form state might need to store the value as a string.
|
|
86
|
+
|
|
87
|
+
By using `Controller`, you can transform the value in both directions:
|
|
88
|
+
|
|
89
|
+
- On `input`: split the stored string into an array to pass to the UI component.
|
|
90
|
+
- On `change`: join the selected array back into a string before updating the form state.
|
|
91
|
+
|
|
92
|
+
> **Note:** Or whenever you want to **control the value of a field** (e.g. by calling `actions.setValue`), you **must** wrap that field with a `Controller`.
|
|
93
|
+
> Without `Controller`, the field will not respond to external value changes.
|
|
94
|
+
|
|
95
|
+
Example:
|
|
96
|
+
|
|
97
|
+
```
|
|
98
|
+
<Controller
|
|
99
|
+
name="multipleSelect"
|
|
100
|
+
render={({ value = "", onChange, name }) => {
|
|
101
|
+
return (
|
|
102
|
+
<Select
|
|
103
|
+
multiple
|
|
104
|
+
name={name}
|
|
105
|
+
value={value.split(",")}
|
|
106
|
+
onChange={(e) => {
|
|
107
|
+
const value = e.target.value;
|
|
108
|
+
// value as array by default but on autofill value as string
|
|
109
|
+
onChange(typeof value === "string" ? value : value.join(","));
|
|
110
|
+
}}
|
|
111
|
+
>
|
|
112
|
+
<MenuItem value="10">Ten</MenuItem>
|
|
113
|
+
<MenuItem value="20">Twenty</MenuItem>
|
|
114
|
+
<MenuItem value="30">Thirty</MenuItem>
|
|
115
|
+
</Select>
|
|
116
|
+
);
|
|
117
|
+
}}
|
|
118
|
+
/>
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### `Captured`
|
|
122
|
+
|
|
123
|
+
Advanced UI components with complex **OUTPUT ONLY**
|
|
124
|
+
|
|
125
|
+
In these cases, the field is **captured**:
|
|
126
|
+
|
|
127
|
+
- You only listen to its value changes through a custom `onChange`.
|
|
128
|
+
- You can sync the final value into the form when needed (via `actions.setValue`).
|
|
129
|
+
|
|
130
|
+
Example:
|
|
131
|
+
|
|
132
|
+
```
|
|
133
|
+
<LocalizationProvider dateAdapter={AdapterDayjs}>
|
|
134
|
+
<DatePicker name="date" onChange={(newValue) => actions.setValue("date", newValue.format("MM/DD/YYYY"))} />
|
|
135
|
+
</LocalizationProvider>
|
|
136
|
+
```
|
|
75
137
|
|
|
76
138
|
```
|
|
77
139
|
const { control, actions } = useForm();
|
|
@@ -114,7 +176,7 @@ return (
|
|
|
114
176
|
)
|
|
115
177
|
```
|
|
116
178
|
|
|
117
|
-
|
|
179
|
+
> **Note\*:** All fields in the form must follow one of the three modes above to ensure the form works correctly
|
|
118
180
|
|
|
119
181
|
## Watching for updates
|
|
120
182
|
|
|
@@ -140,7 +202,7 @@ const values = watch("values")
|
|
|
140
202
|
// useWatch
|
|
141
203
|
useWatch({ name: "fieldName" })
|
|
142
204
|
useWatch({ name: ["fieldName", "fieldName2"] })
|
|
143
|
-
useWatch({ compute: (values) => values.number > 10
|
|
205
|
+
useWatch({ compute: (values) => values.number > 10 ? true : false })
|
|
144
206
|
|
|
145
207
|
// actions.getValues()
|
|
146
208
|
const { actions } = useForm()
|
|
@@ -241,12 +303,12 @@ const { fieldName, fieldName2 } = watch("errors")
|
|
|
241
303
|
```
|
|
242
304
|
// actions.setError()
|
|
243
305
|
const { control, actions, watch } = useForm()
|
|
244
|
-
const { isError,
|
|
306
|
+
const { isError, "errors.input1": input1Error } = watch(["isError", "errors.input1"])
|
|
245
307
|
|
|
246
308
|
return (
|
|
247
309
|
<Form control={control} onChange={console.log}>
|
|
248
310
|
<input name="input1" />
|
|
249
|
-
{
|
|
311
|
+
{input1Error && <span className="error-message">{input1Error}</span>}
|
|
250
312
|
<button type="button" onClick={() => actions.setError("number", "This is error message")}>
|
|
251
313
|
Set Error
|
|
252
314
|
</button>
|
|
@@ -288,8 +350,8 @@ Return:
|
|
|
288
350
|
Generic props:
|
|
289
351
|
|
|
290
352
|
- `control`: received from useForm()
|
|
291
|
-
- `onSubmit`: `(currentValues) => {}`
|
|
292
|
-
- `onChange`: `(name, value, currentValues) => {}`
|
|
353
|
+
- `onSubmit`: `(currentValues) => {}` called when the form is submitted via a button with `type='submit'`
|
|
354
|
+
- `onChange`: `(name, value, currentValues) => {}` called when any field value changes
|
|
293
355
|
|
|
294
356
|
## Controller
|
|
295
357
|
|