react-simple-formkit 2.4.3 → 2.4.5
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 +28 -15
- package/dist/react-simple-formkit.js +4 -4
- package/dist/react-simple-formkit.mjs +508 -469
- package/dist/react-simple-formkit.umd.js +4 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -97,6 +97,8 @@ return (
|
|
|
97
97
|
|
|
98
98
|
- **`onBlur` Behavior**: Works automatically by default.
|
|
99
99
|
|
|
100
|
+
> **Warning:** The library automatically captures changes in the `Form` component through bubbled events from input fields. However, if in some cases it cannot capture changes (e.g., due to a custom input or `e.stopPropagation()`), please use **`Controller`** so the form can work correctly.
|
|
101
|
+
|
|
100
102
|
### 2. `Controlled`:
|
|
101
103
|
|
|
102
104
|
**Best for**: Absolute control over input or complex UIs.
|
|
@@ -110,9 +112,9 @@ By using `Controller`, you can transform the value in both directions:
|
|
|
110
112
|
- On `change`: join the selected array back into a string before updating the form state.
|
|
111
113
|
|
|
112
114
|
> **Note:**
|
|
115
|
+
> You should explicitly pass the **`onBlur`** prop during rendering to use blur tracking features.
|
|
113
116
|
> whenever you want to **control the value of a field** (e.g. by calling **`actions.setValue`**), you **must** wrap that field with a **`Controller`**.
|
|
114
117
|
> Without **`Controller`**, the field will not respond to external value changes.
|
|
115
|
-
> You should explicitly pass the **`onBlur`** prop during rendering for the library to track the field's state.
|
|
116
118
|
|
|
117
119
|
Example:
|
|
118
120
|
|
|
@@ -187,9 +189,10 @@ State updates only when observed via `watch()`, `useWatch()`, `subscribe()`, or
|
|
|
187
189
|
- [onBlur](#form) is just a handler callback that is called when field blurred.
|
|
188
190
|
- [actions.subscribe('onChange', callback)](#useform) subscribe onChange callback instead of passing `onChange` in [Form](#form) component.
|
|
189
191
|
- [actions.subscribe('onBlur', callback)](#useform) subscribe onBlur callback instead of passing `onBlur` in [Form](#form) component.
|
|
190
|
-
- [actions.trigger(name)](#
|
|
192
|
+
- [actions.trigger(name, {bubble, trickle})](#useform) trigger watchers (e.g. `watch`, `useWatch`, `subscribe`) re-update values if needed.
|
|
191
193
|
|
|
192
|
-
> **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
|
+
> **Rule\*:** By default, onBlur works automatically for uncontrolled fields. However, for controlled fields, you must explicitly pass the onBlur prop when rendering the field. Or it can be triggered manually using [`actions.triggerFieldBlur()`](#useform).
|
|
195
|
+
> actions.setValue() makes onChange by default, if you call it in onChange callback so it will make infinite loop.
|
|
193
196
|
|
|
194
197
|
### The Power of watch (Unified Observation)
|
|
195
198
|
|
|
@@ -470,7 +473,7 @@ Generic props:
|
|
|
470
473
|
Return:
|
|
471
474
|
|
|
472
475
|
- `control`: contains methods and utilities to control the form.
|
|
473
|
-
- `watch(name)`: `
|
|
476
|
+
- `watch(name, mode)`: `(name: String | Array | undefined, mode: "onChange" | "onBlur") => Object` [Example](#managing-values)
|
|
474
477
|
- `actions` is an object that contains utilities
|
|
475
478
|
- `actions.reset()`: `(newDefaultValues?: Object, options?: { clearCustomFormStates?: Boolean, clearCustomFieldStates?: Boolean }) => void` [Example](#default-values-and-reset)
|
|
476
479
|
- `actions.getValues()`: `(name?: String | Array) => Object` [Example](#managing-values)
|
|
@@ -481,7 +484,7 @@ Return:
|
|
|
481
484
|
- `actions.setError()`: `(name: String, error: Any) => void` [Example](#set-field-error)
|
|
482
485
|
- `actions.setFieldState()`: `(name: String, property: String, value: Any) => void` [Example](#set-field-states)
|
|
483
486
|
- `actions.setFormState()`: `(name: String, value: Any) => void`
|
|
484
|
-
- `actions.triggerFieldBlur
|
|
487
|
+
- `actions.triggerFieldBlur `(name: String, value?: Any) => void`. Used to trigger the blur event for a field manually.
|
|
485
488
|
- `actions.resetFieldState()`: `(name: String) => void`
|
|
486
489
|
- `actions.resetField()`: `(name: String) => void`
|
|
487
490
|
- `actions.clearError()`: `(name: String | Array) => void`
|
|
@@ -490,7 +493,9 @@ Return:
|
|
|
490
493
|
- `actions.subscribeBlur()`: `(callback) => unsubscribe: Function()`
|
|
491
494
|
- `actions.getNumberFields()`: `() => Array`
|
|
492
495
|
- `actions.getDefaultValues()`: `() => Object`
|
|
493
|
-
- `actions.trigger(name)`: `(name: String | Array) => void` trigger watchers (e.g. `watch`, `useWatch`, `subscribe`) re-update values if needed.
|
|
496
|
+
- `actions.trigger(name, options)`: `(name: String | Array, options?: {bubble?: Boolean, trickle?: Boolean}) => void` trigger watchers (e.g. `watch`, `useWatch`, `subscribe`) re-update values if needed.
|
|
497
|
+
- `bubble`: `Boolean`. If true, it will trigger all parent events (e.g. trigger "address.line1" will trigger watch("address"), watch()).
|
|
498
|
+
- `trickle`: `Boolean`. If true, it will trigger all child events (e.g. trigger "address" will trigger watch("address.line1"), watch("address.line2"), etc.).
|
|
494
499
|
- `actions.addGroups()`: `(name: String | Array) => void` [Example](#registering)
|
|
495
500
|
|
|
496
501
|
## Form
|
|
@@ -506,18 +511,22 @@ Generic props:
|
|
|
506
511
|
|
|
507
512
|
Generic props:
|
|
508
513
|
|
|
509
|
-
- `name`
|
|
510
|
-
- `defaultValue`
|
|
514
|
+
- `name`: `String`
|
|
515
|
+
- `defaultValue`: `Any`
|
|
516
|
+
- `shouldUnRegister`: `Boolean` Default is **false**
|
|
517
|
+
- `control`: received from useForm(). Pass it if use Controller outside `<Form>`
|
|
511
518
|
- `render({name, value, onChange, onBlur, fieldState})`
|
|
512
519
|
|
|
513
520
|
## useController
|
|
514
521
|
|
|
515
522
|
Generic props:
|
|
516
523
|
|
|
517
|
-
- `name`
|
|
518
|
-
- `defaultValue`
|
|
524
|
+
- `name`: `String`
|
|
525
|
+
- `defaultValue`: `Any`
|
|
526
|
+
- `shouldUnRegister`: `Boolean` Default is **false**
|
|
527
|
+
- `control`: received from useForm(). Pass it if use Controller outside `<Form>`
|
|
519
528
|
|
|
520
|
-
Return:
|
|
529
|
+
Return: `render` arguments in `<Controller>`
|
|
521
530
|
|
|
522
531
|
## useWatch
|
|
523
532
|
|
|
@@ -526,7 +535,9 @@ Return: everything in render function above
|
|
|
526
535
|
Generic props:
|
|
527
536
|
|
|
528
537
|
- `name`: `String | Array | undefined`. If undefined, it will return all input values
|
|
538
|
+
- `mode`: `"onChange" | "onBlur"`. Default is `onChange`
|
|
529
539
|
- `compute`: `Function` that will calculate from form values and return a value. It will make re-render when the result changes
|
|
540
|
+
- `control`: received from useForm(). Pass it if use Controller outside `<Form>`
|
|
530
541
|
|
|
531
542
|
Return:
|
|
532
543
|
|
|
@@ -539,14 +550,15 @@ Return:
|
|
|
539
550
|
|
|
540
551
|
Return:
|
|
541
552
|
|
|
542
|
-
- `watch`
|
|
543
|
-
- `actions`
|
|
553
|
+
- `watch(name, mode)`
|
|
554
|
+
- `actions`: same with `useForm().actions`
|
|
544
555
|
|
|
545
556
|
## watch
|
|
546
557
|
|
|
547
|
-
Arguments:
|
|
558
|
+
Arguments: `watch(name, mode)`
|
|
548
559
|
|
|
549
560
|
- `name`: `String | Array | undefined`. If undefined, it will return all input values
|
|
561
|
+
- `mode`: `"onChange" | "onBlur"`. Default is `onChange`
|
|
550
562
|
|
|
551
563
|
Return:
|
|
552
564
|
|
|
@@ -554,10 +566,11 @@ Return:
|
|
|
554
566
|
|
|
555
567
|
## subscribe
|
|
556
568
|
|
|
557
|
-
Arguments:
|
|
569
|
+
Arguments: `subscribe(name, callback, mode)`
|
|
558
570
|
|
|
559
571
|
- `name`: `String | Array | undefined`. If undefined, it will return all input values
|
|
560
572
|
- `callback()`: `Function` receive value based on name parameter. Only be called when the value changes
|
|
573
|
+
- `mode`: `"onChange" | "onBlur"`. Default is `onChange`
|
|
561
574
|
|
|
562
575
|
Return:
|
|
563
576
|
|
|
@@ -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 n=require("react");var ae={exports:{}},re={};/**
|
|
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 ge;function Ae(){if(ge)return re;ge=1;var o=Symbol.for("react.transitional.element"),t=Symbol.for("react.fragment");function c(i,a,d){var u=null;if(d!==void 0&&(u=""+d),a.key!==void 0&&(u=""+a.key),"key"in a){d={};for(var m in a)m!=="key"&&(d[m]=a[m])}else d=a;return a=d.ref,{$$typeof:o,type:i,key:u,ref:a!==void 0?a:null,props:d}}return re.Fragment=t,re.jsx=c,re.jsxs=c,re}var te={};/**
|
|
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 Ee;function
|
|
17
|
+
*/var Ee;function _e(){return Ee||(Ee=1,process.env.NODE_ENV!=="production"&&function(){function o(r){if(r==null)return null;if(typeof r=="function")return r.$$typeof===se?null:r.displayName||r.name||null;if(typeof r=="string")return r;switch(r){case v:return"Fragment";case j:return"Profiler";case N:return"StrictMode";case x:return"Suspense";case Y:return"SuspenseList";case F: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 y:return"Portal";case p:return(r.displayName||"Context")+".Provider";case k:return(r._context.displayName||"Context")+".Consumer";case O:var g=r.render;return r=r.displayName,r||(r=g.displayName||g.name||"",r=r!==""?"ForwardRef("+r+")":"ForwardRef"),r;case z:return g=r.displayName||null,g!==null?g:o(r.type)||"Memo";case G:g=r._payload,r=r._init;try{return o(r(g))}catch{}}return null}function t(r){return""+r}function c(r){try{t(r);var g=!1}catch{g=!0}if(g){g=console;var T=g.error,P=typeof Symbol=="function"&&Symbol.toStringTag&&r[Symbol.toStringTag]||r.constructor.name||"Object";return T.call(g,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",P),t(r)}}function i(r){if(r===v)return"<>";if(typeof r=="object"&&r!==null&&r.$$typeof===G)return"<...>";try{var g=o(r);return g?"<"+g+">":"<...>"}catch{return"<...>"}}function a(){var r=K.A;return r===null?null:r.getOwner()}function d(){return Error("react-stack-top-frame")}function u(r){if(ne.call(r,"key")){var g=Object.getOwnPropertyDescriptor(r,"key").get;if(g&&g.isReactWarning)return!1}return r.key!==void 0}function m(r,g){function T(){J||(J=!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)",g))}T.isReactWarning=!0,Object.defineProperty(r,"key",{get:T,configurable:!0})}function C(){var r=o(this.type);return oe[r]||(oe[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,g,T,P,L,M,X,Q){return T=M.ref,r={$$typeof:A,type:r,key:g,props:M,_owner:L},(T!==void 0?T:null)!==null?Object.defineProperty(r,"ref",{enumerable:!1,get:C}):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:X}),Object.defineProperty(r,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:Q}),Object.freeze&&(Object.freeze(r.props),Object.freeze(r)),r}function h(r,g,T,P,L,M,X,Q){var w=g.children;if(w!==void 0)if(P)if(le(w)){for(P=0;P<w.length;P++)l(w[P]);Object.freeze&&Object.freeze(w)}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 l(w);if(ne.call(g,"key")){w=o(r);var V=Object.keys(g).filter(function(ie){return ie!=="key"});P=0<V.length?"{key: someKey, "+V.join(": ..., ")+": ...}":"{key: someKey}",ce[w+P]||(V=0<V.length?"{"+V.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} />`,O,_,U,_),oe[_+O]=!0)}if(_=null,p!==void 0&&(c(p),_=""+p),u(d)&&(c(d.key),_=""+d.key),"key"in d){p={};for(var Z in d)Z!=="key"&&(p[Z]=d[Z])}else p=d;return _&&m(p,typeof r=="function"?r.displayName||r.name||"Unknown":r),b(r,_,x,W,a(),p,X,B)}function g(r){typeof r=="object"&&r!==null&&r.$$typeof===A&&r._store&&(r._store.validated=1)}var v=s,A=Symbol.for("react.transitional.element"),S=Symbol.for("react.portal"),C=Symbol.for("react.fragment"),k=Symbol.for("react.strict_mode"),w=Symbol.for("react.profiler"),I=Symbol.for("react.consumer"),D=Symbol.for("react.context"),Y=Symbol.for("react.forward_ref"),ce=Symbol.for("react.suspense"),ae=Symbol.for("react.suspense_list"),ee=Symbol.for("react.memo"),L=Symbol.for("react.lazy"),F=Symbol.for("react.activity"),re=Symbol.for("react.client.reference"),H=v.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,te=Object.prototype.hasOwnProperty,le=Array.isArray,J=console.createTask?console.createTask:function(){return null};v={"react-stack-bottom-frame":function(r){return r()}};var G,se={},N=v["react-stack-bottom-frame"].bind(v,f)(),ne=J(l(f)),oe={};K.Fragment=C,K.jsx=function(r,d,p,O,W){var x=1e4>H.recentlyCreatedOwnerStacks++;return i(r,d,p,!1,O,W,x?Error("react-stack-top-frame"):N,x?J(l(r)):ne)},K.jsxs=function(r,d,p,O,W){var x=1e4>H.recentlyCreatedOwnerStacks++;return i(r,d,p,!0,O,W,x?Error("react-stack-top-frame"):N,x?J(l(r)):ne)}}()),K}var ge;function _e(){return ge||(ge=1,process.env.NODE_ENV==="production"?ue.exports=Te():ue.exports=Ae()),ue.exports}var he=_e();const y=()=>{},ve=s.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:{}}),fe=()=>s.useContext(ve),Oe=({id:n,control:t,method:c,action:l,children:a,onChange:f,onBlur:u,onSubmit:m=()=>{},onInput:E=()=>{},onReset:b=()=>{},numberFields:i=[],className:g,...v})=>{const A=s.useCallback(S=>{t.ref&&(t.ref.current=S,t.ref.current&&t.initForm())},[t]);return s.useEffect(()=>{let S=()=>{},C=()=>{};return f&&(S=t.channels.subscribe("onChange",f)),u&&(C=t.channels.subscribe("onBlur",u)),()=>{S(),C()}},[t.lastReloadedAt]),he.jsx(ve.Provider,{value:t,children:he.jsx("form",{id:n,ref:A,action:l,method:c,className:g,onInput:E,onSubmit:S=>{c||S.preventDefault();const C=t.loadFormValues();m(C)},onChange:S=>{const C=S.target.name;!C||t.actions.getControlledFields().has(C)||t.actions.setValue(C,S.target.value)},onBlur:S=>{const C=S.target.name;if(!C||t.actions.getControlledFields().has(C))return;const k=S.target.value;t.channels.publish("onBlur",C,k,t.actions.getValues())},onReset:S=>{t.actions.reset(),b(S)},...v,children:a},t.lastReloadedAt)})},ie={isDirty:!1,isTouched:!1,error:null},me={lastReset:null,isDirty:!1,isError:!1,errorFields:[],dirtyFields:[],touchedFields:[]},we={},je=["badInput","customError","patternMismatch","rangeOverflow","rangeUnderflow","stepMismatch","tooLong","tooShort","typeMismatch","valueMissing"],P=(n={},t="")=>t.split(".").reduce((c,l)=>c&&c[l]!==void 0?c[l]:void 0,n),M=(n={},t="",c)=>{const l=Array.isArray(n)?[...n]:{...n};let a=l;const f=t.split(".");return f.forEach((u,m)=>{m<f.length-1?(a[u]||(a[u]=/^\d+$/.test(f[m+1])?[]:{}),Array.isArray(a[u])?a[u]=[...a[u]]:a[u]={...a[u]},a=a[u]):a[u]=c}),l},Ce=n=>{let t=Array.isArray(n)?[...n]:{...n};return Object.keys(t).forEach(c=>{if(c.includes(".")){const l=P(t,c);t=M(t,c,l||""),delete t[c]}}),t},Pe=["errors","fieldStates","formState"],q=n=>Pe.some(t=>n.startsWith(t))?n:"values."+n,Re=n=>{typeof n.setCustomValidity=="function"&&n.setCustomValidity("");let t=n.validity,c=je.find(l=>t[l]);if(c)return{type:c,message:n.validationMessage}},Se=(n=new Set,t={},c="")=>Object.keys(t||{}).reduce((l,a)=>{const f=(t||{})[a];return n.has(c+a)?[...l,...Se(n,f,c+a+".")]:f!=null&&f.isDirty?[...l,c+a]:l},[]),pe=(n=new Set,t={},c="")=>Object.keys(t||{}).reduce((l,a)=>{const f=(t||{})[a];return n.has(c+a)?[...l,...pe(n,f,c+a+".")]:f!=null&&f.isTouched?[...l,c+a]:l},[]),ye=(n=new Set,t={},c="")=>Object.keys(t||{}).reduce((l,a)=>{const f=(t||{})[a];return n.has(c+a)?[...l,...ye(n,f,c+a+".")]:f?[...l,c+a]:l},[]),Ne=({control:n,name:t,compute:c})=>{const{getWatchValue:l,registerHookWatcher:a}=n||fe(),f=l({name:t,compute:c}),[u,m]=s.useState(f);return s.useEffect(()=>a({name:t,compute:c,value:u,setValue:m}),[]),u},ke=({control:n,name:t,defaultValue:c})=>{const{actions:l,registerController:a,channels:f,getWatchValue:u}=n||fe(),m=s.useRef(),E=s.useRef(),b=P(l.getDefaultValues(),t)||c||"",[i,g]=s.useState(b),[v,A]=s.useState({}),S=s.useCallback((k,{shouldDirty:w=!0,shouldOnChange:I=!0}={})=>{var Y;let D=k;((Y=k==null?void 0:k.target)==null?void 0:Y.value)!==void 0&&(D=k.target.value),g(D),l.setValue(t,D,{shouldDirty:w,shouldOnChange:I})},[l.setValue]),C=s.useCallback(k=>{const w=l.getValues(),I=k??P(w,t);f.publish("onBlur",t,I,w)},[]);return s.useEffect(()=>{const k=a(t,g);return()=>{k(),E.current&&E.current()}},[]),new Proxy({ref:m,name:t,defaultValue:b,value:i,onChange:S,onBlur:C,fieldState:v},{get(k,w,I){return typeof w=="string"&&w==="fieldState"&&(E.current&&E.current(),E.current=l.subscribe(`fieldStates.${t}`,()=>{A(u({name:`fieldStates.${t}`}))})),Reflect.get(k,w,I)}})},De=({name:n,control:t,defaultValue:c,render:l=({ref:a,name:f,defaultValue:u,value:m,onChange:E,onBlur:b,fieldState:i})=>null})=>{const a=ke({name:n,defaultValue:c,control:t});return l(a)},xe=()=>{const[n,t]=s.useState(),c=s.useCallback(()=>t(new Date().toString()),[]);return[n,c]},$e=()=>{const[,n]=s.useState({});return s.useCallback(()=>n({}),[])},Me=({channels:n,getWatchValue:t})=>{const c=$e(),l=s.useCallback(u=>{if(!u)return n.subscribeWatch("values",()=>c()),t();if(typeof u=="string"){const m=q(u);n.subscribeWatch(m,(E,b)=>{E!==b&&c()})}return Array.isArray(u)&&u.forEach(m=>{const E=q(m);n.subscribeWatch(E,(b,i)=>{b!==i&&c()})}),t({name:u})},[]),a=s.useCallback(({name:u,compute:m,setValue:E})=>{if(typeof m=="function")return n.subscribe("values",()=>{const i=t({compute:m});E(i)});if(!u)return n.subscribe("values",()=>{E(t())});if(typeof u=="string"){const b=q(u);return n.subscribe(b,()=>{const g=t({name:u});E(g)})}if(Array.isArray(u)){const b=[];return u.forEach(i=>{const g=q(i),v=n.subscribe(g,()=>{const A=t({name:u});E(A)});b.push(v)}),()=>b.forEach(i=>i())}throw new Error("Parameters of name must be string or array of string or compute must be a function")},[]),f=s.useCallback((u,m)=>{if(!u)return n.subscribe("values",()=>m(t()));if(["onChange","onBlur"].includes(u))return n.subscribe(u,m);if(typeof u=="string"){const E=q(u);return n.subscribe(E,()=>m(t({name:u})))}if(Array.isArray(u)){const E=[];return u.forEach(b=>{const i=q(b),g=n.subscribe(i,()=>{m(t({name:u}))});E.push(g)}),()=>E.forEach(b=>b())}throw new Error("Parameters of name must be string or array of string")},[]);return{watch:l,registerHookWatcher:a,getWatchValue:t,subscribe:f}},We=({getWatchValue:n})=>{const t=s.useRef(new Map),c=s.useRef(new Map),l=s.useCallback(()=>c.current,[]),a=s.useCallback(()=>t.current,[]),f=s.useCallback(()=>{t.current.clear(),c.current.clear()},[]),u=s.useCallback((i,...g)=>{const v=t.current.get(i),A=c.current.get(i);v&&v.forEach(S=>S(...g)),A&&A.forEach(S=>S(...g)),t.current.forEach((S,C)=>{if(C!==i&&i.startsWith(C)){const k=n({name:C.replace("values.","").replace("values","")});S.forEach(w=>w(M(k,i.replace(`${C}.`,""),g[0])))}}),c.current.forEach((S,C)=>{if(C!==i&&i.startsWith(C)){const k=n({name:C.replace("values.","").replace("values","")});S.forEach(w=>w(M(k,i.replace(`${C}.`,""),g[0])))}})},[]),m=s.useCallback(i=>{t.current.forEach((g,v)=>{v.startsWith(i)&&g.forEach(A=>A(n({name:v.replace("values.","").replace("values","")})))}),c.current.forEach((g,v)=>{v.startsWith(i)&&g.forEach(A=>A(n({name:v.replace("values.","").replace("values","")})))})},[]),E=s.useCallback((i,g)=>(t.current.has(i)||t.current.set(i,new Set),t.current.get(i).add(g),()=>{var v;return(v=t.current.get(i))==null?void 0:v.delete(g)}),[]),b=s.useCallback((i,g)=>{c.current.has(i)||(c.current.set(i,new Set),c.current.get(i).add(g))},[]);return{reset:f,publish:u,subscribe:E,subscribeWatch:b,getEvents:a,getWatchEvents:l,trigger:m}},Ie=({defaultValues:n=we,shouldUnRegister:t=!1,groups:c=[]}={})=>{const[l,a]=xe(),f=s.useRef(),u=s.useRef(new Map),m=s.useRef({}),E=s.useRef({}),b=s.useRef({...me}),i=s.useRef({...n}),g=s.useRef({...n}),v=s.useRef(new Set(c)),A=s.useCallback((e,o=g.current)=>Object.keys(o).reduce((h,R)=>{if(typeof o[R]=="object")return{...h,[R]:A(e,o[R])};const j={...e?{}:E.current[R]||{},...ie};return{...h,[R]:j}},{}),[]),S=s.useCallback((e={},{clearCustomFormStates:o=!1,clearCustomFieldStates:h=!1,groups:R=v.current}={})=>{g.current={...g.current,...e},i.current={...g.current},u.current.forEach((j,T)=>{j(P(g.current,T)??"")}),m.current={},v.current=new Set(R),u.current=new Map,b.current={...o?{}:b.current,...me},E.current=A(h),F.reset(),a()},[]),C=s.useCallback(()=>{let e=Object.fromEntries(new FormData(f.current));u.current.forEach((h,R)=>{e=M(e,R,P(i.current,R))});let o={...i.current,...e};return Ce(o)},[]),k=s.useCallback(e=>Re(e.target),[]),w=s.useCallback(()=>g.current,[]),I=s.useCallback(()=>u.current,[]),D=s.useCallback((e,o)=>o?Array.isArray(o)?o.reduce((h,R)=>({...h,[R]:P(e,R)}),{}):P(e,o):{...e},[]),Y=s.useCallback(e=>D(i.current,e),[D]),ce=s.useCallback(e=>D(m.current,e),[D]),ae=s.useCallback(e=>D(E.current,e),[D]),ee=s.useCallback(e=>D({...b.current,lastReset:l},e),[l]),L=s.useCallback(({name:e,compute:o}={})=>!e&&!o||e==="values"?Y():typeof o=="function"?o(Y()):Array.isArray(e)?e.reduce((h,R)=>({...h,[R]:L({name:R})}),{}):P({...i.current,errors:m.current,fieldStates:E.current,formState:ee()},e),[]),F=We({getWatchValue:L}),{watch:re,registerHookWatcher:H,subscribe:te}=Me({getWatchValue:L,channels:F}),le=s.useCallback((e,o)=>{const h=b.current,R=P(h,e);R!==o&&(b.current=M(b.current,e,o),F.publish(`formState.${e}`,o,R))},[]),J=s.useCallback((e,o)=>{N(e,"error",o)},[]),G=s.useCallback(e=>{N(e,"error",null)},[]),se=s.useCallback(()=>{b.current.isError&&Object.keys(m.current).forEach(e=>G(e))},[]),N=s.useCallback((e,o,h)=>{try{if(typeof h=="function"&&(h=h(L({name:e}))),v.current.has(e))if(typeof h=="object"&&h!==null){Object.keys(h).forEach(T=>{N(`${e}.${T}`,o,h[T])});return}else return console.error(`Cannot set primitive value for nested parent field "${e}". Please set value for its children or provide an object.`);const R=P(E.current,e)||{...ie},j=P(R,o);if(j!==h){if(E.current=M(E.current,`${e}.${o}`,h),F.publish(`fieldStates.${e}.${o}`,h,j),o==="error"){const T=P(m.current,e),V=h||null;T!==V&&(m.current=M(m.current,e,V),F.publish(`errors.${e}`,V,T));const $=ye(v.current,m.current);b.current.errorFields=$,F.publish("formState.errorFields",$);const z=$.length>0;if(b.current.isError!==z){const de=b.current.isError;b.current.isError=z,F.publish("formState.isError",z,de)}}if(o==="isDirty"){const T=Se(v.current,E.current);b.current.dirtyFields=T,F.publish("formState.dirtyFields",T);const V=T.length>0;if(b.current.isDirty!==V){const $=b.current.isDirty;b.current.isDirty=V,F.publish("formState.isDirty",V,$)}}if(o==="isTouched"){const T=pe(v.current,E.current);b.current.touchedFields=T,F.publish("formState.touchedFields",T)}}}catch(R){console.log(R)}},[]),ne=s.useCallback(e=>{N(e,"isDirty",!1),N(e,"isTouched",!1),N(e,"error",!1)},[]),oe=s.useCallback(e=>{const o=Re(e.target);o?J(e.target.name,o):G(e.target.name)},[]),r=s.useCallback((e,o,{shouldDirty:h=!0,shouldOnChange:R=!0}={})=>{if(typeof o=="function"&&(o=o(Y(e))),v.current.has(e)){typeof o=="object"&&o!==null?Object.keys(o).forEach($=>{r(`${e}.${$}`,o[$],{shouldDirty:h,shouldOnChange:R})}):console.error(`Cannot set primitive value for nested parent field "${e}". Please set value for its children or provide an object.`);return}const j=P(i.current,e);if(o===j)return;i.current=M(i.current,e,o),F.publish(`values.${e}`,o,j);const T=u.current.get(e);if(T&&T(o),e.includes(".")){const $=e.split("."),z=$.findIndex((de,Fe)=>!v.current.has($.slice(0,Fe+1).join(".")));z!==-1&&(e=$.slice(0,z+1).join("."))}o&&N(e,"isTouched",!0);const V=o!==(P(g.current,e)||"");h&&N(e,"isDirty",V),R&&F.publish("onChange",e,o,j)},[]),d=s.useCallback(e=>{const o=e.split(".");let h="";o.forEach((R,j)=>{j<o.length-1&&(h=h?`${h}.${R}`:R,v.current.add(h))})},[]),p=s.useCallback(e=>{Array.isArray(e)?e.forEach(o=>d(o+".")):d(e+".")},[]),O=s.useCallback((e="",o)=>(u.current.set(e,o),E.current=M(E.current,e,{...ie}),e.includes(".")&&d(e),()=>{t&&u.current.delete(e)}),[]),W=s.useCallback((e,{keepError:o,keepDirty:h,keepTouched:R,defaultValue:j})=>{const T=u.current.get(e);if(!T)throw new Error(`Cannot reset "${e}" because it's uncontrolled field`);g.current=M(g.current,e,j),i.current=M(i.current,e,j),T&&T(j),o||G(e),h||N(e,"isDirty",!1),R||N(e,"isTouched",!1)},[]),x=s.useCallback(e=>{if(!e)return F.trigger("values");if(!Array.isArray(e)){const o=q(e);F.trigger(o);return}e.forEach(o=>{const h=q(o);F.trigger(h)})},[]),X=s.useCallback((e,o=B.getValues(e))=>{F.publish("onBlur",e,o,B.getValues())},[]),B=s.useMemo(()=>({subscribe:te,reset:S,trigger:x,reload:a,resetField:W,setValue:r,getValues:Y,getErrors:ce,getFieldStates:ae,getFormState:ee,setError:J,clearError:G,clearErrors:se,checkValidity:oe,getFieldValidity:k,getDefaultValues:w,setFieldState:N,triggerFieldBlur:X,resetFieldState:ne,getControlledFields:I,setFormState:le,getEvents:F.getEvents,getWatchEvents:F.getWatchEvents,addGroups:p}),[l]),_=s.useCallback(()=>{[...f.current.querySelectorAll("[name]")].forEach(h=>{const R=h.name||"";R.includes(".")&&d(R),!h.defaultValue&&!u.current.has(R)&&P(g.current,R)&&(h.defaultValue=P(g.current,R))});const o=C();i.current={...o},g.current={...o}},[]),U=s.useMemo(()=>({ref:f,watch:re,actions:B,initForm:_,registerController:O,registerHookWatcher:H,lastReloadedAt:l,loadFormValues:C,getWatchValue:L,channels:F}),[l]),Z=s.useMemo(()=>({control:U,actions:B,watch:re}),[l]);return s.useLayoutEffect(()=>{f.current&&_()},[l]),s.useLayoutEffect(()=>{E.current=A(!1)},[]),Z};exports.Controller=De;exports.Form=Oe;exports.restoreFromDotNotation=Ce;exports.useController=ke;exports.useForm=Ie;exports.useFormContext=fe;exports.useWatch=Ne;
|
|
22
|
+
<%s key={someKey} {...props} />`,P,w,V,w),ce[w+P]=!0)}if(w=null,T!==void 0&&(c(T),w=""+T),u(g)&&(c(g.key),w=""+g.key),"key"in g){T={};for(var ee in g)ee!=="key"&&(T[ee]=g[ee])}else T=g;return w&&m(T,typeof r=="function"?r.displayName||r.name||"Unknown":r),E(r,w,M,L,a(),T,X,Q)}function l(r){typeof r=="object"&&r!==null&&r.$$typeof===A&&r._store&&(r._store.validated=1)}var R=n,A=Symbol.for("react.transitional.element"),y=Symbol.for("react.portal"),v=Symbol.for("react.fragment"),N=Symbol.for("react.strict_mode"),j=Symbol.for("react.profiler"),k=Symbol.for("react.consumer"),p=Symbol.for("react.context"),O=Symbol.for("react.forward_ref"),x=Symbol.for("react.suspense"),Y=Symbol.for("react.suspense_list"),z=Symbol.for("react.memo"),G=Symbol.for("react.lazy"),F=Symbol.for("react.activity"),se=Symbol.for("react.client.reference"),K=R.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,ne=Object.prototype.hasOwnProperty,le=Array.isArray,H=console.createTask?console.createTask:function(){return null};R={"react-stack-bottom-frame":function(r){return r()}};var J,oe={},W=R["react-stack-bottom-frame"].bind(R,d)(),ue=H(i(d)),ce={};te.Fragment=v,te.jsx=function(r,g,T,P,L){var M=1e4>K.recentlyCreatedOwnerStacks++;return h(r,g,T,!1,P,L,M?Error("react-stack-top-frame"):W,M?H(i(r)):ue)},te.jsxs=function(r,g,T,P,L){var M=1e4>K.recentlyCreatedOwnerStacks++;return h(r,g,T,!0,P,L,M?Error("react-stack-top-frame"):W,M?H(i(r)):ue)}}()),te}var he;function Oe(){return he||(he=1,process.env.NODE_ENV==="production"?ae.exports=Ae():ae.exports=_e()),ae.exports}var me=Oe();const S=()=>{},ve=n.createContext({ref:null,watch:S,actions:{subscribe:S,reset:S,trigger:S,reload:S,resetField:S,setValue:S,getValues:S,getErrors:S,getFieldStates:S,getFormState:S,setError:S,clearError:S,clearErrors:S,checkValidity:S,getFieldValidity:S,getDefaultValues:S,setFieldState:S,triggerFieldBlur:S,resetFieldState:S,getControlledFields:S,setFormState:S,getEvents:S,getWatchEvents:S,addGroups:S},registerController:S,registerHookWatcher:S,lastReloadedAt:S,loadFormValues:S,getWatchValue:S,channels:{}}),de=()=>n.useContext(ve),je=({id:o,control:t,method:c,action:i,children:a,onChange:d,onBlur:u,onSubmit:m=()=>{},onInput:C=()=>{},onReset:E=()=>{},numberFields:h=[],className:l,...R})=>{const A=n.useCallback(y=>{t.ref&&(t.ref.current=y,t.ref.current&&t.initForm())},[t]);return n.useEffect(()=>{let y=()=>{},v=()=>{};return d&&(y=t.channels.subscribe("onChange",d)),u&&(v=t.channels.subscribe("onBlur",u)),()=>{y(),v()}},[t.lastReloadedAt]),me.jsx(ve.Provider,{value:t,children:me.jsx("form",{id:o,ref:A,action:i,method:c,className:l,onInput:C,onSubmit:y=>{c||y.preventDefault();const v=t.loadFormValues();m(v)},onChange:y=>{const v=y.target.name;!v||t.actions.getControlledFields().has(v)||t.actions.setValue(v,y.target.value)},onBlur:y=>{const v=y.target.name;if(!v||t.actions.getControlledFields().has(v))return;const N=y.target.value;t.channels.publish("onBlur",v,N,t.actions.getValues()),t.triggerBlurWatchers(v)},onReset:y=>{t.actions.reset(),E(y)},...R,children:a},t.lastReloadedAt)})},fe={isDirty:!1,isTouched:!1,error:null},Re={lastReset:null,isDirty:!1,isError:!1,errorFields:[],dirtyFields:[],touchedFields:[]},we={},Pe=["badInput","customError","patternMismatch","rangeOverflow","rangeUnderflow","stepMismatch","tooLong","tooShort","typeMismatch","valueMissing"],D=(o={},t="")=>t.split(".").reduce((c,i)=>c&&c[i]!==void 0?c[i]:void 0,o),I=(o={},t="",c)=>{const i=Array.isArray(o)?[...o]:{...o};let a=i;const d=t.split(".");return d.forEach((u,m)=>{m<d.length-1?(a[u]||(a[u]=/^\d+$/.test(d[m+1])?[]:{}),Array.isArray(a[u])?a[u]=[...a[u]]:a[u]={...a[u]},a=a[u]):a[u]=c}),i},Se=o=>{let t=Array.isArray(o)?[...o]:{...o};return Object.keys(t).forEach(c=>{if(c.includes(".")){const i=D(t,c);t=I(t,c,i||""),delete t[c]}}),t},$e=["errors","fieldStates","formState"],q=o=>$e.some(t=>o.startsWith(t))?o:"values."+o,Ce=o=>{typeof o.setCustomValidity=="function"&&o.setCustomValidity("");let t=o.validity,c=Pe.find(i=>t[i]);if(c)return{type:c,message:o.validationMessage}},ye=(o=new Set,t={},c="")=>Object.keys(t||{}).reduce((i,a)=>{const d=(t||{})[a];return o.has(c+a)?[...i,...ye(o,d,c+a+".")]:d!=null&&d.isDirty?[...i,c+a]:i},[]),ke=(o=new Set,t={},c="")=>Object.keys(t||{}).reduce((i,a)=>{const d=(t||{})[a];return o.has(c+a)?[...i,...ke(o,d,c+a+".")]:d!=null&&d.isTouched?[...i,c+a]:i},[]),pe=(o=new Set,t={},c="")=>Object.keys(t||{}).reduce((i,a)=>{const d=(t||{})[a];return o.has(c+a)?[...i,...pe(o,d,c+a+".")]:d?[...i,c+a]:i},[]),Fe=({control:o,name:t,defaultValue:c,shouldUnRegister:i})=>{const{actions:a,registerController:d,channels:u,getWatchValue:m,triggerBlurWatchers:C}=o||de(),E=n.useRef(),h=n.useRef(),l=D(a.getDefaultValues(),t)||c||"",[R,A]=n.useState(l),[y,v]=n.useState({}),N=n.useCallback((k,{shouldDirty:p=!0,shouldOnChange:O=!0}={})=>{var Y;let x=k;((Y=k==null?void 0:k.target)==null?void 0:Y.value)!==void 0&&(x=k.target.value),A(x),a.setValue(t,x,{shouldDirty:p,shouldOnChange:O})},[a.setValue]),j=n.useCallback(k=>{const p=a.getValues(),O=k??D(p,t);u.publish("onBlur",t,O,p),C(t)},[]);return n.useEffect(()=>{const k=d(t,A,{shouldUnRegister:i});return()=>{k(),h.current&&h.current()}},[i]),new Proxy({ref:E,name:t,defaultValue:l,value:R,onChange:N,onBlur:j,fieldState:y},{get(k,p,O){return typeof p=="string"&&p==="fieldState"&&(h.current&&h.current(),h.current=a.subscribe(`fieldStates.${t}`,()=>{v(m({name:`fieldStates.${t}`}))})),Reflect.get(k,p,O)}})},Ne=({name:o,control:t,shouldUnRegister:c,defaultValue:i,render:a=({ref:d,name:u,defaultValue:m,value:C,onChange:E,onBlur:h,fieldState:l})=>null})=>{const d=Fe({name:o,defaultValue:i,shouldUnRegister:c,control:t});return a(d)},De=()=>{const[o,t]=n.useState(),c=n.useCallback(()=>t(new Date().toString()),[]);return[o,c]},xe=()=>{const[,o]=n.useState({});return n.useCallback(()=>o({}),[])},We=({channels:o,getWatchValue:t})=>{const c=xe(),i=n.useCallback((u,m="onChange")=>{if(!u)return o.subscribeWatch("values",()=>c()),t();if(typeof u=="string"){let C=q(u);m==="onBlur"&&(C=`blur.${C}`),o.subscribeWatch(C,(E,h)=>{E!==h&&c()})}return Array.isArray(u)&&u.forEach(C=>{let E=q(C);m==="onBlur"&&(E=`blur.${E}`),o.subscribeWatch(E,(h,l)=>{h!==l&&c()})}),t({name:u})},[]),a=n.useCallback(({name:u,compute:m,setValue:C,mode:E="onChange"})=>{if(typeof m=="function")return o.subscribe("values",()=>{const l=t({compute:m});C(l)});if(!u)return o.subscribe("values",()=>{C(t())});if(typeof u=="string"){let h=q(u);return E==="onBlur"&&(h=`blur.${h}`),o.subscribe(h,()=>{const R=t({name:u});C(R)})}if(Array.isArray(u)){const h=[];return u.forEach(l=>{let R=q(l);E==="onBlur"&&(R=`blur.${R}`);const A=o.subscribe(R,()=>{const y=t({name:u});C(y)});h.push(A)}),()=>h.forEach(l=>l())}throw new Error("Parameters of name must be string or array of string or compute must be a function")},[]),d=n.useCallback((u,m,C="onChange")=>{if(!u)return o.subscribe("values",()=>m(t()));if(["onChange","onBlur"].includes(u))return o.subscribe(u,m);if(typeof u=="string"){let E=q(u);return C==="onBlur"&&(E=`blur.${E}`),o.subscribe(E,()=>m(t({name:u})))}if(Array.isArray(u)){const E=[];return u.forEach(h=>{let l=q(h);C==="onBlur"&&(l=`blur.${l}`);const R=o.subscribe(l,()=>{m(t({name:u}))});E.push(R)}),()=>E.forEach(h=>h())}throw new Error("Parameters of name must be string or array of string")},[]);return{watch:i,registerHookWatcher:a,getWatchValue:t,subscribe:d}},Me=({getWatchValue:o})=>{const t=n.useRef(new Map),c=n.useRef(new Map),i=n.useCallback(()=>c.current,[]),a=n.useCallback(()=>t.current,[]),d=n.useCallback(()=>{t.current.clear(),c.current.clear()},[]),u=l=>l.replace("values.","").replace("values",""),m=n.useCallback((l,...R)=>{const A=l.split(".");A.forEach((y,v)=>{const N=A.slice(0,A.length-v).join("."),j=t.current.get(N),k=c.current.get(N);if(v>0){const p=R[0],O=o({name:u(N)});R=[I(O,l.replace(N,""),p)]}j&&j.forEach(p=>p(...R)),k&&k.forEach(p=>p(...R))})},[]),C=n.useCallback((l,{trickle:R=!1,bubble:A=!1})=>{const y=l.split(".");let v=[o({name:u(l)})];y.forEach((N,j)=>{const k=y.slice(0,y.length-j).join("."),p=t.current.get(k),O=c.current.get(k);if(j>0&&A){const x=v[0],Y=o({name:u(k)});v=[I(Y,l.replace(k,""),x)]}p&&(j===0||A)&&p.forEach(x=>x(...v)),O&&(j===0||A)&&O.forEach(x=>x(...v))}),R&&(t.current.forEach((N,j)=>{j.startsWith(l)&&N.forEach(k=>k(o({name:u(j)})))}),c.current.forEach((N,j)=>{j.startsWith(l)&&N.forEach(k=>k(o({name:u(j)})))}))},[]),E=n.useCallback((l,R)=>(t.current.has(l)||t.current.set(l,new Set),t.current.get(l).add(R),()=>{var A;return(A=t.current.get(l))==null?void 0:A.delete(R)}),[]),h=n.useCallback((l,R)=>{c.current.has(l)||(c.current.set(l,new Set),c.current.get(l).add(R))},[]);return{reset:d,publish:m,subscribe:E,subscribeWatch:h,getEvents:a,getWatchEvents:i,trigger:C}},Be=({defaultValues:o=we,shouldUnRegister:t=!1,groups:c=[]}={})=>{const[i,a]=De(),d=n.useRef(),u=n.useRef(new Map),m=n.useRef({}),C=n.useRef({}),E=n.useRef({...Re}),h=n.useRef({...o}),l=n.useRef({...o}),R=n.useRef(new Set(c)),A=n.useCallback((e,s=l.current)=>Object.keys(s).reduce((b,f)=>{if(typeof s[f]=="object")return{...b,[f]:A(e,s[f])};const $={...e?{}:C.current[f]||{},...fe};return{...b,[f]:$}},{}),[]),y=n.useCallback((e={},{clearCustomFormStates:s=!1,clearCustomFieldStates:b=!1,groups:f=R.current}={})=>{l.current={...l.current,...e},h.current={...l.current},u.current.forEach(($,_)=>{$(D(l.current,_)??"")}),m.current={},R.current=new Set(f),u.current=new Map,E.current={...s?{}:E.current,...Re},C.current=A(b),F.reset(),a()},[]),v=n.useCallback(()=>{let e=Object.fromEntries(new FormData(d.current));u.current.forEach((b,f)=>{e=I(e,f,D(h.current,f))});let s={...h.current,...e};return Se(s)},[]),N=n.useCallback(e=>Ce(e.target),[]),j=n.useCallback(()=>l.current,[]),k=n.useCallback(()=>u.current,[]),p=n.useCallback((e,s)=>s?Array.isArray(s)?s.reduce((b,f)=>({...b,[f]:D(e,f)}),{}):D(e,s):{...e},[]),O=n.useCallback(e=>p(h.current,e),[p]),x=n.useCallback(e=>p(m.current,e),[p]),Y=n.useCallback(e=>p(C.current,e),[p]),z=n.useCallback(e=>p({...E.current,lastReset:i},e),[i]),G=n.useCallback(({name:e,compute:s}={})=>!e&&!s||e==="values"?O():typeof s=="function"?s(O()):Array.isArray(e)?e.reduce((b,f)=>({...b,[f]:G({name:f})}),{}):D({...h.current,errors:m.current,fieldStates:C.current,formState:z()},e),[]),F=Me({getWatchValue:G}),{watch:se,registerHookWatcher:K,subscribe:ne}=We({getWatchValue:G,channels:F}),le=n.useCallback((e,s)=>{typeof s=="function"&&(s=s(z(e)));const b=E.current,f=D(b,e);f!==s&&(E.current=I(E.current,e,s),F.publish(`formState.${e}`,s,f))},[]),H=n.useCallback((e,s)=>{typeof s=="function"&&(s=s(x(e))),W(e,"error",s)},[]),J=n.useCallback(e=>{W(e,"error",null)},[]),oe=n.useCallback(()=>{E.current.isError&&Object.keys(m.current).forEach(e=>J(e))},[]),W=n.useCallback((e,s,b)=>{try{if(typeof b=="function"&&(b=b(Y(e))),R.current.has(e))if(typeof b=="object"&&b!==null){Object.keys(b).forEach(_=>{W(`${e}.${_}`,s,b[_])});return}else return console.error(`Cannot set primitive value for nested parent field "${e}". Please set value for its children or provide an object.`);const f=D(C.current,e)||{...fe},$=D(f,s);if($!==b){if(C.current=I(C.current,`${e}.${s}`,b),F.publish(`fieldStates.${e}.${s}`,b,$),s==="error"){const _=D(m.current,e),U=b||null;_!==U&&(m.current=I(m.current,e,U),F.publish(`errors.${e}`,U,_));const B=pe(R.current,m.current);E.current.errorFields=B,F.publish("formState.errorFields",B);const Z=B.length>0;if(E.current.isError!==Z){const be=E.current.isError;E.current.isError=Z,F.publish("formState.isError",Z,be)}}if(s==="isDirty"){const _=ye(R.current,C.current);E.current.dirtyFields=_,F.publish("formState.dirtyFields",_);const U=_.length>0;if(E.current.isDirty!==U){const B=E.current.isDirty;E.current.isDirty=U,F.publish("formState.isDirty",U,B)}}if(s==="isTouched"){const _=ke(R.current,C.current);E.current.touchedFields=_,F.publish("formState.touchedFields",_)}}}catch(f){console.log(f)}},[]),ue=n.useCallback(e=>{W(e,"isDirty",!1),W(e,"isTouched",!1),W(e,"error",!1)},[]),ce=n.useCallback(e=>{const s=Ce(e.target);s?H(e.target.name,s):J(e.target.name)},[]),r=n.useCallback((e,s,{shouldDirty:b=!0,shouldOnChange:f=!0}={})=>{if(typeof s=="function"&&(s=s(O(e))),R.current.has(e)){typeof s=="object"&&s!==null?Object.keys(s).forEach(B=>{r(`${e}.${B}`,s[B],{shouldDirty:b,shouldOnChange:f})}):console.error(`Cannot set primitive value for nested parent field "${e}". Please set value for its children or provide an object.`);return}const $=D(h.current,e);if(s===$)return;h.current=I(h.current,e,s),F.publish(`values.${e}`,s,$);const _=u.current.get(e);if(_&&_(s),e.includes(".")){const B=e.split("."),Z=B.findIndex((be,Te)=>!R.current.has(B.slice(0,Te+1).join(".")));Z!==-1&&(e=B.slice(0,Z+1).join("."))}s&&W(e,"isTouched",!0);const U=s!==(D(l.current,e)||"");b&&W(e,"isDirty",U),f&&F.publish("onChange",e,s,$)},[]),g=n.useCallback(e=>{const s=e.split(".");let b="";s.forEach((f,$)=>{$<s.length-1&&(b=b?`${b}.${f}`:f,R.current.add(b))})},[]),T=n.useCallback(e=>{Array.isArray(e)?e.forEach(s=>g(s+".")):g(e+".")},[]),P=n.useCallback((e="",s,{shouldUnRegister:b=t}={})=>{if(!e){console.error("Controller must have a name");return}return u.current.set(e,s),C.current=I(C.current,e,{...fe}),e.includes(".")&&g(e),()=>{b&&u.current.delete(e)}},[]),L=n.useCallback((e,{keepError:s,keepDirty:b,keepTouched:f,defaultValue:$})=>{const _=u.current.get(e);if(!_)throw new Error(`Cannot reset "${e}" because it's uncontrolled field`);l.current=I(l.current,e,$),h.current=I(h.current,e,$),_&&_($),s||J(e),b||W(e,"isDirty",!1),f||W(e,"isTouched",!1)},[]),M=n.useCallback((e,{bubble:s,trickle:b}={})=>{if(!e)return F.trigger("values",{bubble:s,trickle:b??!0});if(!Array.isArray(e)){const f=q(e);F.trigger(f,{bubble:s,trickle:b});return}e.forEach(f=>{const $=q(f);F.trigger($,{bubble:s,trickle:b})})},[]),X=n.useCallback(e=>{F.publish(`blur.values.${e}`,O(e)),F.publish(`blur.errors.${e}`,x(e));const s=Y(e);Object.keys(s||{}).forEach(f=>{F.publish(`blur.fieldStates.${e}.${f}`,s[f])});const b=z();Object.keys(b||{}).forEach(f=>{F.publish(`blur.formState.${f}`,b[f])})},[]),Q=n.useCallback((e,s=O(e))=>{F.publish("onBlur",e,s,O()),X(e)},[]),w=n.useMemo(()=>({subscribe:ne,reset:y,trigger:M,reload:a,resetField:L,setValue:r,getValues:O,getErrors:x,getFieldStates:Y,getFormState:z,setError:H,clearError:J,clearErrors:oe,checkValidity:ce,getFieldValidity:N,getDefaultValues:j,setFieldState:W,triggerFieldBlur:Q,resetFieldState:ue,getControlledFields:k,setFormState:le,getEvents:F.getEvents,getWatchEvents:F.getWatchEvents,addGroups:T}),[i]),V=n.useCallback(()=>{[...d.current.querySelectorAll("[name]")].forEach(b=>{const f=b.name||"";f.includes(".")&&g(f),!b.defaultValue&&!u.current.has(f)&&D(l.current,f)&&(b.defaultValue=D(l.current,f))});const s=v();h.current={...s},l.current={...s}},[]),ee=n.useMemo(()=>({ref:d,watch:se,actions:w,initForm:V,registerController:P,registerHookWatcher:K,lastReloadedAt:i,loadFormValues:v,getWatchValue:G,triggerBlurWatchers:X,channels:F}),[i]),ie=n.useMemo(()=>({control:ee,actions:w,watch:se}),[i]);return n.useLayoutEffect(()=>{d.current&&V()},[i]),n.useLayoutEffect(()=>{C.current=A(!1)},[]),ie},Ie=({control:o,name:t,compute:c,mode:i})=>{const{getWatchValue:a,registerHookWatcher:d}=o||de(),u=a({name:t,compute:c}),[m,C]=n.useState(u);return n.useEffect(()=>d({name:t,compute:c,value:m,setValue:C,mode:i}),[]),m};exports.Controller=Ne;exports.Form=je;exports.restoreFromDotNotation=Se;exports.useController=Fe;exports.useForm=Be;exports.useFormContext=de;exports.useWatch=Ie;
|