react-simple-formkit 2.4.4 → 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 +14 -9
- package/dist/react-simple-formkit.js +4 -4
- package/dist/react-simple-formkit.mjs +497 -468
- package/dist/react-simple-formkit.umd.js +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -189,9 +189,9 @@ State updates only when observed via `watch()`, `useWatch()`, `subscribe()`, or
|
|
|
189
189
|
- [onBlur](#form) is just a handler callback that is called when field blurred.
|
|
190
190
|
- [actions.subscribe('onChange', callback)](#useform) subscribe onChange callback instead of passing `onChange` in [Form](#form) component.
|
|
191
191
|
- [actions.subscribe('onBlur', callback)](#useform) subscribe onBlur callback instead of passing `onBlur` in [Form](#form) component.
|
|
192
|
-
- [actions.trigger(name)](#
|
|
192
|
+
- [actions.trigger(name, {bubble, trickle})](#useform) trigger watchers (e.g. `watch`, `useWatch`, `subscribe`) re-update values if needed.
|
|
193
193
|
|
|
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.
|
|
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
195
|
> actions.setValue() makes onChange by default, if you call it in onChange callback so it will make infinite loop.
|
|
196
196
|
|
|
197
197
|
### The Power of watch (Unified Observation)
|
|
@@ -473,7 +473,7 @@ Generic props:
|
|
|
473
473
|
Return:
|
|
474
474
|
|
|
475
475
|
- `control`: contains methods and utilities to control the form.
|
|
476
|
-
- `watch(name)`: `
|
|
476
|
+
- `watch(name, mode)`: `(name: String | Array | undefined, mode: "onChange" | "onBlur") => Object` [Example](#managing-values)
|
|
477
477
|
- `actions` is an object that contains utilities
|
|
478
478
|
- `actions.reset()`: `(newDefaultValues?: Object, options?: { clearCustomFormStates?: Boolean, clearCustomFieldStates?: Boolean }) => void` [Example](#default-values-and-reset)
|
|
479
479
|
- `actions.getValues()`: `(name?: String | Array) => Object` [Example](#managing-values)
|
|
@@ -484,7 +484,7 @@ Return:
|
|
|
484
484
|
- `actions.setError()`: `(name: String, error: Any) => void` [Example](#set-field-error)
|
|
485
485
|
- `actions.setFieldState()`: `(name: String, property: String, value: Any) => void` [Example](#set-field-states)
|
|
486
486
|
- `actions.setFormState()`: `(name: String, value: Any) => void`
|
|
487
|
-
- `actions.triggerFieldBlur
|
|
487
|
+
- `actions.triggerFieldBlur `(name: String, value?: Any) => void`. Used to trigger the blur event for a field manually.
|
|
488
488
|
- `actions.resetFieldState()`: `(name: String) => void`
|
|
489
489
|
- `actions.resetField()`: `(name: String) => void`
|
|
490
490
|
- `actions.clearError()`: `(name: String | Array) => void`
|
|
@@ -493,7 +493,9 @@ Return:
|
|
|
493
493
|
- `actions.subscribeBlur()`: `(callback) => unsubscribe: Function()`
|
|
494
494
|
- `actions.getNumberFields()`: `() => Array`
|
|
495
495
|
- `actions.getDefaultValues()`: `() => Object`
|
|
496
|
-
- `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.).
|
|
497
499
|
- `actions.addGroups()`: `(name: String | Array) => void` [Example](#registering)
|
|
498
500
|
|
|
499
501
|
## Form
|
|
@@ -533,6 +535,7 @@ Return: `render` arguments in `<Controller>`
|
|
|
533
535
|
Generic props:
|
|
534
536
|
|
|
535
537
|
- `name`: `String | Array | undefined`. If undefined, it will return all input values
|
|
538
|
+
- `mode`: `"onChange" | "onBlur"`. Default is `onChange`
|
|
536
539
|
- `compute`: `Function` that will calculate from form values and return a value. It will make re-render when the result changes
|
|
537
540
|
- `control`: received from useForm(). Pass it if use Controller outside `<Form>`
|
|
538
541
|
|
|
@@ -547,14 +550,15 @@ Return:
|
|
|
547
550
|
|
|
548
551
|
Return:
|
|
549
552
|
|
|
550
|
-
- `watch`
|
|
551
|
-
- `actions`
|
|
553
|
+
- `watch(name, mode)`
|
|
554
|
+
- `actions`: same with `useForm().actions`
|
|
552
555
|
|
|
553
556
|
## watch
|
|
554
557
|
|
|
555
|
-
Arguments:
|
|
558
|
+
Arguments: `watch(name, mode)`
|
|
556
559
|
|
|
557
560
|
- `name`: `String | Array | undefined`. If undefined, it will return all input values
|
|
561
|
+
- `mode`: `"onChange" | "onBlur"`. Default is `onChange`
|
|
558
562
|
|
|
559
563
|
Return:
|
|
560
564
|
|
|
@@ -562,10 +566,11 @@ Return:
|
|
|
562
566
|
|
|
563
567
|
## subscribe
|
|
564
568
|
|
|
565
|
-
Arguments:
|
|
569
|
+
Arguments: `subscribe(name, callback, mode)`
|
|
566
570
|
|
|
567
571
|
- `name`: `String | Array | undefined`. If undefined, it will return all input values
|
|
568
572
|
- `callback()`: `Function` receive value based on name parameter. Only be called when the value changes
|
|
573
|
+
- `mode`: `"onChange" | "onBlur"`. Default is `onChange`
|
|
569
574
|
|
|
570
575
|
Return:
|
|
571
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,_),ue[_+O]=!0)}if(_=null,p!==void 0&&(c(p),_=""+p),u(b)&&(c(b.key),_=""+b.key),"key"in b){p={};for(var Q in b)Q!=="key"&&(p[Q]=b[Q])}else p=b;return _&&h(p,typeof r=="function"?r.displayName||r.name||"Unknown":r),d(r,_,x,Y,a(),p,Z,B)}function g(r){typeof r=="object"&&r!==null&&r.$$typeof===T&&r._store&&(r._store.validated=1)}var v=s,T=Symbol.for("react.transitional.element"),S=Symbol.for("react.portal"),C=Symbol.for("react.fragment"),M=Symbol.for("react.strict_mode"),A=Symbol.for("react.profiler"),D=Symbol.for("react.consumer"),P=Symbol.for("react.context"),I=Symbol.for("react.forward_ref"),J=Symbol.for("react.suspense"),ae=Symbol.for("react.suspense_list"),re=Symbol.for("react.memo"),L=Symbol.for("react.lazy"),k=Symbol.for("react.activity"),te=Symbol.for("react.client.reference"),X=v.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,se=Object.prototype.hasOwnProperty,le=Array.isArray,z=console.createTask?console.createTask:function(){return null};v={"react-stack-bottom-frame":function(r){return r()}};var G,ne={},N=v["react-stack-bottom-frame"].bind(v,f)(),oe=z(i(f)),ue={};ee.Fragment=C,ee.jsx=function(r,b,p,O,Y){var x=1e4>X.recentlyCreatedOwnerStacks++;return l(r,b,p,!1,O,Y,x?Error("react-stack-top-frame"):N,x?z(i(r)):oe)},ee.jsxs=function(r,b,p,O,Y){var x=1e4>X.recentlyCreatedOwnerStacks++;return l(r,b,p,!0,O,Y,x?Error("react-stack-top-frame"):N,x?z(i(r)):oe)}}()),ee}var ge;function _e(){return ge||(ge=1,process.env.NODE_ENV==="production"?ce.exports=Te():ce.exports=Ae()),ce.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:i,children:a,onChange:f,onBlur:u,onSubmit:h=()=>{},onInput:R=()=>{},onReset:d=()=>{},numberFields:l=[],className:g,...v})=>{const T=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:T,action:i,method:c,className:g,onInput:R,onSubmit:S=>{c||S.preventDefault();const C=t.loadFormValues();h(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 M=S.target.value;t.channels.publish("onBlur",C,M,t.actions.getValues())},onReset:S=>{t.actions.reset(),d(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"],j=(n={},t="")=>t.split(".").reduce((c,i)=>c&&c[i]!==void 0?c[i]:void 0,n),W=(n={},t="",c)=>{const i=Array.isArray(n)?[...n]:{...n};let a=i;const f=t.split(".");return f.forEach((u,h)=>{h<f.length-1?(a[u]||(a[u]=/^\d+$/.test(f[h+1])?[]:{}),Array.isArray(a[u])?a[u]=[...a[u]]:a[u]={...a[u]},a=a[u]):a[u]=c}),i},Ce=n=>{let t=Array.isArray(n)?[...n]:{...n};return Object.keys(t).forEach(c=>{if(c.includes(".")){const i=j(t,c);t=W(t,c,i||""),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(i=>t[i]);if(c)return{type:c,message:n.validationMessage}},Se=(n=new Set,t={},c="")=>Object.keys(t||{}).reduce((i,a)=>{const f=(t||{})[a];return n.has(c+a)?[...i,...Se(n,f,c+a+".")]:f!=null&&f.isDirty?[...i,c+a]:i},[]),pe=(n=new Set,t={},c="")=>Object.keys(t||{}).reduce((i,a)=>{const f=(t||{})[a];return n.has(c+a)?[...i,...pe(n,f,c+a+".")]:f!=null&&f.isTouched?[...i,c+a]:i},[]),ye=(n=new Set,t={},c="")=>Object.keys(t||{}).reduce((i,a)=>{const f=(t||{})[a];return n.has(c+a)?[...i,...ye(n,f,c+a+".")]:f?[...i,c+a]:i},[]),ke=({control:n,name:t,defaultValue:c,shouldUnRegister:i})=>{const{actions:a,registerController:f,channels:u,getWatchValue:h}=n||fe(),R=s.useRef(),d=s.useRef(),l=j(a.getDefaultValues(),t)||c||"",[g,v]=s.useState(l),[T,S]=s.useState({}),C=s.useCallback((A,{shouldDirty:D=!0,shouldOnChange:P=!0}={})=>{var J;let I=A;((J=A==null?void 0:A.target)==null?void 0:J.value)!==void 0&&(I=A.target.value),v(I),a.setValue(t,I,{shouldDirty:D,shouldOnChange:P})},[a.setValue]),M=s.useCallback(A=>{const D=a.getValues(),P=A??j(D,t);u.publish("onBlur",t,P,D)},[]);return s.useEffect(()=>{const A=f(t,v,{shouldUnRegister:i});return()=>{A(),d.current&&d.current()}},[i]),new Proxy({ref:R,name:t,defaultValue:l,value:g,onChange:C,onBlur:M,fieldState:T},{get(A,D,P){return typeof D=="string"&&D==="fieldState"&&(d.current&&d.current(),d.current=a.subscribe(`fieldStates.${t}`,()=>{S(h({name:`fieldStates.${t}`}))})),Reflect.get(A,D,P)}})},Ne=({name:n,control:t,shouldUnRegister:c,defaultValue:i,render:a=({ref:f,name:u,defaultValue:h,value:R,onChange:d,onBlur:l,fieldState:g})=>null})=>{const f=ke({name:n,defaultValue:i,shouldUnRegister:c,control:t});return a(f)},De=()=>{const[n,t]=s.useState(),c=s.useCallback(()=>t(new Date().toString()),[]);return[n,c]},xe=()=>{const[,n]=s.useState({});return s.useCallback(()=>n({}),[])},$e=({channels:n,getWatchValue:t})=>{const c=xe(),i=s.useCallback(u=>{if(!u)return n.subscribeWatch("values",()=>c()),t();if(typeof u=="string"){const h=q(u);n.subscribeWatch(h,(R,d)=>{R!==d&&c()})}return Array.isArray(u)&&u.forEach(h=>{const R=q(h);n.subscribeWatch(R,(d,l)=>{d!==l&&c()})}),t({name:u})},[]),a=s.useCallback(({name:u,compute:h,setValue:R})=>{if(typeof h=="function")return n.subscribe("values",()=>{const l=t({compute:h});R(l)});if(!u)return n.subscribe("values",()=>{R(t())});if(typeof u=="string"){const d=q(u);return n.subscribe(d,()=>{const g=t({name:u});R(g)})}if(Array.isArray(u)){const d=[];return u.forEach(l=>{const g=q(l),v=n.subscribe(g,()=>{const T=t({name:u});R(T)});d.push(v)}),()=>d.forEach(l=>l())}throw new Error("Parameters of name must be string or array of string or compute must be a function")},[]),f=s.useCallback((u,h)=>{if(!u)return n.subscribe("values",()=>h(t()));if(["onChange","onBlur"].includes(u))return n.subscribe(u,h);if(typeof u=="string"){const R=q(u);return n.subscribe(R,()=>h(t({name:u})))}if(Array.isArray(u)){const R=[];return u.forEach(d=>{const l=q(d),g=n.subscribe(l,()=>{h(t({name:u}))});R.push(g)}),()=>R.forEach(d=>d())}throw new Error("Parameters of name must be string or array of string")},[]);return{watch:i,registerHookWatcher:a,getWatchValue:t,subscribe:f}},Me=({getWatchValue:n})=>{const t=s.useRef(new Map),c=s.useRef(new Map),i=s.useCallback(()=>c.current,[]),a=s.useCallback(()=>t.current,[]),f=s.useCallback(()=>{t.current.clear(),c.current.clear()},[]),u=s.useCallback((l,...g)=>{const v=t.current.get(l),T=c.current.get(l);v&&v.forEach(S=>S(...g)),T&&T.forEach(S=>S(...g)),t.current.forEach((S,C)=>{if(C!==l&&l.startsWith(C)){const M=n({name:C.replace("values.","").replace("values","")});S.forEach(A=>A(W(M,l.replace(`${C}.`,""),g[0])))}}),c.current.forEach((S,C)=>{if(C!==l&&l.startsWith(C)){const M=n({name:C.replace("values.","").replace("values","")});S.forEach(A=>A(W(M,l.replace(`${C}.`,""),g[0])))}})},[]),h=s.useCallback(l=>{t.current.forEach((g,v)=>{v.startsWith(l)&&g.forEach(T=>T(n({name:v.replace("values.","").replace("values","")})))}),c.current.forEach((g,v)=>{v.startsWith(l)&&g.forEach(T=>T(n({name:v.replace("values.","").replace("values","")})))})},[]),R=s.useCallback((l,g)=>(t.current.has(l)||t.current.set(l,new Set),t.current.get(l).add(g),()=>{var v;return(v=t.current.get(l))==null?void 0:v.delete(g)}),[]),d=s.useCallback((l,g)=>{c.current.has(l)||(c.current.set(l,new Set),c.current.get(l).add(g))},[]);return{reset:f,publish:u,subscribe:R,subscribeWatch:d,getEvents:a,getWatchEvents:i,trigger:h}},We=({defaultValues:n=we,shouldUnRegister:t=!1,groups:c=[]}={})=>{const[i,a]=De(),f=s.useRef(),u=s.useRef(new Map),h=s.useRef({}),R=s.useRef({}),d=s.useRef({...me}),l=s.useRef({...n}),g=s.useRef({...n}),v=s.useRef(new Set(c)),T=s.useCallback((e,o=g.current)=>Object.keys(o).reduce((E,m)=>{if(typeof o[m]=="object")return{...E,[m]:T(e,o[m])};const w={...e?{}:R.current[m]||{},...ie};return{...E,[m]:w}},{}),[]),S=s.useCallback((e={},{clearCustomFormStates:o=!1,clearCustomFieldStates:E=!1,groups:m=v.current}={})=>{g.current={...g.current,...e},l.current={...g.current},u.current.forEach((w,F)=>{w(j(g.current,F)??"")}),h.current={},v.current=new Set(m),u.current=new Map,d.current={...o?{}:d.current,...me},R.current=T(E),k.reset(),a()},[]),C=s.useCallback(()=>{let e=Object.fromEntries(new FormData(f.current));u.current.forEach((E,m)=>{e=W(e,m,j(l.current,m))});let o={...l.current,...e};return Ce(o)},[]),M=s.useCallback(e=>Re(e.target),[]),A=s.useCallback(()=>g.current,[]),D=s.useCallback(()=>u.current,[]),P=s.useCallback((e,o)=>o?Array.isArray(o)?o.reduce((E,m)=>({...E,[m]:j(e,m)}),{}):j(e,o):{...e},[]),I=s.useCallback(e=>P(l.current,e),[P]),J=s.useCallback(e=>P(h.current,e),[P]),ae=s.useCallback(e=>P(R.current,e),[P]),re=s.useCallback(e=>P({...d.current,lastReset:i},e),[i]),L=s.useCallback(({name:e,compute:o}={})=>!e&&!o||e==="values"?I():typeof o=="function"?o(I()):Array.isArray(e)?e.reduce((E,m)=>({...E,[m]:L({name:m})}),{}):j({...l.current,errors:h.current,fieldStates:R.current,formState:re()},e),[]),k=Me({getWatchValue:L}),{watch:te,registerHookWatcher:X,subscribe:se}=$e({getWatchValue:L,channels:k}),le=s.useCallback((e,o)=>{const E=d.current,m=j(E,e);m!==o&&(d.current=W(d.current,e,o),k.publish(`formState.${e}`,o,m))},[]),z=s.useCallback((e,o)=>{N(e,"error",o)},[]),G=s.useCallback(e=>{N(e,"error",null)},[]),ne=s.useCallback(()=>{d.current.isError&&Object.keys(h.current).forEach(e=>G(e))},[]),N=s.useCallback((e,o,E)=>{try{if(typeof E=="function"&&(E=E(L({name:e}))),v.current.has(e))if(typeof E=="object"&&E!==null){Object.keys(E).forEach(F=>{N(`${e}.${F}`,o,E[F])});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 m=j(R.current,e)||{...ie},w=j(m,o);if(w!==E){if(R.current=W(R.current,`${e}.${o}`,E),k.publish(`fieldStates.${e}.${o}`,E,w),o==="error"){const F=j(h.current,e),V=E||null;F!==V&&(h.current=W(h.current,e,V),k.publish(`errors.${e}`,V,F));const $=ye(v.current,h.current);d.current.errorFields=$,k.publish("formState.errorFields",$);const H=$.length>0;if(d.current.isError!==H){const de=d.current.isError;d.current.isError=H,k.publish("formState.isError",H,de)}}if(o==="isDirty"){const F=Se(v.current,R.current);d.current.dirtyFields=F,k.publish("formState.dirtyFields",F);const V=F.length>0;if(d.current.isDirty!==V){const $=d.current.isDirty;d.current.isDirty=V,k.publish("formState.isDirty",V,$)}}if(o==="isTouched"){const F=pe(v.current,R.current);d.current.touchedFields=F,k.publish("formState.touchedFields",F)}}}catch(m){console.log(m)}},[]),oe=s.useCallback(e=>{N(e,"isDirty",!1),N(e,"isTouched",!1),N(e,"error",!1)},[]),ue=s.useCallback(e=>{const o=Re(e.target);o?z(e.target.name,o):G(e.target.name)},[]),r=s.useCallback((e,o,{shouldDirty:E=!0,shouldOnChange:m=!0}={})=>{if(typeof o=="function"&&(o=o(I(e))),v.current.has(e)){typeof o=="object"&&o!==null?Object.keys(o).forEach($=>{r(`${e}.${$}`,o[$],{shouldDirty:E,shouldOnChange:m})}):console.error(`Cannot set primitive value for nested parent field "${e}". Please set value for its children or provide an object.`);return}const w=j(l.current,e);if(o===w)return;l.current=W(l.current,e,o),k.publish(`values.${e}`,o,w);const F=u.current.get(e);if(F&&F(o),e.includes(".")){const $=e.split("."),H=$.findIndex((de,Fe)=>!v.current.has($.slice(0,Fe+1).join(".")));H!==-1&&(e=$.slice(0,H+1).join("."))}o&&N(e,"isTouched",!0);const V=o!==(j(g.current,e)||"");E&&N(e,"isDirty",V),m&&k.publish("onChange",e,o,w)},[]),b=s.useCallback(e=>{const o=e.split(".");let E="";o.forEach((m,w)=>{w<o.length-1&&(E=E?`${E}.${m}`:m,v.current.add(E))})},[]),p=s.useCallback(e=>{Array.isArray(e)?e.forEach(o=>b(o+".")):b(e+".")},[]),O=s.useCallback((e="",o,{shouldUnRegister:E=t}={})=>{if(!e){console.error("Controller must have a name");return}return u.current.set(e,o),R.current=W(R.current,e,{...ie}),e.includes(".")&&b(e),()=>{E&&u.current.delete(e)}},[]),Y=s.useCallback((e,{keepError:o,keepDirty:E,keepTouched:m,defaultValue:w})=>{const F=u.current.get(e);if(!F)throw new Error(`Cannot reset "${e}" because it's uncontrolled field`);g.current=W(g.current,e,w),l.current=W(l.current,e,w),F&&F(w),o||G(e),E||N(e,"isDirty",!1),m||N(e,"isTouched",!1)},[]),x=s.useCallback(e=>{if(!e)return k.trigger("values");if(!Array.isArray(e)){const o=q(e);k.trigger(o);return}e.forEach(o=>{const E=q(o);k.trigger(E)})},[]),Z=s.useCallback((e,o=B.getValues(e))=>{k.publish("onBlur",e,o,B.getValues())},[]),B=s.useMemo(()=>({subscribe:se,reset:S,trigger:x,reload:a,resetField:Y,setValue:r,getValues:I,getErrors:J,getFieldStates:ae,getFormState:re,setError:z,clearError:G,clearErrors:ne,checkValidity:ue,getFieldValidity:M,getDefaultValues:A,setFieldState:N,triggerFieldBlur:Z,resetFieldState:oe,getControlledFields:D,setFormState:le,getEvents:k.getEvents,getWatchEvents:k.getWatchEvents,addGroups:p}),[i]),_=s.useCallback(()=>{[...f.current.querySelectorAll("[name]")].forEach(E=>{const m=E.name||"";m.includes(".")&&b(m),!E.defaultValue&&!u.current.has(m)&&j(g.current,m)&&(E.defaultValue=j(g.current,m))});const o=C();l.current={...o},g.current={...o}},[]),U=s.useMemo(()=>({ref:f,watch:te,actions:B,initForm:_,registerController:O,registerHookWatcher:X,lastReloadedAt:i,loadFormValues:C,getWatchValue:L,channels:k}),[i]),Q=s.useMemo(()=>({control:U,actions:B,watch:te}),[i]);return s.useLayoutEffect(()=>{f.current&&_()},[i]),s.useLayoutEffect(()=>{R.current=T(!1)},[]),Q},Ie=({control:n,name:t,compute:c})=>{const{getWatchValue:i,registerHookWatcher:a}=n||fe(),f=i({name:t,compute:c}),[u,h]=s.useState(f);return s.useEffect(()=>a({name:t,compute:c,value:u,setValue:h}),[]),u};exports.Controller=Ne;exports.Form=Oe;exports.restoreFromDotNotation=Ce;exports.useController=ke;exports.useForm=We;exports.useFormContext=fe;exports.useWatch=Ie;
|
|
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;
|