react-simple-formkit 1.1.5 → 1.1.6
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 +194 -144
- package/dist/react-simple-formkit.js +4 -4
- package/dist/react-simple-formkit.mjs +262 -265
- package/dist/react-simple-formkit.umd.js +4 -4
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ return (
|
|
|
39
39
|
|
|
40
40
|
# More kits
|
|
41
41
|
|
|
42
|
-
##
|
|
42
|
+
## Number fields
|
|
43
43
|
|
|
44
44
|
- By default form, values are string at all. So, adding numberFields will catch the number value in on submit
|
|
45
45
|
|
|
@@ -111,67 +111,117 @@ return (
|
|
|
111
111
|
## Validate by other field
|
|
112
112
|
|
|
113
113
|
```
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
const form = useForm();
|
|
115
|
+
const { isDirty, isError, errors, actions } = form;
|
|
116
116
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
117
|
+
return (
|
|
118
|
+
<Form form={form}>
|
|
119
|
+
<input required name="number" onBlur={actions.checkValidity} />
|
|
120
|
+
{errors.number && <span className="error-message">{errors.number}</span>}
|
|
121
|
+
|
|
122
|
+
<input
|
|
123
|
+
required
|
|
124
|
+
name="number2"
|
|
125
|
+
onBlur={(e) => {
|
|
126
|
+
let error = null;
|
|
127
|
+
error = actions.getFieldValidity(e);
|
|
128
|
+
|
|
129
|
+
if (error) {
|
|
130
|
+
actions.changeError("number2", error);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const value = Number(e.target.value || 0);
|
|
135
|
+
const formState = actions.getFormState();
|
|
136
|
+
const number = Number(formState.number || 0);
|
|
137
|
+
|
|
138
|
+
if (value >= number) error = "Number 2 must be less than number 1";
|
|
139
|
+
actions.changeError("number2", error);
|
|
140
|
+
}}
|
|
141
|
+
/>
|
|
142
|
+
{errors.number2 && <span className="error-message">{errors.number2}</span>}
|
|
143
|
+
<button type="submit" disabled={!isDirty || isError}>
|
|
144
|
+
Submit
|
|
145
|
+
</button>
|
|
146
|
+
</Form>
|
|
147
|
+
)
|
|
148
|
+
```
|
|
121
149
|
|
|
122
|
-
|
|
123
|
-
required
|
|
124
|
-
name="number2"
|
|
125
|
-
onBlur={(e) => {
|
|
126
|
-
let error = null;
|
|
127
|
-
error = actions.getFieldValidity(e);
|
|
150
|
+
## Centralized processing with onChange
|
|
128
151
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
152
|
+
```
|
|
153
|
+
const handleChange = (data) => {
|
|
154
|
+
console.log(data);
|
|
133
155
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
156
|
+
// setValue will trigger onChange by default and recall handle change making an infinite loop
|
|
157
|
+
// That why we put shouldOnChange = false here
|
|
158
|
+
actions.setValue("email2", data.email + "2", { shouldOnChange: false });
|
|
159
|
+
};
|
|
137
160
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
161
|
+
return (
|
|
162
|
+
<Form form={form} onChange={handleChange}>
|
|
163
|
+
<input required name="email" />
|
|
164
|
+
{/* Controller make this field assignable */}
|
|
165
|
+
<Controller
|
|
166
|
+
name="email2"
|
|
167
|
+
render={({ ref, name, value, setValue }) => (
|
|
168
|
+
<input required ref={ref} name={name} value={value} onChange={(e) => setValue(e.target.value)} />
|
|
169
|
+
)}
|
|
170
|
+
/>
|
|
171
|
+
<button type="submit" disabled={!isDirty}>
|
|
172
|
+
Submit
|
|
173
|
+
</button>
|
|
174
|
+
</Form>
|
|
175
|
+
);
|
|
148
176
|
```
|
|
149
177
|
|
|
150
|
-
##
|
|
178
|
+
## Default values
|
|
179
|
+
|
|
180
|
+
-
|
|
151
181
|
|
|
152
182
|
```
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
183
|
+
const [loading, setLoading] = useState(false);
|
|
184
|
+
const [defaultValues, setDefaultValues] = useState({ email: "email@example.com", password: "123456" });
|
|
185
|
+
|
|
186
|
+
const form = useForm();
|
|
187
|
+
const { isDirty, actions } = form;
|
|
188
|
+
|
|
189
|
+
const handleSubmit = async (data) => {
|
|
190
|
+
setLoading(true);
|
|
191
|
+
await new Promise((res) => setTimeout(res, 1500));
|
|
192
|
+
setDefaultValues(data);
|
|
193
|
+
// After update default values, reload the form
|
|
194
|
+
// Because data updating is asynchronous. Catching it's change is ineffective.
|
|
195
|
+
actions.reload();
|
|
196
|
+
setLoading(false);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
return (
|
|
200
|
+
<Form form={form} onSubmit={handleSubmit}>
|
|
201
|
+
<input required name="email" defaultValue={defaultValues.email} />
|
|
202
|
+
<Controller
|
|
203
|
+
name="password"
|
|
204
|
+
defaultValue={defaultValues.password}
|
|
205
|
+
render={({ ref, name, defaultValue, value, setValue }) => (
|
|
206
|
+
<input
|
|
207
|
+
ref={ref}
|
|
208
|
+
required
|
|
209
|
+
name={name}
|
|
210
|
+
type="password"
|
|
211
|
+
defaultValue={defaultValue}
|
|
212
|
+
value={value}
|
|
213
|
+
onChange={(e) => setValue(e.target.value)}
|
|
214
|
+
/>
|
|
215
|
+
)}
|
|
216
|
+
/>
|
|
217
|
+
<button type="reset" disabled={!isDirty}>
|
|
218
|
+
Reset
|
|
219
|
+
</button>
|
|
220
|
+
<Button disableElevation variant="contained" type="submit" loading={loading} disabled={!isDirty}>
|
|
221
|
+
Submit
|
|
222
|
+
</Button>
|
|
223
|
+
</Form>
|
|
224
|
+
);
|
|
175
225
|
```
|
|
176
226
|
|
|
177
227
|
# Support for third party library
|
|
@@ -183,27 +233,27 @@ return (
|
|
|
183
233
|
- Just put the actions.instantChange where these components onchange
|
|
184
234
|
|
|
185
235
|
```
|
|
186
|
-
|
|
187
|
-
|
|
236
|
+
const form = useForm();
|
|
237
|
+
const { isDirty, actions } = form;
|
|
188
238
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
239
|
+
return (
|
|
240
|
+
<Form form={form}>
|
|
241
|
+
<Select name="select" onChange={actions.instantChange}>
|
|
242
|
+
<MenuItem value={10}>Ten</MenuItem>
|
|
243
|
+
<MenuItem value={20}>Twenty</MenuItem>
|
|
244
|
+
<MenuItem value={30}>Thirty</MenuItem>
|
|
245
|
+
</Select>
|
|
246
|
+
<LocalizationProvider dateAdapter={AdapterDayjs}>
|
|
247
|
+
<DatePicker name="date" onChange={actions.instantChange} />
|
|
248
|
+
</LocalizationProvider>
|
|
249
|
+
<button type="button" onClick={() => alert(JSON.stringify(actions.getFormState()))}>
|
|
250
|
+
Get value
|
|
251
|
+
</button>
|
|
252
|
+
<button type="submit" disabled={!isDirty}>
|
|
253
|
+
Submit
|
|
254
|
+
</button>
|
|
255
|
+
</Form>
|
|
256
|
+
)
|
|
207
257
|
```
|
|
208
258
|
|
|
209
259
|
## More control with Controller
|
|
@@ -212,79 +262,79 @@ return (
|
|
|
212
262
|
- Make field assignable by actions.setValue
|
|
213
263
|
|
|
214
264
|
```
|
|
215
|
-
|
|
265
|
+
import { Controller, Form, useForm } from "react-simple-formkit";
|
|
216
266
|
|
|
217
|
-
|
|
218
|
-
|
|
267
|
+
const form = useForm({ numberFields: ["number1", "number2"] });
|
|
268
|
+
const { isDirty, actions } = form;
|
|
219
269
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
270
|
+
return (
|
|
271
|
+
<Form form={form}>
|
|
272
|
+
{/* Custom value as array */}
|
|
273
|
+
<Controller
|
|
274
|
+
name="multipleSelect"
|
|
275
|
+
defaultValue={[]}
|
|
276
|
+
render={({ ref, value, setValue, name }) => (
|
|
277
|
+
<Select
|
|
278
|
+
multiple
|
|
279
|
+
ref={ref}
|
|
280
|
+
name={name}
|
|
281
|
+
labelId="demo-multiple-name-label"
|
|
282
|
+
id="demo-multiple-name"
|
|
283
|
+
value={value}
|
|
284
|
+
onChange={(e) => {
|
|
285
|
+
const value = e.target.value;
|
|
286
|
+
// On autofill we get a stringified value.
|
|
287
|
+
setValue(typeof value === "string" ? value.split(",") : value);
|
|
288
|
+
// instantChange by select
|
|
289
|
+
actions.instantChange();
|
|
290
|
+
}}
|
|
291
|
+
input={<OutlinedInput label="Name" />}
|
|
292
|
+
>
|
|
293
|
+
<MenuItem value={10}>Ten</MenuItem>
|
|
294
|
+
<MenuItem value={20}>Twenty</MenuItem>
|
|
295
|
+
<MenuItem value={30}>Thirty</MenuItem>
|
|
296
|
+
</Select>
|
|
297
|
+
)}
|
|
298
|
+
/>
|
|
299
|
+
{/* actions.setValue (target field of actions.setValue must use controller)*/}
|
|
300
|
+
<input
|
|
301
|
+
name="number1"
|
|
302
|
+
placeholder="number 1"
|
|
303
|
+
onBlur={(e) => {
|
|
304
|
+
const number1 = Number(e.target.value || 0);
|
|
305
|
+
const number2 = actions.getFormState().number2 || 0;
|
|
306
|
+
actions.setValue("sum", number1 + number2);
|
|
307
|
+
}}
|
|
308
|
+
/>
|
|
309
|
+
<input
|
|
310
|
+
name="number2"
|
|
311
|
+
placeholder="number 2"
|
|
312
|
+
onBlur={(e) => {
|
|
313
|
+
const number2 = Number(e.target.value || 0);
|
|
314
|
+
const number1 = actions.getFormState().number1 || 0;
|
|
315
|
+
actions.setValue("sum", number1 + number2);
|
|
316
|
+
}}
|
|
317
|
+
/>
|
|
318
|
+
<Controller
|
|
319
|
+
name="sum"
|
|
320
|
+
render={({ ref, value, setValue, name }) => (
|
|
321
|
+
<input
|
|
322
|
+
ref={ref}
|
|
323
|
+
value={value}
|
|
324
|
+
onChange={(e) => setValue(e.target.value)}
|
|
325
|
+
name={name}
|
|
326
|
+
placeholder="sum(number1, number2)"
|
|
327
|
+
/>
|
|
328
|
+
)}
|
|
329
|
+
/>
|
|
330
|
+
<button type="button" onClick={() => alert(JSON.stringify(actions.getFormState()))}>
|
|
331
|
+
Get value
|
|
332
|
+
</button>
|
|
333
|
+
<button type="submit" disabled={!isDirty}>
|
|
334
|
+
Submit
|
|
335
|
+
</button>
|
|
336
|
+
</Form>
|
|
337
|
+
)
|
|
288
338
|
```
|
|
289
339
|
|
|
290
340
|
# Examples
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react");var z={exports:{}},D={};/**
|
|
2
2
|
* @license React
|
|
3
3
|
* react-jsx-runtime.production.js
|
|
4
4
|
*
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
* This source code is licensed under the MIT license found in the
|
|
8
8
|
* LICENSE file in the root directory of this source tree.
|
|
9
|
-
*/var
|
|
9
|
+
*/var Z;function oe(){if(Z)return D;Z=1;var o=Symbol.for("react.transitional.element"),r=Symbol.for("react.fragment");function f(b,c,s){var m=null;if(s!==void 0&&(m=""+s),c.key!==void 0&&(m=""+c.key),"key"in c){s={};for(var l in c)l!=="key"&&(s[l]=c[l])}else s=c;return c=s.ref,{$$typeof:o,type:b,key:m,ref:c!==void 0?c:null,props:s}}return D.Fragment=r,D.jsx=f,D.jsxs=f,D}var Y={};/**
|
|
10
10
|
* @license React
|
|
11
11
|
* react-jsx-runtime.development.js
|
|
12
12
|
*
|
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
*
|
|
15
15
|
* This source code is licensed under the MIT license found in the
|
|
16
16
|
* LICENSE file in the root directory of this source tree.
|
|
17
|
-
*/var
|
|
17
|
+
*/var Q;function se(){return Q||(Q=1,process.env.NODE_ENV!=="production"&&function(){function o(e){if(e==null)return null;if(typeof e=="function")return e.$$typeof===F?null:e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case g:return"Fragment";case $:return"Profiler";case I:return"StrictMode";case W:return"Suspense";case U:return"SuspenseList";case q:return"Activity"}if(typeof e=="object")switch(typeof e.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),e.$$typeof){case P:return"Portal";case M:return(e.displayName||"Context")+".Provider";case G:return(e._context.displayName||"Context")+".Consumer";case L:var t=e.render;return e=e.displayName,e||(e=t.displayName||t.name||"",e=e!==""?"ForwardRef("+e+")":"ForwardRef"),e;case w:return t=e.displayName||null,t!==null?t:o(e.type)||"Memo";case x:t=e._payload,e=e._init;try{return o(e(t))}catch{}}return null}function r(e){return""+e}function f(e){try{r(e);var t=!1}catch{t=!0}if(t){t=console;var i=t.error,v=typeof Symbol=="function"&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object";return i.call(t,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",v),r(e)}}function b(e){if(e===g)return"<>";if(typeof e=="object"&&e!==null&&e.$$typeof===x)return"<...>";try{var t=o(e);return t?"<"+t+">":"<...>"}catch{return"<...>"}}function c(){var e=N.A;return e===null?null:e.getOwner()}function s(){return Error("react-stack-top-frame")}function m(e){if(n.call(e,"key")){var t=Object.getOwnPropertyDescriptor(e,"key").get;if(t&&t.isReactWarning)return!1}return e.key!==void 0}function l(e,t){function i(){S||(S=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",t))}i.isReactWarning=!0,Object.defineProperty(e,"key",{get:i,configurable:!0})}function E(){var e=o(this.type);return y[e]||(y[e]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),e=this.props.ref,e!==void 0?e:null}function R(e,t,i,v,O,h,X,B){return i=h.ref,e={$$typeof:j,type:e,key:t,props:h,_owner:O},(i!==void 0?i:null)!==null?Object.defineProperty(e,"ref",{enumerable:!1,get:E}):Object.defineProperty(e,"ref",{enumerable:!1,value:null}),e._store={},Object.defineProperty(e._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(e,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(e,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:X}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:B}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}function T(e,t,i,v,O,h,X,B){var _=t.children;if(_!==void 0)if(v)if(p(_)){for(v=0;v<_.length;v++)u(_[v]);Object.freeze&&Object.freeze(_)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else u(_);if(n.call(t,"key")){_=o(e);var A=Object.keys(t).filter(function(ae){return ae!=="key"});v=0<A.length?"{key: someKey, "+A.join(": ..., ")+": ...}":"{key: someKey}",V[_+v]||(A=0<A.length?"{"+A.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX:
|
|
18
18
|
let props = %s;
|
|
19
19
|
<%s {...props} />
|
|
20
20
|
React keys must be passed directly to JSX without using spread:
|
|
21
21
|
let props = %s;
|
|
22
|
-
<%s key={someKey} {...props} />`,_,
|
|
22
|
+
<%s key={someKey} {...props} />`,v,_,A,_),V[_+v]=!0)}if(_=null,i!==void 0&&(f(i),_=""+i),m(t)&&(f(t.key),_=""+t.key),"key"in t){i={};for(var H in t)H!=="key"&&(i[H]=t[H])}else i=t;return _&&l(i,typeof e=="function"?e.displayName||e.name||"Unknown":e),R(e,_,h,O,c(),i,X,B)}function u(e){typeof e=="object"&&e!==null&&e.$$typeof===j&&e._store&&(e._store.validated=1)}var k=a,j=Symbol.for("react.transitional.element"),P=Symbol.for("react.portal"),g=Symbol.for("react.fragment"),I=Symbol.for("react.strict_mode"),$=Symbol.for("react.profiler"),G=Symbol.for("react.consumer"),M=Symbol.for("react.context"),L=Symbol.for("react.forward_ref"),W=Symbol.for("react.suspense"),U=Symbol.for("react.suspense_list"),w=Symbol.for("react.memo"),x=Symbol.for("react.lazy"),q=Symbol.for("react.activity"),F=Symbol.for("react.client.reference"),N=k.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,n=Object.prototype.hasOwnProperty,p=Array.isArray,d=console.createTask?console.createTask:function(){return null};k={"react-stack-bottom-frame":function(e){return e()}};var S,y={},C=k["react-stack-bottom-frame"].bind(k,s)(),J=d(b(s)),V={};Y.Fragment=g,Y.jsx=function(e,t,i,v,O){var h=1e4>N.recentlyCreatedOwnerStacks++;return T(e,t,i,!1,v,O,h?Error("react-stack-top-frame"):C,h?d(b(e)):J)},Y.jsxs=function(e,t,i,v,O){var h=1e4>N.recentlyCreatedOwnerStacks++;return T(e,t,i,!0,v,O,h?Error("react-stack-top-frame"):C,h?d(b(e)):J)}}()),Y}var K;function ce(){return K||(K=1,process.env.NODE_ENV==="production"?z.exports=oe():z.exports=se()),z.exports}var ee=ce();const te=a.createContext({}),ue=()=>a.useContext(te),le=({id:o,form:r,method:f,action:b,children:c,onSubmit:s=()=>{},onInput:m=()=>{},onChange:l=()=>{},numberFields:E=[],className:R,...T})=>(a.useEffect(()=>r.actions.registerOnchange(l),[l]),ee.jsx(te.Provider,{value:r,children:ee.jsx("form",{ref:r==null?void 0:r.ref,action:b,className:R,onSubmit:u=>{f||u.preventDefault();const k=r.actions.loadFormValues();s(k)},onInput:u=>{if(u.target.tagName==="FORM"){const k=r.actions.loadFormValues();r.actions.setFormState(k)}else m(u)},onChange:()=>{const u=r.actions.loadFormValues();r.actions.setFormState(u)},onReset:u=>{r&&(u.preventDefault(),r.actions.reset())},...T,children:c},r==null?void 0:r.lastReloadedAt)})),ie=({name:o,defaultValue:r,render:f=({ref:b,name:c,defaultValue:s,value:m,setValue:l})=>null})=>{const[b,c]=a.useState(r),s=ue(),m=a.useRef(),l=a.useCallback((E,{shouldDirty:R=!1}={})=>{s.actions.setFormState(T=>({...T,[o]:E}),{shouldDirty:R}),c(E)},[c,s.actions.setFormState]);return a.useEffect(()=>(s.actions.setFormState(R=>({...R,[o]:r}),{shouldDirty:!1}),s.subscribe(o,(R,{shouldDirty:T=!0,shouldOnChange:u=!0}={})=>{c(R),s.actions.setFormState(k=>({...k,[o]:R}),{shouldDirty:T,shouldOnChange:u}),m.current&&typeof m.current.dispatchEvent=="function"&&u&&m.current.dispatchEvent(new Event("change"))})),[]),f({ref:m,name:o,defaultValue:r,value:b,setValue:l})},fe=(o,r)=>{let f=!1;return Object.keys(r).forEach(b=>{if(!(b in o)||f)return;const c=r[b],s=o[b];(typeof c=="object"||c!==s)&&(f=!0)}),f},re=o=>(typeof o.setCustomValidity=="function"&&o.setCustomValidity(""),!o||typeof o.checkValidity!="function"||o.checkValidity()?null:o.validationMessage),ne=()=>{const[o,r]=a.useState({}),f=a.useMemo(()=>Object.values(o).filter(Boolean).length>0,[o]),b=a.useCallback((l,E)=>{r(R=>R[l]===E?R:{...R,[l]:E})},[]),c=a.useCallback(l=>{r(E=>({...E,...l}))},[]),s=a.useCallback(l=>{r(E=>E[l]?{...E,[l]:""}:E)},[]),m=a.useCallback(()=>{r({})},[]);return{isError:f,errors:o,changeError:b,changeErrors:c,clearError:s,clearErrors:m}},de=()=>{const[o,r]=a.useState(),f=a.useCallback(()=>r(new Date().toString()),[]);return[o,f]},be=[],Ee=({numberFields:o=be}={})=>{const{changeError:r,errors:f,changeErrors:b,clearError:c,clearErrors:s,isError:m}=ne(),[l,E]=a.useState(!1),[R,T]=de(),u=a.useRef({}),k=a.useRef({}),j=a.useRef({}),P=a.useRef(()=>{}),g=a.useRef(),I=a.useCallback((n,{shouldDirty:p=!0,shouldOnChange:d=!0}={})=>{let S=n;typeof n=="function"&&(S=n(u.current)),u.current=S;let y=l;p&&(y=fe(u.current,k.current),y!==l&&E(y)),d&&P.current(u.current)},[l]),$=a.useCallback(()=>{E(!1),s();const n=j.current;Object.keys(n).forEach(p=>{const d=n[p];d(k.current[p],{shouldDirty:!1,shouldOnChange:!1})}),g.current&&g.current.reset(),T()},[s]),G=a.useCallback((n,p)=>{const d=j.current;return d[n]=p,()=>d[n]=null},[]),M=a.useCallback(n=>(P.current=n,()=>P.current=()=>{})),L=a.useCallback(n=>re(n.target),[]),W=a.useCallback(n=>n?u.current[n]:u.current,[]),U=a.useCallback(()=>k.current,[]),w=a.useCallback(()=>{setTimeout(()=>g.current.dispatchEvent(new Event("input",{bubbles:!0})))},[]),x=a.useCallback(n=>{const p=re(n.target);p?r(n.target.name,p):r(n.target.name,null)},[r]),q=a.useCallback((n,p,{shouldDirty:d=!0,shouldOnChange:S=!0}={})=>{const y=j.current;switch(typeof n){case"string":{const C=y[n];if(!C)throw new Error(`${n} is uncontrolled, please use Controller for it!`);C(p,{shouldDirty:d,shouldOnChange:S}),S&&w()}break;case"object":Object.entries(n).forEach(([C,J])=>{const V=y[C];if(!V)throw new Error(`${C} is uncontrolled, please use Controller for it!`);V(J,{shouldDirty:d,shouldOnChange:S})}),S&&w();default:return}},[w]),F=a.useCallback(()=>{const n=Object.fromEntries(new FormData(g.current)),p=j.current;return Object.keys(p).map(d=>{n[d]=u.current[d]}),o.forEach(d=>n.hasOwnProperty(d)&&(n[d]=+n[d])),n},[]),N=a.useMemo(()=>({setValue:q,instantChange:w,getDefaultValues:U,getFormState:W,setFormState:I,changeError:r,changeErrors:b,clearError:c,clearErrors:s,loadFormValues:F,checkValidity:x,getFieldValidity:L,registerOnchange:M,reload:T,reset:$}),[q,w,U,W,I,r,b,c,F,L,x,M,T,$]);return a.useEffect(()=>{if(g.current){const n=F();k.current=n,u.current=n}return()=>{k.current={},u.current={},E(!1),s()}},[R]),{ref:g,lastReloadedAt:R,isDirty:l,isError:m,errors:f,numberFields:o,subscribe:G,actions:N}};exports.Controller=ie;exports.Form=le;exports.useForm=Ee;exports.useValidateForm=ne;
|