react-simple-formkit 2.3.0 → 2.4.0
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 +102 -67
- package/dist/react-simple-formkit.js +4 -4
- package/dist/react-simple-formkit.mjs +577 -506
- package/dist/react-simple-formkit.umd.js +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# React Simple FormKit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A lightweight library for managing forms as uncontrolled. State updates only when actively watched. Simple and quick to configure with outstanding efficiency.
|
|
4
4
|
|
|
5
5
|
# Table of Contents
|
|
6
6
|
|
|
@@ -8,20 +8,20 @@ Supports managing forms as uncontrolled. State updates only when watched. Simple
|
|
|
8
8
|
- [Features](#features)
|
|
9
9
|
- [Quick start](#quick-start)
|
|
10
10
|
- [Core concepts](#core-concepts)
|
|
11
|
-
- [
|
|
11
|
+
- [Input Field Modes](#input-field-modes)
|
|
12
12
|
- [Watching for updates](#watching-for-updates)
|
|
13
13
|
- [The Power of watch (Unified Observation)](#the-power-of-watch-unified-observation)
|
|
14
|
-
- [
|
|
14
|
+
- [Managing values](#managing-values)
|
|
15
15
|
- [Get value](#get-value)
|
|
16
16
|
- [Set value](#set-value)
|
|
17
17
|
- [Default values and reset](#default-values-and-reset)
|
|
18
|
-
- [
|
|
18
|
+
- [Managing form state](#managing-form-state)
|
|
19
19
|
- [Get form state](#get-form-state)
|
|
20
20
|
- [Set form state](#set-form-state)
|
|
21
|
-
- [
|
|
21
|
+
- [Managing field states](#managing-field-states)
|
|
22
22
|
- [Get field states](#get-field-states)
|
|
23
23
|
- [Set field states](#set-field-states)
|
|
24
|
-
- [
|
|
24
|
+
- [Managing errors](#managing-errors)
|
|
25
25
|
- [Get field error](#get-field-error)
|
|
26
26
|
- [Set field error](#set-field-error)
|
|
27
27
|
- [APIs](#apis)
|
|
@@ -47,14 +47,15 @@ Supports managing forms as uncontrolled. State updates only when watched. Simple
|
|
|
47
47
|
|
|
48
48
|
# Features
|
|
49
49
|
|
|
50
|
-
- **Lightweight**:
|
|
51
|
-
- **Easy setup**:
|
|
52
|
-
- **Optimized for uncontrolled forms**:
|
|
50
|
+
- **Lightweight**: Only ~**15KB**
|
|
51
|
+
- **Easy setup**: Simple and quick configuration
|
|
52
|
+
- **Optimized for uncontrolled forms**: No unnecessary re-renders
|
|
53
53
|
- Comprehensive form management:
|
|
54
|
-
- [
|
|
55
|
-
- [
|
|
56
|
-
- [
|
|
57
|
-
- [
|
|
54
|
+
- [Managing values](#managing-values): get/set values, defaults, reset, ...
|
|
55
|
+
- [Managing form state](#managing-form-state): isDirty, isError, dirtyFields, touchedFields, errorFields, ...
|
|
56
|
+
- [Managing field states](#managing-field-states): isDirty, isTouched, isError, custom states, ...
|
|
57
|
+
- [Managing errors](#managing-errors): form errors, field errors, ...
|
|
58
|
+
- [Managing nested fields](#managing-nested-fields): Supports nested fields with dot notation.
|
|
58
59
|
|
|
59
60
|
# Quick start
|
|
60
61
|
|
|
@@ -79,9 +80,7 @@ return (
|
|
|
79
80
|
|
|
80
81
|
# Core concepts
|
|
81
82
|
|
|
82
|
-
##
|
|
83
|
-
|
|
84
|
-
React Simple FormKit provides three flexible modes to help you balance performance and feature requirements.
|
|
83
|
+
## Input Field Modes
|
|
85
84
|
|
|
86
85
|
### 1. `Uncontrolled`:
|
|
87
86
|
|
|
@@ -89,7 +88,7 @@ React Simple FormKit provides three flexible modes to help you balance performan
|
|
|
89
88
|
|
|
90
89
|
- **Use Cases**:
|
|
91
90
|
- Standard HTML inputs (e.g. `<input>`, `<textarea>`, `<select>`...)
|
|
92
|
-
- UI library components that
|
|
91
|
+
- UI library components that act as simple wrappers around native elements and follow standard browser behavior
|
|
93
92
|
|
|
94
93
|
- **How it works**: The browser manages form by default. However, you still maintain **full visibility**: you can [watch](#watching-for-updates) the entire form state, field states, and values.
|
|
95
94
|
|
|
@@ -99,7 +98,8 @@ React Simple FormKit provides three flexible modes to help you balance performan
|
|
|
99
98
|
|
|
100
99
|
**Best for**: Absolute control over input or complex UIs.
|
|
101
100
|
|
|
102
|
-
**Use Cases**:
|
|
101
|
+
- **Use Cases**:
|
|
102
|
+
- Advanced UI components with complex **INPUTS AND OUTPUTS**. 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.
|
|
103
103
|
|
|
104
104
|
By using `Controller`, you can transform the value in both directions:
|
|
105
105
|
|
|
@@ -116,11 +116,12 @@ Example:
|
|
|
116
116
|
```
|
|
117
117
|
<Controller
|
|
118
118
|
name="multipleSelect"
|
|
119
|
-
render={({ value = "", onChange, name }) => {
|
|
119
|
+
render={({ value = "", onChange, onBlur, name }) => {
|
|
120
120
|
return (
|
|
121
121
|
<Select
|
|
122
122
|
multiple
|
|
123
123
|
name={name}
|
|
124
|
+
onBlur={onBlur}
|
|
124
125
|
value={value.split(",")}
|
|
125
126
|
onChange={(e) => {
|
|
126
127
|
const value = e.target.value;
|
|
@@ -137,46 +138,40 @@ Example:
|
|
|
137
138
|
/>
|
|
138
139
|
```
|
|
139
140
|
|
|
140
|
-
|
|
141
|
+
Example `fieldState`:
|
|
141
142
|
|
|
142
|
-
|
|
143
|
-
|
|
143
|
+
- Only when you get `fieldState` from render props, `Controller` will auto subscribe to fieldState changes.
|
|
144
|
+
- If you need to set custom `fieldState`, you can use [`actions.setFieldState`](#useform).
|
|
144
145
|
|
|
145
|
-
return (
|
|
146
|
-
<Form control={control} onChange={console.log}>
|
|
147
|
-
{/* Uncontrolled */}
|
|
148
|
-
<input name="number1" placeholder="Enter a number" />
|
|
149
|
-
{/* Controlled by Controller. And the value at this field can be controlled by actions.setValue() */}
|
|
150
|
-
<Controller
|
|
151
|
-
name="multipleSelect"
|
|
152
|
-
render={({ value = "", onChange, name }) => {
|
|
153
|
-
return (
|
|
154
|
-
<Select
|
|
155
|
-
multiple
|
|
156
|
-
name={name}
|
|
157
|
-
value={value.split(",")}
|
|
158
|
-
onChange={(e) => {
|
|
159
|
-
const value = e.target.value;
|
|
160
|
-
// value as array by default but on autofill value as string
|
|
161
|
-
onChange(typeof value === "string" ? value : value.join(","));
|
|
162
|
-
}}
|
|
163
|
-
>
|
|
164
|
-
<MenuItem value="10">Ten</MenuItem>
|
|
165
|
-
<MenuItem value="20">Twenty</MenuItem>
|
|
166
|
-
<MenuItem value="30">Thirty</MenuItem>
|
|
167
|
-
</Select>
|
|
168
|
-
);
|
|
169
|
-
}}
|
|
170
|
-
/>
|
|
171
|
-
<button type="button" onClick={() => alert(JSON.stringify(actions.getValues()))}>
|
|
172
|
-
Get value
|
|
173
|
-
</button>
|
|
174
|
-
<button type="submit">Submit</button>
|
|
175
|
-
</Form>
|
|
176
|
-
)
|
|
177
146
|
```
|
|
178
|
-
|
|
179
|
-
|
|
147
|
+
<Controller
|
|
148
|
+
name="multipleSelect"
|
|
149
|
+
render={({ value = "", onChange, onBlur, name, fieldState }) => {
|
|
150
|
+
if(fieldState.hidden) return null
|
|
151
|
+
return (
|
|
152
|
+
<FormControl error={Boolean(fieldState.error)}>
|
|
153
|
+
<Select
|
|
154
|
+
multiple
|
|
155
|
+
name={name}
|
|
156
|
+
onBlur={onBlur}
|
|
157
|
+
value={value.split(",")}
|
|
158
|
+
error
|
|
159
|
+
onChange={(e) => {
|
|
160
|
+
const value = e.target.value;
|
|
161
|
+
// value as array by default but on autofill value as string
|
|
162
|
+
onChange(typeof value === "string" ? value : value.join(","));
|
|
163
|
+
}}
|
|
164
|
+
>
|
|
165
|
+
<MenuItem value="10">Ten</MenuItem>
|
|
166
|
+
<MenuItem value="20">Twenty</MenuItem>
|
|
167
|
+
<MenuItem value="30">Thirty</MenuItem>
|
|
168
|
+
</Select>
|
|
169
|
+
<FormHelperText>{fieldState.error}</FormHelperText>
|
|
170
|
+
</FormControl>
|
|
171
|
+
);
|
|
172
|
+
}}
|
|
173
|
+
/>
|
|
174
|
+
```
|
|
180
175
|
|
|
181
176
|
## Watching for updates
|
|
182
177
|
|
|
@@ -189,6 +184,7 @@ State updates only when observed via `watch()`, `useWatch()`, `subscribe()`, or
|
|
|
189
184
|
- [onBlur](#form) is just a handler callback that is called when field blurred.
|
|
190
185
|
- [actions.subscribe('onChange', callback)](#useform) subscribe onChange callback instead of passing `onChange` in [Form](#form) component.
|
|
191
186
|
- [actions.subscribe('onBlur', callback)](#useform) subscribe onBlur callback instead of passing `onBlur` in [Form](#form) component.
|
|
187
|
+
- [actions.trigger(name)](#actions.trigger) trigger watchers (e.g. `watch`, `useWatch`, `subscribe`) re-update values if needed.
|
|
192
188
|
|
|
193
189
|
> **Rule\*:** By default, onBlur works automatically for uncontrolled fields. However, for controlled fields, you must explicitly pass the onBlur prop when rendering the field.
|
|
194
190
|
|
|
@@ -207,7 +203,7 @@ The `watch` function (and `useWatch`, `subscribe`) is the "brain" of your form.
|
|
|
207
203
|
|
|
208
204
|
> **💡Why it matters\*:** This unified approach ensures you only need to learn one API to track the entire lifecycle of your form.
|
|
209
205
|
|
|
210
|
-
#
|
|
206
|
+
# Managing values
|
|
211
207
|
|
|
212
208
|
## Get value
|
|
213
209
|
|
|
@@ -289,7 +285,7 @@ return (
|
|
|
289
285
|
);
|
|
290
286
|
```
|
|
291
287
|
|
|
292
|
-
#
|
|
288
|
+
# Managing form state
|
|
293
289
|
|
|
294
290
|
## Set form state
|
|
295
291
|
|
|
@@ -317,7 +313,7 @@ console.log(actions.getFormState(["isDirty", "isError"]))
|
|
|
317
313
|
|
|
318
314
|
> **Note\*:** watch(), useWatch(), and subscribe() share the same name format.
|
|
319
315
|
|
|
320
|
-
#
|
|
316
|
+
# Managing field states
|
|
321
317
|
|
|
322
318
|
## Set field states
|
|
323
319
|
|
|
@@ -343,7 +339,7 @@ console.log(actions.getFieldStates(["fieldStates.fieldName.isDirty", "fieldState
|
|
|
343
339
|
|
|
344
340
|
> **Note\*:** watch(), useWatch(), and subscribe() share the same name format.
|
|
345
341
|
|
|
346
|
-
#
|
|
342
|
+
# Managing errors
|
|
347
343
|
|
|
348
344
|
## Get field error
|
|
349
345
|
|
|
@@ -379,6 +375,44 @@ return (
|
|
|
379
375
|
)
|
|
380
376
|
```
|
|
381
377
|
|
|
378
|
+
# Managing Nested Fields
|
|
379
|
+
|
|
380
|
+
## Registering
|
|
381
|
+
|
|
382
|
+
```
|
|
383
|
+
// uncontrolled
|
|
384
|
+
<input name="address.line1" />
|
|
385
|
+
<input name="address.line2" />
|
|
386
|
+
<input name="address.line3" />
|
|
387
|
+
// controlled
|
|
388
|
+
<Controller
|
|
389
|
+
name="address.line4"
|
|
390
|
+
render={({ value = "", onChange }) => {
|
|
391
|
+
return <input value={value} onChange={onChange} />;
|
|
392
|
+
}}
|
|
393
|
+
/>
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
## Watching
|
|
397
|
+
|
|
398
|
+
```
|
|
399
|
+
const addressValue = watch("address")
|
|
400
|
+
const addressLine1 = watch("address.line1")
|
|
401
|
+
const addressLine1Error = watch("errors.address.line1")
|
|
402
|
+
const addressLine1FieldState = watch("fieldStates.address.line1")
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
## Updating
|
|
406
|
+
|
|
407
|
+
```
|
|
408
|
+
const { actions } = useForm()
|
|
409
|
+
actions.setValue("address.line1", "hello")
|
|
410
|
+
actions.setError("address.line1", "error message")
|
|
411
|
+
actions.setFieldState("address.line1", "hidden", true)
|
|
412
|
+
actions.setFieldState("address.line1", "disabled", true)
|
|
413
|
+
actions.setFieldState("address.line1", "custom", { hello: "world" })
|
|
414
|
+
```
|
|
415
|
+
|
|
382
416
|
# APIs
|
|
383
417
|
|
|
384
418
|
## useForm
|
|
@@ -393,13 +427,13 @@ Generic props:
|
|
|
393
427
|
Return:
|
|
394
428
|
|
|
395
429
|
- `control`: contains methods and utilities to control the form.
|
|
396
|
-
- `watch(name)`: `Function` [Example](#
|
|
430
|
+
- `watch(name)`: `Function` [Example](#managing-values)
|
|
397
431
|
- `actions` is an object that contains utilities
|
|
398
432
|
- `actions.reset()`: `(newDefaultValues?: Object, options?: { clearCustomFormStates?: Boolean, clearCustomFieldStates?: Boolean }) => void` [Example](#default-values-and-reset)
|
|
399
|
-
- `actions.getValues()`: `(name?: String | Array) => Object` [Example](#
|
|
400
|
-
- `actions.getErrors()`: `(name?: String | Array) => Object` [Example](#
|
|
401
|
-
- `actions.getFormState()`: `(name?: String | Array) => Object` [Example](#
|
|
402
|
-
- `actions.getFieldStates()`: `(name?: String | Array) => Object` [Example](#
|
|
433
|
+
- `actions.getValues()`: `(name?: String | Array) => Object` [Example](#managing-values)
|
|
434
|
+
- `actions.getErrors()`: `(name?: String | Array) => Object` [Example](#managing-errors)
|
|
435
|
+
- `actions.getFormState()`: `(name?: String | Array) => Object` [Example](#managing-form-state)
|
|
436
|
+
- `actions.getFieldStates()`: `(name?: String | Array) => Object` [Example](#managing-field-states)
|
|
403
437
|
- `actions.setValue()`: `(name: String, value: Any, options?: { shouldDirty?: Boolean, shouldTouched?: Boolean }) => void` [Example](#set-value)
|
|
404
438
|
- `actions.setError()`: `(name: String, error: Any) => void` [Example](#set-field-error)
|
|
405
439
|
- `actions.setFieldState()`: `(name: String, property: String, value: Any) => void` [Example](#set-field-states)
|
|
@@ -413,6 +447,7 @@ Return:
|
|
|
413
447
|
- `actions.subscribeBlur()`: `(callback) => unsubscribe: Function()`
|
|
414
448
|
- `actions.getNumberFields()`: `() => Array`
|
|
415
449
|
- `actions.getDefaultValues()`: `() => Object`
|
|
450
|
+
- `actions.trigger(name)`: `(name: String | Array) => void` trigger watchers (e.g. `watch`, `useWatch`, `subscribe`) re-update values if needed.
|
|
416
451
|
|
|
417
452
|
## Form
|
|
418
453
|
|
|
@@ -442,7 +477,7 @@ Return: everything in render function above
|
|
|
442
477
|
|
|
443
478
|
## useWatch
|
|
444
479
|
|
|
445
|
-
[Example](#
|
|
480
|
+
[Example](#managing-values)
|
|
446
481
|
|
|
447
482
|
Generic props:
|
|
448
483
|
|
|
@@ -490,4 +525,4 @@ Return:
|
|
|
490
525
|
|
|
491
526
|
# Contact
|
|
492
527
|
|
|
493
|
-
For any ideas or issues, please
|
|
528
|
+
For any ideas or issues, please contact me at **anhhuy2000@gmail.com**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("react");var rr={exports:{}},J={};/**
|
|
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 lr;function kr(){if(lr)return J;lr=1;var n=Symbol.for("react.transitional.element"),t=Symbol.for("react.fragment");function o(l,a,b){var c=null;if(b!==void 0&&(c=""+b),a.key!==void 0&&(c=""+a.key),"key"in a){b={};for(var h in a)h!=="key"&&(b[h]=a[h])}else b=a;return a=b.ref,{$$typeof:n,type:l,key:c,ref:a!==void 0?a:null,props:b}}return J.Fragment=t,J.jsx=o,J.jsxs=o,J}var z={};/**
|
|
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 ir;function yr(){return ir||(ir=1,process.env.NODE_ENV!=="production"&&function(){function n(r){if(r==null)return null;if(typeof r=="function")return r.$$typeof===sr?null:r.displayName||r.name||null;if(typeof r=="string")return r;switch(r){case S:return"Fragment";case p:return"Profiler";case T:return"StrictMode";case H:return"Suspense";case Y:return"SuspenseList";case tr:return"Activity"}if(typeof r=="object")switch(typeof r.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),r.$$typeof){case C:return"Portal";case M:return(r.displayName||"Context")+".Provider";case N:return(r._context.displayName||"Context")+".Consumer";case I:var m=r.render;return r=r.displayName,r||(r=m.displayName||m.name||"",r=r!==""?"ForwardRef("+r+")":"ForwardRef"),r;case v:return m=r.displayName||null,m!==null?m:n(r.type)||"Memo";case L:m=r._payload,r=r._init;try{return n(r(m))}catch{}}return null}function t(r){return""+r}function o(r){try{t(r);var m=!1}catch{m=!0}if(m){m=console;var k=m.error,_=typeof Symbol=="function"&&Symbol.toStringTag&&r[Symbol.toStringTag]||r.constructor.name||"Object";return k.call(m,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",_),t(r)}}function l(r){if(r===S)return"<>";if(typeof r=="object"&&r!==null&&r.$$typeof===L)return"<...>";try{var m=n(r);return m?"<"+m+">":"<...>"}catch{return"<...>"}}function a(){var r=U.A;return r===null?null:r.getOwner()}function b(){return Error("react-stack-top-frame")}function c(r){if(P.call(r,"key")){var m=Object.getOwnPropertyDescriptor(r,"key").get;if(m&&m.isReactWarning)return!1}return r.key!==void 0}function h(r,m){function k(){W||(W=!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)",m))}k.isReactWarning=!0,Object.defineProperty(r,"key",{get:k,configurable:!0})}function i(){var r=n(this.type);return X[r]||(X[r]=!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.")),r=this.props.ref,r!==void 0?r:null}function E(r,m,k,_,x,D,q,e){return k=D.ref,r={$$typeof:w,type:r,key:m,props:D,_owner:x},(k!==void 0?k:null)!==null?Object.defineProperty(r,"ref",{enumerable:!1,get:i}):Object.defineProperty(r,"ref",{enumerable:!1,value:null}),r._store={},Object.defineProperty(r._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(r,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(r,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:q}),Object.defineProperty(r,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:e}),Object.freeze&&(Object.freeze(r.props),Object.freeze(r)),r}function f(r,m,k,_,x,D,q,e){var s=m.children;if(s!==void 0)if(_)if(nr(s)){for(_=0;_<s.length;_++)R(s[_]);Object.freeze&&Object.freeze(s)}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 R(s);if(P.call(m,"key")){s=n(r);var d=Object.keys(m).filter(function(O){return O!=="key"});_=0<d.length?"{key: someKey, "+d.join(": ..., ")+": ...}":"{key: someKey}",K[s+_]||(d=0<d.length?"{"+d.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} />`,F,T,P,T),re[T+F]=!0)}if(T=null,S!==void 0&&(h(S),T=""+S),d(f)&&(h(f.key),T=""+f.key),"key"in f){S={};for(var D in f)D!=="key"&&(S[D]=f[D])}else S=f;return T&&C(S,typeof e=="function"?e.displayName||e.name||"Unknown":e),b(e,T,O,x,g(),S,q,J)}function c(e){typeof e=="object"&&e!==null&&e.$$typeof===i&&e._store&&(e._store.validated=1)}var l=t,i=Symbol.for("react.transitional.element"),n=Symbol.for("react.portal"),E=Symbol.for("react.fragment"),A=Symbol.for("react.strict_mode"),N=Symbol.for("react.profiler"),Y=Symbol.for("react.consumer"),I=Symbol.for("react.context"),se=Symbol.for("react.forward_ref"),ne=Symbol.for("react.suspense"),ue=Symbol.for("react.suspense_list"),oe=Symbol.for("react.memo"),j=Symbol.for("react.lazy"),X=Symbol.for("react.activity"),B=Symbol.for("react.client.reference"),W=l.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,L=Object.prototype.hasOwnProperty,Z=Array.isArray,U=console.createTask?console.createTask:function(){return null};l={"react-stack-bottom-frame":function(e){return e()}};var Q,K={},ee=l["react-stack-bottom-frame"].bind(l,k)(),_=U(R(k)),re={};G.Fragment=E,G.jsx=function(e,f,S,F,x){var O=1e4>W.recentlyCreatedOwnerStacks++;return u(e,f,S,!1,F,x,O?Error("react-stack-top-frame"):ee,O?U(R(e)):_)},G.jsxs=function(e,f,S,F,x){var O=1e4>W.recentlyCreatedOwnerStacks++;return u(e,f,S,!0,F,x,O?Error("react-stack-top-frame"):ee,O?U(R(e)):_)}}()),G}var de;function ve(){return de||(de=1,process.env.NODE_ENV==="production"?te.exports=Se():te.exports=ye()),te.exports}var be=ve();const y=()=>{},me=t.createContext({ref:null,watch:y,actions:{reset:y,resetField:y,setValue:y,getValues:y,getErrors:y,getFieldStates:y,getFormStates:y,setError:y,clearError:y,clearErrors:y,checkValidity:y,getNumberFields:y,getFieldValidity:y,getDefaultValues:y,setFieldState:y,resetFieldState:y,getControlledFields:y},registerController:y,registerHookWatcher:y,lastReloadedAt:y,loadFormValues:y,getWatchValue:y,channels:{}}),le=()=>t.useContext(me),pe=({id:s,control:o,method:h,action:R,children:g,onChange:k,onBlur:d,onSubmit:C=()=>{},onInput:v=()=>{},onReset:b=()=>{},numberFields:u=[],className:c,...l})=>{const i=t.useCallback(n=>{o.ref&&(o.ref.current=n,o.ref.current&&o.initForm())},[o]);return t.useEffect(()=>{let n=()=>{},E=()=>{};return k&&(n=o.channels.subscribe("onChange",k)),d&&(E=o.channels.subscribe("onBlur",d)),()=>{n(),E()}},[o.lastReloadedAt]),be.jsx(me.Provider,{value:o,children:be.jsx("form",{id:s,ref:i,action:R,method:h,className:c,onInput:v,onSubmit:n=>{h||n.preventDefault();const E=o.loadFormValues();C(E)},onChange:n=>{const E=n.target.name;!E||o.actions.getControlledFields().has(E)||o.actions.setValue(E,n.target.value)},onBlur:n=>{const E=n.target.name;if(!E||o.actions.getControlledFields().has(E))return;const A=n.target.value;o.channels.publish("onBlur",E,A,o.actions.getValues())},onReset:n=>{o.actions.reset(),b(n)},...l,children:g},o.lastReloadedAt)})},Re=({control:s,name:o,compute:h})=>{const{getWatchValue:R,registerHookWatcher:g}=s||le(),k=R({name:o,compute:h}),[d,C]=t.useState(k);return t.useEffect(()=>g({name:o,compute:h,value:d,setValue:C}),[]),d},ke=({name:s,defaultValue:o})=>{const{actions:h,registerController:R,channels:g}=le(),k=t.useRef(),d=h.getDefaultValues()[s]||o||"",[C,v]=t.useState(d),b=Re({name:`fieldStates.${s}`}),u=t.useCallback((l,{shouldDirty:i=!0,shouldOnChange:n=!0}={})=>{var A,N;let E=l;(A=l==null?void 0:l.target)!=null&&A.value&&(E=l.target.value),(N=l==null?void 0:l.target)!=null&&N.checked&&(E=l.target.checked+""),v(E),h.setValue(s,E,{shouldDirty:i,shouldOnChange:n})},[h.setValue]),c=t.useCallback(l=>{const i=h.getValues(),n=l??i[s];g.publish("onBlur",s,n,i)},[]);return t.useEffect(()=>R(s,v),[]),{ref:k,name:s,defaultValue:d,value:C,onChange:u,onBlur:c,fieldState:b}},Fe=({name:s,defaultValue:o,render:h=({ref:R,name:g,defaultValue:k,value:d,onChange:C,onBlur:v,fieldState:b})=>null})=>{const R=ke({name:s,defaultValue:o});return h(R)},Te=["errors","fieldStates","formState"],H=(s={},o="")=>o.split(".").reduce((h,R)=>h&&h[R]!==void 0?h[R]:void 0,s),Ee=(s={},o="",h)=>{const R={...s};let g=s;const k=o.split(".");return k.forEach((d,C)=>{C<k.length-1?(g[d]||(g[d]={}),R[d]={...g[d]},g=R[d]):g[d]=h}),R},V=s=>Te.some(o=>s.startsWith(o))?s:"values."+s,_e=["badInput","customError","patternMismatch","rangeOverflow","rangeUnderflow","stepMismatch","tooLong","tooShort","typeMismatch","valueMissing"],ge=s=>{typeof s.setCustomValidity=="function"&&s.setCustomValidity("");let o=s.validity,h=_e.find(R=>o[R]);if(h)return{type:h,message:s.validationMessage}},we=()=>{const s=t.useRef(new Map),o=t.useRef(new Map),h=t.useCallback(()=>o.current,[]),R=t.useCallback(()=>s.current,[]),g=t.useCallback(()=>{s.current.clear(),o.current.clear()},[]),k=t.useCallback((b,...u)=>{const c=s.current.get(b),l=o.current.get(b);c&&c.forEach(i=>i(...u)),l&&l.forEach(i=>i(...u)),s.current.forEach((i,n)=>{n!==b&&b.startsWith(n)&&i.forEach(E=>E(...u))}),o.current.forEach((i,n)=>{n!==b&&b.startsWith(n)&&i.forEach(E=>E(...u))})},[]),d=t.useCallback(b=>{s.current.forEach((u,c)=>{c.startsWith(b)&&u.forEach(l=>l())}),o.current.forEach((u,c)=>{c.startsWith(b)&&u.forEach(l=>l())})},[]),C=t.useCallback((b,u)=>(s.current.has(b)||s.current.set(b,new Set),s.current.get(b).add(u),()=>{var c;return(c=s.current.get(b))==null?void 0:c.delete(u)}),[]),v=t.useCallback((b,u)=>{o.current.has(b)||(o.current.set(b,new Set),o.current.get(b).add(u))},[]);return{reset:g,publish:k,subscribe:C,subscribeWatch:v,getEvents:R,getWatchEvents:h,trigger:d}},Ae=()=>{const[s,o]=t.useState(),h=t.useCallback(()=>o(new Date().toString()),[]);return[s,h]},Oe=()=>{const[,s]=t.useState({});return t.useCallback(()=>s({}),[])},Pe=({channels:s,getValues:o,getErrors:h,getFieldStates:R,getFormState:g})=>{const k=Oe(),d=t.useCallback(({name:u,compute:c})=>{if(typeof c=="function")return c(o());if(Array.isArray(u)){const l={};return u.forEach(i=>{l[i]=d({name:i})}),l}return H({...o(),errors:{...h()},formState:{...g()},fieldStates:{...R()}},u)},[]),C=t.useCallback(u=>{if(!u)return s.subscribeWatch("values",()=>k()),o();if(typeof u=="string"){const c=V(u);return s.subscribeWatch(c,()=>k()),d({name:u})}if(Array.isArray(u)){const c={};return u.forEach(l=>{const i=V(l);s.subscribeWatch(i,()=>k()),c[l]=d({name:l})}),{...c}}throw new Error("Parameters of watch must be string or array of string")},[]),v=t.useCallback(({name:u,compute:c,setValue:l})=>{if(typeof c=="function")return s.subscribe("values",()=>{const n=d({compute:c});l(n)});if(!u)return s.subscribe("values",()=>{l(o())});if(typeof u=="string"){const i=V(u);return s.subscribe(i,()=>{const E=d({name:u});l(E)})}if(Array.isArray(u)){const i=d({name:u}),n=[];return u.forEach(E=>{const A=V(E),N=s.subscribe(A,()=>{const Y=d({name:E});i[E]=Y,l({...i})});n.push(N)}),()=>n.forEach(E=>E())}throw new Error("Parameters of name must be string or array of string or compute must be a function")},[]),b=t.useCallback((u,c)=>{if(!u)return s.subscribe("values",()=>c(o()));if(["onChange","onBlur"].includes(u))return s.subscribe(u,c);if(typeof u=="string"){const l=V(u);return s.subscribe(l,()=>c(d({name:u})))}if(Array.isArray(u)){const l={},i=[];return u.forEach(n=>{const E=V(n),A=s.subscribe(E,()=>{l[n]=d({name:n}),c({...l})});i.push(A)}),()=>i.forEach(n=>n())}throw new Error("Parameters of name must be string or array of string")},[]);return{watch:C,registerHookWatcher:v,getWatchValue:d,subscribe:b}},he={lastReset:null,isDirty:!1,isError:!1,errorFields:[],dirtyFields:[],touchedFields:[]},ce={isDirty:!1,isTouched:!1,error:null},je=[],xe={},Ne=({numberFields:s=je,defaultValues:o=xe,shouldUnRegister:h=!1,shouldConvertNumber:R=!1}={})=>{const[g,k]=Ae(),d=t.useRef(),C=t.useRef(s),v=t.useRef(new Map),b=t.useRef({}),u=t.useRef({}),c=t.useRef({...he}),l=t.useRef({...o}),i=t.useRef({...o}),n=we(),E=t.useCallback(r=>{u.current=Object.keys(i.current).reduce((a,m)=>({...a,[m]:{...r?{}:u.current[m]||{},...ce}}),{})},[]),A=t.useCallback((r={},{clearCustomFormStates:a=!1,clearCustomFieldStates:m=!1}={})=>{i.current={...i.current,...r},l.current={...i.current},v.current.forEach((p,$)=>{p(i.current[$]??"")}),b.current={},c.current={...a?{}:c.current,...he},C.current=s,v.current=new Map,n.reset(),E(m),s.length>0&&(R=!0),k()},[]),N=t.useCallback(()=>{const r=Object.fromEntries(new FormData(d.current));return v.current.forEach((m,p)=>{r[p]=l.current[p]}),R&&C.current.forEach(m=>r.hasOwnProperty(m)&&(r[m]=Number(r[m])||0)),{...l.current,...r}},[]),Y=t.useCallback(()=>!!Object.values(W()).find(r=>r.isDirty),[]),I=t.useCallback(()=>!!Object.values(B()).find(r=>!!r),[]),se=t.useCallback(r=>ge(r.target),[]),ne=t.useCallback(()=>i.current,[]),ue=t.useCallback(()=>C.current,[]),oe=t.useCallback(()=>v.current,[]),j=t.useCallback((r,a)=>a?Array.isArray(a)?a.reduce((m,p)=>({...m,[p]:H(r,p)}),{}):H(r,a):{...r},[]),X=t.useCallback(r=>j(l.current,r),[j]),B=t.useCallback(r=>j(b.current,r),[j]),W=t.useCallback(r=>j(u.current,r),[j]),L=t.useCallback(r=>j({...c.current,lastReset:g},r),[g]),{watch:Z,registerHookWatcher:U,getWatchValue:Q,subscribe:K}=Pe({channels:n,getValues:X,getErrors:B,getFieldStates:W,getFormState:L}),ee=t.useCallback((r,a)=>{const m=c.current;H(m,r)!==a&&(c.current=Ee(c.current,r,a),n.publish(`formState.${r}`,a))},[]),_=t.useCallback((r,a,m)=>{const p=u.current[r]||{...ce};H(p,a)!==m&&(u.current=Ee(u.current,`${r}.${a}`,m),n.publish(`fieldStates.${r}.${a}`,m),a==="error"&&(c.current.errorFields=Object.keys(b.current).filter(w=>!!b.current[w]),n.publish("formState.errorFields",c.current.errorFields)),a==="isDirty"&&(c.current.dirtyFields=Object.keys(u.current).filter(w=>u.current[w].isDirty),n.publish("formState.dirtyFields",c.current.dirtyFields)),a==="isTouched"&&(c.current.touchedFields=Object.keys(u.current).filter(w=>u.current[w].isTouched),n.publish("formState.touchedFields",c.current.touchedFields)))},[]),re=t.useCallback(r=>{_(r,"isDirty",!1),_(r,"isTouched",!1),_(r,"error",null)},[]),e=t.useCallback((r,a)=>{if(b.current[r]===a)return;b.current[r]=a,_(r,"error",a),n.publish(`errors.${r}`,a);const p=I();c.current.isError!==p&&(c.current.isError=p,n.publish("formState.isError",p))},[]),f=t.useCallback(r=>{if(!b.current[r])return;b.current[r]=null,_(r,"error",null),n.publish(`errors.${r}`,null);const a=I();c.current.isError!==a&&(c.current.isError=a,n.publish("formState.isError",a))},[]),S=t.useCallback(()=>{if(Object.keys(b.current)===0)return;Object.keys(b.current).forEach(a=>f(a)),b.current={};const r=I();c.current.isError!==r&&(c.current.isError=r,n.publish("formState.isError",r))},[]),F=t.useCallback(r=>{const a=ge(r.target);a?e(r.target.name,a):f(r.target.name)},[]),x=t.useCallback((r,a,{shouldDirty:m=!0,shouldOnChange:p=!0}={})=>{if(a===l.current[r])return;R&&C.current.includes(r)&&(a=Number(a)||void 0),_(r,"isTouched",!0);const $=a!==i.current[r];m&&u.current[r].isDirty!==$&&_(r,"isDirty",$);const w=Y();c.current.isDirty!==w&&(c.current.isDirty=w,n.publish("formState.isDirty",w)),l.current[r]=a;const M=v.current.get(r);M&&M(a),n.publish(`values.${r}`,a,{shouldDirty:m,shouldOnChange:p}),p&&n.publish("onChange",r,a,l.current)},[]),O=t.useCallback((r,a)=>(v.current.set(r,a),u.current[r]={...ce},()=>{h&&v.current.delete(r)}),[]),q=t.useCallback((r,{keepError:a,keepDirty:m,keepTouched:p,keepCustomState:$,defaultValue:w})=>{const M=v.current.get(r);if(!M)throw new Error(`Cannot reset "${r}" because it's uncontrolled field`);i.current[r]=w,l.current[r]=w,M&&M(w),a||f(r),m||_(r,"isDirty",!1),p||_(r,"isTouched",!1)},[]),J=t.useCallback(r=>{if(!r)return n.trigger("values");if(Array.isArray(r))return r.forEach(m=>{const p=V(m);n.trigger(p)});const a=V(r);n.trigger(a)},[]),T=t.useCallback((r,a=P.getValues(r))=>{n.publish("onBlur",r,a,P.getValues())},[]),P=t.useMemo(()=>({subscribe:K,reset:A,trigger:J,reload:k,resetField:q,setValue:x,getValues:X,getErrors:B,getFieldStates:W,getFormState:L,setError:e,clearError:f,clearErrors:S,checkValidity:F,getNumberFields:ue,getFieldValidity:se,getDefaultValues:ne,setFieldState:_,triggerFieldBlur:T,resetFieldState:re,getControlledFields:oe,setFormState:ee,getEvents:n.getEvents,getWatchEvents:n.getWatchEvents}),[g]),D=t.useCallback(()=>{[...d.current.querySelectorAll("[name]")].forEach(m=>{!m.defaultValue&&!v.current.has(m.name)&&i.current[m.name]&&(m.defaultValue=i.current[m.name]),m.type==="number"&&!C.current.includes(m.name)&&C.current.push(m.name)});const a=N();l.current={...a},i.current={...a}},[]),ae=t.useMemo(()=>({ref:d,watch:Z,actions:P,initForm:D,registerController:O,registerHookWatcher:U,lastReloadedAt:g,loadFormValues:N,getWatchValue:Q,channels:n}),[g]),Ce=t.useMemo(()=>({control:ae,actions:P,watch:Z}),[g]);return t.useLayoutEffect(()=>{d.current&&D()},[g]),t.useLayoutEffect(()=>{E(!1)},[]),Ce};exports.Controller=Fe;exports.Form=pe;exports.useController=ke;exports.useForm=Ne;exports.useFormContext=le;exports.useWatch=Re;
|
|
22
|
+
<%s key={someKey} {...props} />`,_,s,d,s),K[s+_]=!0)}if(s=null,k!==void 0&&(o(k),s=""+k),c(m)&&(o(m.key),s=""+m.key),"key"in m){k={};for(var g in m)g!=="key"&&(k[g]=m[g])}else k=m;return s&&h(k,typeof r=="function"?r.displayName||r.name||"Unknown":r),E(r,s,D,x,a(),k,q,e)}function R(r){typeof r=="object"&&r!==null&&r.$$typeof===w&&r._store&&(r._store.validated=1)}var y=u,w=Symbol.for("react.transitional.element"),C=Symbol.for("react.portal"),S=Symbol.for("react.fragment"),T=Symbol.for("react.strict_mode"),p=Symbol.for("react.profiler"),N=Symbol.for("react.consumer"),M=Symbol.for("react.context"),I=Symbol.for("react.forward_ref"),H=Symbol.for("react.suspense"),Y=Symbol.for("react.suspense_list"),v=Symbol.for("react.memo"),L=Symbol.for("react.lazy"),tr=Symbol.for("react.activity"),sr=Symbol.for("react.client.reference"),U=y.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,P=Object.prototype.hasOwnProperty,nr=Array.isArray,B=console.createTask?console.createTask:function(){return null};y={"react-stack-bottom-frame":function(r){return r()}};var W,X={},Z=y["react-stack-bottom-frame"].bind(y,b)(),Q=B(l(b)),K={};z.Fragment=S,z.jsx=function(r,m,k,_,x){var D=1e4>U.recentlyCreatedOwnerStacks++;return f(r,m,k,!1,_,x,D?Error("react-stack-top-frame"):Z,D?B(l(r)):Q)},z.jsxs=function(r,m,k,_,x){var D=1e4>U.recentlyCreatedOwnerStacks++;return f(r,m,k,!0,_,x,D?Error("react-stack-top-frame"):Z,D?B(l(r)):Q)}}()),z}var fr;function vr(){return fr||(fr=1,process.env.NODE_ENV==="production"?rr.exports=kr():rr.exports=yr()),rr.exports}var dr=vr();const F=()=>{},gr=u.createContext({ref:null,watch:F,actions:{reset:F,resetField:F,setValue:F,getValues:F,getErrors:F,getFieldStates:F,getFormStates:F,setError:F,clearError:F,clearErrors:F,checkValidity:F,getNumberFields:F,getFieldValidity:F,getDefaultValues:F,setFieldState:F,resetFieldState:F,getControlledFields:F},registerController:F,registerHookWatcher:F,lastReloadedAt:F,loadFormValues:F,getWatchValue:F,channels:{}}),ar=()=>u.useContext(gr),Fr=({id:n,control:t,method:o,action:l,children:a,onChange:b,onBlur:c,onSubmit:h=()=>{},onInput:i=()=>{},onReset:E=()=>{},numberFields:f=[],className:R,...y})=>{const w=u.useCallback(C=>{t.ref&&(t.ref.current=C,t.ref.current&&t.initForm())},[t]);return u.useEffect(()=>{let C=()=>{},S=()=>{};return b&&(C=t.channels.subscribe("onChange",b)),c&&(S=t.channels.subscribe("onBlur",c)),()=>{C(),S()}},[t.lastReloadedAt]),dr.jsx(gr.Provider,{value:t,children:dr.jsx("form",{id:n,ref:w,action:l,method:o,className:R,onInput:i,onSubmit:C=>{o||C.preventDefault();const S=t.loadFormValues();h(S)},onChange:C=>{const S=C.target.name;!S||t.actions.getControlledFields().has(S)||t.actions.setValue(S,C.target.value)},onBlur:C=>{const S=C.target.name;if(!S||t.actions.getControlledFields().has(S))return;const T=C.target.value;t.channels.publish("onBlur",S,T,t.actions.getValues())},onReset:C=>{t.actions.reset(),E(C)},...y,children:a},t.lastReloadedAt)})},cr={isDirty:!1,isTouched:!1,error:null},br={lastReset:null,isDirty:!1,isError:!1,errorFields:[],dirtyFields:[],touchedFields:[]},pr={},A=(n={},t="")=>t.split(".").reduce((o,l)=>o&&o[l]!==void 0?o[l]:void 0,n),V=(n={},t="",o)=>{const l={...n};let a=l;const b=t.split(".");return b.forEach((c,h)=>{h<b.length-1?(a[c]=a[c]?{...a[c]}:{},a=a[c]):a[c]=o}),l},G=(n,t)=>{const o=A(n,t);return typeof o=="object"&&!Array.isArray(o)},Tr=["errors","fieldStates","formState"],$=n=>Tr.some(t=>n.startsWith(t))?n:"values."+n,_r=["badInput","customError","patternMismatch","rangeOverflow","rangeUnderflow","stepMismatch","tooLong","tooShort","typeMismatch","valueMissing"],Er=n=>{typeof n.setCustomValidity=="function"&&n.setCustomValidity("");let t=n.validity,o=_r.find(l=>t[l]);if(o)return{type:o,message:n.validationMessage}},wr=({control:n,name:t,compute:o})=>{const{getWatchValue:l,registerHookWatcher:a}=n||ar(),b=l({name:t,compute:o}),[c,h]=u.useState(b);return u.useEffect(()=>a({name:t,compute:o,value:c,setValue:h}),[]),c},hr=({control:n,name:t,defaultValue:o})=>{const{actions:l,registerController:a,channels:b,getWatchValue:c}=n||ar(),h=u.useRef(),i=u.useRef(),E=A(l.getDefaultValues(),t)||o||"",[f,R]=u.useState(E),[y,w]=u.useState({}),C=u.useCallback((T,{shouldDirty:p=!0,shouldOnChange:N=!0}={})=>{var I;let M=T;((I=T==null?void 0:T.target)==null?void 0:I.value)!==void 0&&(M=T.target.value),R(M),l.setValue(t,M,{shouldDirty:p,shouldOnChange:N})},[l.setValue]),S=u.useCallback(T=>{const p=l.getValues(),N=T??A(p,t);b.publish("onBlur",t,N,p)},[]);return u.useEffect(()=>{const T=a(t,R);return()=>{T(),i.current&&i.current()}},[]),new Proxy({ref:h,name:t,defaultValue:E,value:f,onChange:C,onBlur:S,fieldState:y},{get(T,p,N){return typeof p=="string"&&p==="fieldState"&&(i.current&&i.current(),i.current=l.subscribe(`fieldStates.${t}`,()=>{w(c({name:`fieldStates.${t}`}))})),Reflect.get(T,p,N)}})},Ar=({name:n,control:t,defaultValue:o,render:l=({ref:a,name:b,defaultValue:c,value:h,onChange:i,onBlur:E,fieldState:f})=>null})=>{const a=hr({name:n,defaultValue:o,control:t});return l(a)},Or=()=>{const[n,t]=u.useState(),o=u.useCallback(()=>t(new Date().toString()),[]);return[n,o]},jr=()=>{const[,n]=u.useState({});return u.useCallback(()=>n({}),[])},Pr=({channels:n,getWatchValue:t})=>{const o=jr(),l=u.useCallback(c=>{if(!c)return n.subscribeWatch("values",()=>o()),t();if(typeof c=="string"){const h=$(c);n.subscribeWatch(h,(i,E)=>{i!==E&&o()})}return Array.isArray(c)&&c.forEach(h=>{const i=$(h);n.subscribeWatch(i,(E,f)=>{E!==f&&o()})}),t({name:c})},[]),a=u.useCallback(({name:c,compute:h,setValue:i})=>{if(typeof h=="function")return n.subscribe("values",()=>{const f=t({compute:h});i(f)});if(!c)return n.subscribe("values",()=>{i(t())});if(typeof c=="string"){const E=$(c);return n.subscribe(E,()=>{const R=t({name:c});i(R)})}if(Array.isArray(c)){const E=[];return c.forEach(f=>{const R=$(f),y=n.subscribe(R,()=>{const w=t({name:c});i(w)});E.push(y)}),()=>E.forEach(f=>f())}throw new Error("Parameters of name must be string or array of string or compute must be a function")},[]),b=u.useCallback((c,h)=>{if(!c)return n.subscribe("values",()=>h(t()));if(["onChange","onBlur"].includes(c))return n.subscribe(c,h);if(typeof c=="string"){const i=$(c);return n.subscribe(i,()=>h(t({name:c})))}if(Array.isArray(c)){const i=[];return c.forEach(E=>{const f=$(E),R=n.subscribe(f,()=>{h(t({name:c}))});i.push(R)}),()=>i.forEach(E=>E())}throw new Error("Parameters of name must be string or array of string")},[]);return{watch:l,registerHookWatcher:a,getWatchValue:t,subscribe:b}},Dr=({getWatchValue:n})=>{const t=u.useRef(new Map),o=u.useRef(new Map),l=u.useCallback(()=>o.current,[]),a=u.useCallback(()=>t.current,[]),b=u.useCallback(()=>{t.current.clear(),o.current.clear()},[]),c=u.useCallback((f,...R)=>{const y=t.current.get(f),w=o.current.get(f);y&&y.forEach(C=>C(...R)),w&&w.forEach(C=>C(...R)),t.current.forEach((C,S)=>{if(S!==f&&f.startsWith(S)){const T=n({name:S.replace("values.","").replace("values","")});C.forEach(p=>p(V(T,f.replace(`${S}.`,""),R[0])))}}),o.current.forEach((C,S)=>{if(S!==f&&f.startsWith(S)){const T=n({name:S.replace("values.","").replace("values","")});C.forEach(p=>p(V(T,f.replace(`${S}.`,""),R[0])))}})},[]),h=u.useCallback(f=>{t.current.forEach((R,y)=>{y.startsWith(f)&&R.forEach(w=>w(n({name:y.replace("values.","").replace("values","")})))}),o.current.forEach((R,y)=>{y.startsWith(f)&&R.forEach(w=>w(n({name:y.replace("values.","").replace("values","")})))})},[]),i=u.useCallback((f,R)=>(t.current.has(f)||t.current.set(f,new Set),t.current.get(f).add(R),()=>{var y;return(y=t.current.get(f))==null?void 0:y.delete(R)}),[]),E=u.useCallback((f,R)=>{o.current.has(f)||(o.current.set(f,new Set),o.current.get(f).add(R))},[]);return{reset:b,publish:c,subscribe:i,subscribeWatch:E,getEvents:a,getWatchEvents:l,trigger:h}},mr=(n,t={},o="")=>!!Object.keys(t).find(l=>{const a=t[l];return G(n,o+l)?mr(n,a,o+l+"."):!!a.isDirty}),er=(n,t={},o="")=>!!Object.keys(t).find(l=>{const a=t[l];return G(n,o+l)?er(n,a,o+l+"."):!!a}),Rr=(n,t={},o="")=>Object.keys(t).reduce((l,a)=>{const b=t[a];return G(n,o+a)?[...l,...Rr(n,b,o+a+".")]:b.isDirty?[...l,o+a]:l},[]),Sr=(n,t={},o="")=>Object.keys(t).reduce((l,a)=>{const b=t[a];return G(n,o+a)?[...l,...Sr(n,b,o+a+".")]:b.isTouched?[...l,o+a]:l},[]),Cr=(n,t={},o="")=>Object.keys(t).reduce((l,a)=>{const b=t[a];return G(n,o+a)?[...l,...Cr(n,b,o+a+".")]:b?[...l,o+a]:l},[]),Nr=({defaultValues:n=pr,shouldUnRegister:t=!1}={})=>{const[o,l]=Or(),a=u.useRef(),b=u.useRef(new Map),c=u.useRef({}),h=u.useRef({}),i=u.useRef({...br}),E=u.useRef({...n}),f=u.useRef({...n}),R=u.useCallback((e,s=f.current)=>Object.keys(s).reduce((d,g)=>{if(typeof s[g]=="object")return{...d,[g]:R(e,s[g])};const O={...e?{}:h.current[g]||{},...cr};return{...d,[g]:O}},{}),[]),y=u.useCallback((e={},{clearCustomFormStates:s=!1,clearCustomFieldStates:d=!1}={})=>{f.current={...f.current,...e},E.current={...f.current},b.current.forEach((g,O)=>{g(A(f.current,O)??"")}),c.current={},i.current={...s?{}:i.current,...br},b.current=new Map,v.reset(),h.current=R(d),l()},[]),w=u.useCallback(()=>{let e=Object.fromEntries(new FormData(a.current));b.current.forEach((d,g)=>{e=V(e,g,A(E.current,g))});let s={...E.current,...e};return Object.keys(s).forEach(d=>{if(d.includes(".")){const g=A(s,d);s=V(s,d,g||""),delete s[d]}}),s},[]),C=u.useCallback(e=>Er(e.target),[]),S=u.useCallback(()=>f.current,[]),T=u.useCallback(()=>b.current,[]),p=u.useCallback((e,s)=>s?Array.isArray(s)?s.reduce((d,g)=>({...d,[g]:A(e,g)}),{}):A(e,s):{...e},[]),N=u.useCallback(e=>p(E.current,e),[p]),M=u.useCallback(e=>p(c.current,e),[p]),I=u.useCallback(e=>p(h.current,e),[p]),H=u.useCallback(e=>p({...i.current,lastReset:o},e),[o]),Y=u.useCallback(({name:e,compute:s}={})=>{if(!e&&!s||e==="values")return N();if(typeof s=="function")return s(N());if(Array.isArray(e)){const d={};return e.forEach(g=>{d[g]=Y({name:g})}),d}return A({...E.current,errors:c.current,fieldStates:h.current,formState:H()},e)},[]),v=Dr({getWatchValue:Y}),{watch:L,registerHookWatcher:tr,subscribe:sr}=Pr({getWatchValue:Y,channels:v}),U=u.useCallback((e,s)=>{const d=i.current,g=A(d,e);g!==s&&(i.current=V(i.current,e,s),v.publish(`formState.${e}`,s,g))},[]),P=u.useCallback((e,s,d)=>{try{const g=A(h.current,e)||{...cr},O=A(g,s);if(O!==d){if(h.current=V(h.current,`${e}.${s}`,d),v.publish(`fieldStates.${e}.${s}`,d,O),s==="error"){const j=Cr(E.current,c.current);i.current.errorFields=j,v.publish("formState.errorFields",j)}if(s==="isDirty"){const j=Rr(E.current,h.current);i.current.dirtyFields=j,v.publish("formState.dirtyFields",j)}if(s==="isTouched"){const j=Sr(E.current,h.current);i.current.touchedFields=j,v.publish("formState.touchedFields",j)}}}catch(g){console.log(g)}},[]),nr=u.useCallback(e=>{P(e,"isDirty",!1),P(e,"isTouched",!1),P(e,"error",null)},[]),B=u.useCallback((e,s)=>{try{const d=A(c.current,e);if(d===s)return;c.current=V(c.current,e,s),v.publish(`errors.${e}`,s,d),P(e,"error",s);const g=er(E.current,c.current);if(i.current.isError!==g){const O=i.current.isError;i.current.isError=g,v.publish("formState.isError",g,O)}}catch(d){console.error(d)}},[]),W=u.useCallback(e=>{try{const s=A(c.current,e);if(!s)return;c.current=V(c.current,e,null),v.publish(`errors.${e}`,null,s),P(e,"error",null);const d=er(E.current,c.current);if(i.current.isError!==d){const g=i.current.isError;i.current.isError=d,v.publish("formState.isError",d,g)}}catch(s){console.error(s)}},[]),X=u.useCallback(()=>{if(!i.current.isError)return;Object.keys(c.current).forEach(s=>W(s)),c.current={};const e=er(E.current,c.current);if(i.current.isError!==e){const s=i.current.isError;i.current.isError=e,v.publish("formState.isError",e,s)}},[]),Z=u.useCallback(e=>{const s=Er(e.target);s?B(e.target.name,s):W(e.target.name)},[]),Q=u.useCallback((e,s,{shouldDirty:d=!0,shouldOnChange:g=!0}={})=>{const O=A(E.current,e);if(s===O)return;if(P(e,"isTouched",!0),d){const ur=s!==(A(f.current,e)||"");P(e,"isDirty",ur)}E.current=V(E.current,e,s),v.publish(`values.${e}`,s,O);const j=b.current.get(e);j&&j(s),g&&v.publish("onChange",e,s,E.current);const or=mr(E.current,h.current);if(i.current.isDirty!==or){const ur=i.current.isDirty;i.current.isDirty=or,v.publish("formState.isDirty",or,ur)}},[]),K=u.useCallback((e,s)=>(b.current.set(e,s),h.current=V(h.current,e,{...cr}),()=>{t&&b.current.delete(e)}),[]),r=u.useCallback((e,{keepError:s,keepDirty:d,keepTouched:g,defaultValue:O})=>{const j=b.current.get(e);if(!j)throw new Error(`Cannot reset "${e}" because it's uncontrolled field`);f.current[e]=O,E.current[e]=O,j&&j(O),s||W(e),d||P(e,"isDirty",!1),g||P(e,"isTouched",!1)},[]),m=u.useCallback(e=>{if(!e)return v.trigger("values");if(!Array.isArray(e)){const s=$(e);v.trigger(s);return}e.forEach(s=>{const d=$(s);v.trigger(d)})},[]),k=u.useCallback((e,s=_.getValues(e))=>{v.publish("onBlur",e,s,_.getValues())},[]),_=u.useMemo(()=>({subscribe:sr,reset:y,trigger:m,reload:l,resetField:r,setValue:Q,getValues:N,getErrors:M,getFieldStates:I,getFormState:H,setError:B,clearError:W,clearErrors:X,checkValidity:Z,getFieldValidity:C,getDefaultValues:S,setFieldState:P,triggerFieldBlur:k,resetFieldState:nr,getControlledFields:T,setFormState:U,getEvents:v.getEvents,getWatchEvents:v.getWatchEvents}),[o]),x=u.useCallback(()=>{[...a.current.querySelectorAll("[name]")].forEach(d=>{const g=d.name;!d.defaultValue&&!b.current.has(g)&&A(f.current,g)&&(d.defaultValue=A(f.current,g))});const s=w();E.current={...s},f.current={...s}},[]),D=u.useMemo(()=>({ref:a,watch:L,actions:_,initForm:x,registerController:K,registerHookWatcher:tr,lastReloadedAt:o,loadFormValues:w,getWatchValue:Y,channels:v}),[o]),q=u.useMemo(()=>({control:D,actions:_,watch:L}),[o]);return u.useLayoutEffect(()=>{a.current&&x()},[o]),u.useLayoutEffect(()=>{h.current=R(!1)},[]),q};exports.Controller=Ar;exports.Form=Fr;exports.useController=hr;exports.useForm=Nr;exports.useFormContext=ar;exports.useWatch=wr;
|