@a-ludi/rjsf-formulas 0.1.2 → 0.1.3

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/dist/index.cjs CHANGED
@@ -1,22 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react"),pe=require("@rjsf/core"),H=Symbol("arrayIndex");function le(r,t){const o=(t==null?void 0:t.formulaKey)??"x-formula",s=(t==null?void 0:t.formulaContextKey)??"x-formula-context",n=[];return B(r,[],n,o,s),n}const me=["$ref","oneOf","anyOf","allOf"];function ye(r,t){return r===void 0||r==="siblings"?"siblings":r==="extended"?"extended":(console.warn(`[rjsf-formulas] Unknown x-formula-context value "${r}" at path [${re(t)}], treating as "siblings"`),"siblings")}function re(r){return r.map(t=>typeof t=="symbol"?"[*]":String(t)).join(", ")}function B(r,t,o,s,n){for(const a of me)a in r&&console.warn(`[rjsf-formulas] Schema composition operator "${a}" at path [${re(t)}] is not supported and will be skipped. See issue 06.`);if(s in r){const a=r[s];if(typeof a!="string"){console.warn(`[rjsf-formulas] Formula at path [${re(t)}] is not a string (got ${typeof a}), skipping.`);return}o.push({path:t,formula:a,contextMode:ye(r[n],t)});return}if(r.type==="object"&&r.properties)for(const[a,c]of Object.entries(r.properties))B(c,[...t,a],o,s,n);if(r.type==="array"){const a=r;Array.isArray(a.prefixItems)&&a.prefixItems.forEach((i,f)=>{B(i,[...t,f],o,s,n)}),r.items&&!Array.isArray(r.items)&&B(r.items,[...t,H],o,s,n)}}function _e(r){return r&&r.__esModule&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r}var G={exports:{}},U={};/**
2
- * @license React
3
- * react-jsx-runtime.production.js
4
- *
5
- * Copyright (c) Meta Platforms, Inc. and affiliates.
6
- *
7
- * This source code is licensed under the MIT license found in the
8
- * LICENSE file in the root directory of this source tree.
9
- */var ae;function Re(){if(ae)return U;ae=1;var r=Symbol.for("react.transitional.element"),t=Symbol.for("react.fragment");function o(s,n,a){var c=null;if(a!==void 0&&(c=""+a),n.key!==void 0&&(c=""+n.key),"key"in n){a={};for(var i in n)i!=="key"&&(a[i]=n[i])}else a=n;return n=a.ref,{$$typeof:r,type:s,key:c,ref:n!==void 0?n:null,props:a}}return U.Fragment=t,U.jsx=o,U.jsxs=o,U}var W={};/**
10
- * @license React
11
- * react-jsx-runtime.development.js
12
- *
13
- * Copyright (c) Meta Platforms, Inc. and affiliates.
14
- *
15
- * This source code is licensed under the MIT license found in the
16
- * LICENSE file in the root directory of this source tree.
17
- */var ue;function be(){return ue||(ue=1,process.env.NODE_ENV!=="production"&&(function(){function r(e){if(e==null)return null;if(typeof e=="function")return e.$$typeof===$?null:e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case g:return"Fragment";case D:return"Profiler";case I:return"StrictMode";case S:return"Suspense";case O:return"SuspenseList";case P:return"Activity"}if(typeof e=="object")switch(typeof e.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),e.$$typeof){case A:return"Portal";case w:return e.displayName||"Context";case T:return(e._context.displayName||"Context")+".Consumer";case j:var u=e.render;return e=e.displayName,e||(e=u.displayName||u.name||"",e=e!==""?"ForwardRef("+e+")":"ForwardRef"),e;case C:return u=e.displayName||null,u!==null?u:r(e.type)||"Memo";case M:u=e._payload,e=e._init;try{return r(e(u))}catch{}}return null}function t(e){return""+e}function o(e){try{t(e);var u=!1}catch{u=!0}if(u){u=console;var m=u.error,y=typeof Symbol=="function"&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object";return m.call(u,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",y),t(e)}}function s(e){if(e===g)return"<>";if(typeof e=="object"&&e!==null&&e.$$typeof===M)return"<...>";try{var u=r(e);return u?"<"+u+">":"<...>"}catch{return"<...>"}}function n(){var e=x.A;return e===null?null:e.getOwner()}function a(){return Error("react-stack-top-frame")}function c(e){if(K.call(e,"key")){var u=Object.getOwnPropertyDescriptor(e,"key").get;if(u&&u.isReactWarning)return!1}return e.key!==void 0}function i(e,u){function m(){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)",u))}m.isReactWarning=!0,Object.defineProperty(e,"key",{get:m,configurable:!0})}function f(){var e=r(this.type);return V[e]||(V[e]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),e=this.props.ref,e!==void 0?e:null}function d(e,u,m,y,X,Z){var _=m.ref;return e={$$typeof:E,type:e,key:u,props:m,_owner:y},(_!==void 0?_:null)!==null?Object.defineProperty(e,"ref",{enumerable:!1,get:f}):Object.defineProperty(e,"ref",{enumerable:!1,value:null}),e._store={},Object.defineProperty(e._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(e,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(e,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:X}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:Z}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}function p(e,u,m,y,X,Z){var _=u.children;if(_!==void 0)if(y)if(N(_)){for(y=0;y<_.length;y++)R(_[y]);Object.freeze&&Object.freeze(_)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else R(_);if(K.call(u,"key")){_=r(e);var Y=Object.keys(u).filter(function(de){return de!=="key"});y=0<Y.length?"{key: someKey, "+Y.join(": ..., ")+": ...}":"{key: someKey}",se[_+y]||(Y=0<Y.length?"{"+Y.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX:
18
- let props = %s;
19
- <%s {...props} />
20
- React keys must be passed directly to JSX without using spread:
21
- let props = %s;
22
- <%s key={someKey} {...props} />`,y,_,Y,_),se[_+y]=!0)}if(_=null,m!==void 0&&(o(m),_=""+m),c(u)&&(o(u.key),_=""+u.key),"key"in u){m={};for(var Q in u)Q!=="key"&&(m[Q]=u[Q])}else m=u;return _&&i(m,typeof e=="function"?e.displayName||e.name||"Unknown":e),d(e,_,m,n(),X,Z)}function R(e){b(e)?e._store&&(e._store.validated=1):typeof e=="object"&&e!==null&&e.$$typeof===M&&(e._payload.status==="fulfilled"?b(e._payload.value)&&e._payload.value._store&&(e._payload.value._store.validated=1):e._store&&(e._store.validated=1))}function b(e){return typeof e=="object"&&e!==null&&e.$$typeof===E}var v=l,E=Symbol.for("react.transitional.element"),A=Symbol.for("react.portal"),g=Symbol.for("react.fragment"),I=Symbol.for("react.strict_mode"),D=Symbol.for("react.profiler"),T=Symbol.for("react.consumer"),w=Symbol.for("react.context"),j=Symbol.for("react.forward_ref"),S=Symbol.for("react.suspense"),O=Symbol.for("react.suspense_list"),C=Symbol.for("react.memo"),M=Symbol.for("react.lazy"),P=Symbol.for("react.activity"),$=Symbol.for("react.client.reference"),x=v.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,K=Object.prototype.hasOwnProperty,N=Array.isArray,h=console.createTask?console.createTask:function(){return null};v={react_stack_bottom_frame:function(e){return e()}};var J,V={},k=v.react_stack_bottom_frame.bind(v,a)(),L=h(s(a)),se={};W.Fragment=g,W.jsx=function(e,u,m){var y=1e4>x.recentlyCreatedOwnerStacks++;return p(e,u,m,!1,y?Error("react-stack-top-frame"):k,y?h(s(e)):L)},W.jsxs=function(e,u,m){var y=1e4>x.recentlyCreatedOwnerStacks++;return p(e,u,m,!0,y?Error("react-stack-top-frame"):k,y?h(s(e)):L)}})()),W}var ce;function Ee(){return ce||(ce=1,process.env.NODE_ENV==="production"?G.exports=Re():G.exports=be()),G.exports}var ge=Ee(),ee,fe;function ve(){return fe||(fe=1,ee=function r(t,o){if(t===o)return!0;if(t&&o&&typeof t=="object"&&typeof o=="object"){if(t.constructor!==o.constructor)return!1;var s,n,a;if(Array.isArray(t)){if(s=t.length,s!=o.length)return!1;for(n=s;n--!==0;)if(!r(t[n],o[n]))return!1;return!0}if(t.constructor===RegExp)return t.source===o.source&&t.flags===o.flags;if(t.valueOf!==Object.prototype.valueOf)return t.valueOf()===o.valueOf();if(t.toString!==Object.prototype.toString)return t.toString()===o.toString();if(a=Object.keys(t),s=a.length,s!==Object.keys(o).length)return!1;for(n=s;n--!==0;)if(!Object.prototype.hasOwnProperty.call(o,a[n]))return!1;for(n=s;n--!==0;){var c=a[n];if(!r(t[c],o[c]))return!1}return!0}return t!==t&&o!==o}),ee}var Oe=ve();const je=_e(Oe);function Ae(r,t,o,s){const a=t.slice(0,-1).reduce((i,f)=>{if(!(i==null||typeof i!="object"))return i[f]},o),c=a!=null&&typeof a=="object"&&!Array.isArray(a)?{...a}:{};return r.contextMode==="extended"?((s.formulaDataKey in c||s.formulaPathKey in c)&&console.warn(`[rjsf-formulas] Extended context key collision: sibling field named "${s.formulaDataKey in c?s.formulaDataKey:s.formulaPathKey}" will be overwritten by the injected context. Use formulaDataKey/formulaPathKey props to choose a different key.`),{...c,[s.formulaDataKey]:o,[s.formulaPathKey]:[...t]}):c}function q(r,t){return t.reduce((o,s)=>{if(!(o==null||typeof o!="object"))return o[s]},r)}function te(r,t,o){if(t.length===0)return;const s=q(r,t.slice(0,-1));s!=null&&typeof s=="object"&&(s[t[t.length-1]]=o)}function F(r,t,o=[]){if(r.length===0)return[o];const[s,...n]=r;if(s===H){const a=q(t,o);if(!Array.isArray(a))return[];const c=[];for(let i=0;i<a.length;i++)c.push(...F(n,t,[...o,i]));return c}return F(n,t,[...o,s])}function oe(r,t){return je(r,t)}async function ie(r,t,o,s){const n=structuredClone(r),a=[];for(const f of t)for(const d of F(f.path,n)){const p=Ae(f,d,n,s);a.push({field:f,concretePath:d,context:p})}const c=await Promise.allSettled(a.map(({field:f,context:d})=>new Promise(p=>p(o(f.formula,d))))),i=[];for(let f=0;f<a.length;f++){const{concretePath:d}=a[f],p=c[f];if(p.status==="fulfilled")te(n,d,p.value);else{const R=p.reason instanceof Error?p.reason:new Error(String(p.reason));i.push({path:d,error:R}),te(n,d,void 0)}}return{data:n,errors:i}}function Te(r,t,o){for(const s of o)for(const n of F(s.path,t))if(!oe(q(r,n),q(t,n)))return!1;return!0}async function xe(r,t,o,s,n,a="__formData__",c="__path__"){if(t.length===0)return r;const i={formulaDataKey:a,formulaPathKey:c};let f=r;for(let R=0;R<s;R++){const{data:b,errors:v}=await ie(f,t,o,i);if(Te(f,b,t)){for(const{path:E,error:A}of v)n==null||n(E,A);return b}f=b}const{data:d}=await ie(f,t,o,i),p=structuredClone(d);for(const R of t)for(const b of F(R.path,d))oe(q(f,b),q(d,b))||(n==null||n(b,new Error(`[rjsf-formulas] Formula at [${b.join(", ")}] did not converge after ${s} passes`)),te(p,b,void 0));return p}function he(r){return JSON.stringify(r)}function ke(r,t,o,s,n,a,c,i){const[f,d]=l.useState(r),p=l.useRef(r),R=l.useRef(r),b=l.useRef(new Map),v=l.useRef(null),E=l.useRef(!1),A=l.useRef(a),g=l.useRef(c),I=l.useRef(o),D=l.useRef(t),T=l.useRef(n),w=l.useRef(i);l.useEffect(()=>{A.current=a},[a]),l.useEffect(()=>{g.current=c},[c]),l.useEffect(()=>{I.current=o},[o]),l.useEffect(()=>{D.current=t},[t]),l.useEffect(()=>{T.current=n},[n]),l.useEffect(()=>{w.current=i},[i]);const j=l.useCallback(()=>{if(E.current)return;(async()=>{var P,$;E.current=!0;try{for(;;){const x=p.current,K=D.current,N=[];for(const k of K)for(const L of F(k.path,x))N.push(L);const h=b.current;h.clear();for(const k of N)h.set(he(k),"running");N.length>0&&((P=g.current)==null||P.call(g,N));const J=await xe(x,K,I.current,T.current,A.current,w.current.formulaDataKey,w.current.formulaPathKey);if(C.current)break;const V=[...h.values()].some(k=>k==="dirty");if(h.clear(),!V){d(J),N.length>0&&(($=g.current)==null||$.call(g,[]));break}}}finally{E.current=!1}})()},[]),S=l.useCallback(M=>{p.current=M;const P=b.current;for(const[$,x]of P)x==="running"&&P.set($,"dirty");E.current||(v.current!==null&&clearTimeout(v.current),v.current=setTimeout(()=>{v.current=null,j()},s))},[s,j]),O=l.useRef(!1),C=l.useRef(!1);return l.useEffect(()=>{O.current?oe(r,R.current)||(R.current=r,S(r)):(O.current=!0,R.current=r,S(r))}),l.useEffect(()=>()=>{C.current=!0,v.current!==null&&clearTimeout(v.current)},[]),{enrichedFormData:f,handleInput:S}}function we(r,t){const o=structuredClone(r??{}),s=Se(t);for(const n of t)z(o,n.path,s);return o}function Se(r){const t=new Set;for(let o=0;o<r.length;o++){const s=r[o].path;for(let n=0;n<s.length;n++){const a=s[n];if(typeof a!="number"&&a!==H)continue;const c=ne(s.slice(0,n)),i=typeof a=="number";r.some((d,p)=>{if(p===o||d.path.length<=n||ne(d.path.slice(0,n))!==c)return!1;const R=d.path[n];return i?R===H:typeof R=="number"})&&t.add(c)}}return t}function ne(r){return JSON.stringify(r.map(t=>typeof t=="symbol"?"__ARRAY_INDEX__":t))}function z(r,t,o,s=[]){if(t.length===0){r["ui:readonly"]=!0;return}const[n,...a]=t;if(typeof n=="string"){(!r[n]||typeof r[n]!="object"||Array.isArray(r[n]))&&(r[n]={}),z(r[n],a,o,[...s,n]);return}if(typeof n=="number"){if(r.items!==void 0&&!Array.isArray(r.items)){console.warn("[rjsf-formulas] mergeReadOnly: cannot inject ui:readonly at tuple index because existing uiSchema has items as an object. Skipping.");return}Array.isArray(r.items)||(r.items=[]);const f=r.items;(!f[n]||typeof f[n]!="object")&&(f[n]={}),z(f[n],a,o,[...s,n]);return}const c=ne(s);if(o.has(c))(!r.additionalItems||typeof r.additionalItems!="object")&&(r.additionalItems={}),z(r.additionalItems,a,o,[...s,n]);else{if(Array.isArray(r.items)){console.warn("[rjsf-formulas] mergeReadOnly: cannot inject ui:readonly for uniform array because existing uiSchema has items as an array. Skipping.");return}(!r.items||typeof r.items!="object")&&(r.items={}),z(r.items,a,o,[...s,n])}}function Pe(r){const{schema:t,formData:o,uiSchema:s,evaluator:n,Form:a=pe,formulaKey:c="x-formula",formulaContextKey:i="x-formula-context",formulaDataKey:f="__formData__",formulaPathKey:d="__path__",maxConvergencePasses:p=10,debounceMs:R=300,onFormulaError:b,onLoadingChange:v,onChange:E,...A}=r,g=l.useMemo(()=>le(t,{formulaKey:c,formulaContextKey:i}),[t,c,i]),I=l.useMemo(()=>({formulaDataKey:f,formulaPathKey:d}),[f,d]),D=l.useMemo(()=>we(s,g),[s,g]),{enrichedFormData:T,handleInput:w}=ke(o,g,n,R,p,b,v,I),j=l.useRef(E);l.useEffect(()=>{j.current=E},[E]),l.useEffect(()=>{var O;g.length!==0&&((O=j.current)==null||O.call(j,{formData:T},void 0))},[T]);const S=(O,C)=>{w(O.formData),E==null||E(O,C)};return ge.jsx(a,{...A,schema:t,formData:T,uiSchema:D,onChange:S})}exports.FormulaForm=Pe;exports.analyzeSchema=le;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const V=require("react/jsx-runtime"),c=require("react"),W=require("@rjsf/core"),$=Symbol("arrayIndex");function B(e,t){const n=(t==null?void 0:t.formulaKey)??"x-formula",s=(t==null?void 0:t.formulaContextKey)??"x-formula-context",r=[];return P(e,[],r,n,s),r}const Z=["$ref","oneOf","anyOf","allOf"];function L(e,t){return e===void 0||e==="siblings"?"siblings":e==="extended"?"extended":(console.warn(`[rjsf-formulas] Unknown x-formula-context value "${e}" at path [${N(t)}], treating as "siblings"`),"siblings")}function N(e){return e.map(t=>typeof t=="symbol"?"[*]":String(t)).join(", ")}function P(e,t,n,s,r){for(const o of Z)o in e&&console.warn(`[rjsf-formulas] Schema composition operator "${o}" at path [${N(t)}] is not supported and will be skipped. See issue 06.`);if(s in e){const o=e[s];if(typeof o!="string"){console.warn(`[rjsf-formulas] Formula at path [${N(t)}] is not a string (got ${typeof o}), skipping.`);return}n.push({path:t,formula:o,contextMode:L(e[r],t)});return}if(e.type==="object"&&e.properties)for(const[o,u]of Object.entries(e.properties))P(u,[...t,o],n,s,r);if(e.type==="array"){const o=e;Array.isArray(o.prefixItems)&&o.prefixItems.forEach((i,f)=>{P(i,[...t,f],n,s,r)}),e.items&&!Array.isArray(e.items)&&P(e.items,[...t,$],n,s,r)}}function ee(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var T,X;function te(){return X||(X=1,T=function e(t,n){if(t===n)return!0;if(t&&n&&typeof t=="object"&&typeof n=="object"){if(t.constructor!==n.constructor)return!1;var s,r,o;if(Array.isArray(t)){if(s=t.length,s!=n.length)return!1;for(r=s;r--!==0;)if(!e(t[r],n[r]))return!1;return!0}if(t.constructor===RegExp)return t.source===n.source&&t.flags===n.flags;if(t.valueOf!==Object.prototype.valueOf)return t.valueOf()===n.valueOf();if(t.toString!==Object.prototype.toString)return t.toString()===n.toString();if(o=Object.keys(t),s=o.length,s!==Object.keys(n).length)return!1;for(r=s;r--!==0;)if(!Object.prototype.hasOwnProperty.call(n,o[r]))return!1;for(r=s;r--!==0;){var u=o[r];if(!e(t[u],n[u]))return!1}return!0}return t!==t&&n!==n}),T}var re=te();const ne=ee(re);function se(e,t,n,s){const o=t.slice(0,-1).reduce((i,f)=>{if(!(i==null||typeof i!="object"))return i[f]},n),u=o!=null&&typeof o=="object"&&!Array.isArray(o)?{...o}:{};return e.contextMode==="extended"?((s.formulaDataKey in u||s.formulaPathKey in u)&&console.warn(`[rjsf-formulas] Extended context key collision: sibling field named "${s.formulaDataKey in u?s.formulaDataKey:s.formulaPathKey}" will be overwritten by the injected context. Use formulaDataKey/formulaPathKey props to choose a different key.`),{...u,[s.formulaDataKey]:n,[s.formulaPathKey]:[...t]}):u}function h(e,t){return t.reduce((n,s)=>{if(!(n==null||typeof n!="object"))return n[s]},e)}function C(e,t,n){if(t.length===0)return;const s=h(e,t.slice(0,-1));s!=null&&typeof s=="object"&&(s[t[t.length-1]]=n)}function O(e,t,n=[]){if(e.length===0)return[n];const[s,...r]=e;if(s===$){const o=h(t,n);if(!Array.isArray(o))return[];const u=[];for(let i=0;i<o.length;i++)u.push(...O(r,t,[...n,i]));return u}return O(r,t,[...n,s])}function z(e,t){return ne(e,t)}async function F(e,t,n,s){const r=structuredClone(e),o=[];for(const f of t)for(const a of O(f.path,r)){const l=se(f,a,r,s);o.push({field:f,concretePath:a,context:l})}const u=await Promise.allSettled(o.map(({field:f,context:a})=>new Promise(l=>l(n(f.formula,a))))),i=[];for(let f=0;f<o.length;f++){const{concretePath:a}=o[f],l=u[f];if(l.status==="fulfilled")C(r,a,l.value);else{const y=l.reason instanceof Error?l.reason:new Error(String(l.reason));i.push({path:a,error:y}),C(r,a,void 0)}}return{data:r,errors:i}}function oe(e,t,n){for(const s of n)for(const r of O(s.path,t))if(!z(h(e,r),h(t,r)))return!1;return!0}async function fe(e,t,n,s,r,o="__formData__",u="__path__"){if(t.length===0)return e;const i={formulaDataKey:o,formulaPathKey:u};let f=e;for(let y=0;y<s;y++){const{data:p,errors:g}=await F(f,t,n,i);if(oe(f,p,t)){for(const{path:d,error:R}of g)r==null||r(d,R);return p}f=p}const{data:a}=await F(f,t,n,i),l=structuredClone(a);for(const y of t)for(const p of O(y.path,a))z(h(f,p),h(a,p))||(r==null||r(p,new Error(`[rjsf-formulas] Formula at [${p.join(", ")}] did not converge after ${s} passes`)),C(l,p,void 0));return l}function ue(e){return JSON.stringify(e)}function ce(e,t,n,s,r,o,u,i){const[f,a]=c.useState(e),l=c.useRef(e),y=c.useRef(e),p=c.useRef(new Map),g=c.useRef(null),d=c.useRef(!1),R=c.useRef(o),m=c.useRef(u),K=c.useRef(n),M=c.useRef(t),x=c.useRef(r),A=c.useRef(i);c.useEffect(()=>{R.current=o},[o]),c.useEffect(()=>{m.current=u},[u]),c.useEffect(()=>{K.current=n},[n]),c.useEffect(()=>{M.current=t},[t]),c.useEffect(()=>{x.current=r},[r]),c.useEffect(()=>{A.current=i},[i]);const b=c.useCallback(()=>{if(d.current)return;(async()=>{var w,k;d.current=!0;try{for(;;){const q=l.current,J=M.current,E=[];for(const I of J)for(const Q of O(I.path,q))E.push(Q);const S=p.current;S.clear();for(const I of E)S.set(ue(I),"running");E.length>0&&((w=m.current)==null||w.call(m,E));const G=await fe(q,J,K.current,x.current,R.current,A.current.formulaDataKey,A.current.formulaPathKey);if(v.current)break;const H=[...S.values()].some(I=>I==="dirty");if(S.clear(),!H){a(G),E.length>0&&((k=m.current)==null||k.call(m,[]));break}}}finally{d.current=!1}})()},[]),_=c.useCallback(Y=>{l.current=Y;const w=p.current;for(const[k,q]of w)q==="running"&&w.set(k,"dirty");d.current||(g.current!==null&&clearTimeout(g.current),g.current=setTimeout(()=>{g.current=null,b()},s))},[s,b]),j=c.useRef(!1),v=c.useRef(!1);return c.useEffect(()=>{j.current?z(e,y.current)||(y.current=e,_(e)):(j.current=!0,y.current=e,_(e))}),c.useEffect(()=>()=>{v.current=!0,g.current!==null&&clearTimeout(g.current)},[]),{enrichedFormData:f,handleInput:_}}function ie(e,t){const n=structuredClone(e??{}),s=ae(t);for(const r of t)D(n,r.path,s);return n}function ae(e){const t=new Set;for(let n=0;n<e.length;n++){const s=e[n].path;for(let r=0;r<s.length;r++){const o=s[r];if(typeof o!="number"&&o!==$)continue;const u=U(s.slice(0,r)),i=typeof o=="number";e.some((a,l)=>{if(l===n||a.path.length<=r||U(a.path.slice(0,r))!==u)return!1;const y=a.path[r];return i?y===$:typeof y=="number"})&&t.add(u)}}return t}function U(e){return JSON.stringify(e.map(t=>typeof t=="symbol"?"__ARRAY_INDEX__":t))}function D(e,t,n,s=[]){if(t.length===0){e["ui:readonly"]=!0;return}const[r,...o]=t;if(typeof r=="string"){(!e[r]||typeof e[r]!="object"||Array.isArray(e[r]))&&(e[r]={}),D(e[r],o,n,[...s,r]);return}if(typeof r=="number"){if(e.items!==void 0&&!Array.isArray(e.items)){console.warn("[rjsf-formulas] mergeReadOnly: cannot inject ui:readonly at tuple index because existing uiSchema has items as an object. Skipping.");return}Array.isArray(e.items)||(e.items=[]);const f=e.items;(!f[r]||typeof f[r]!="object")&&(f[r]={}),D(f[r],o,n,[...s,r]);return}const u=U(s);if(n.has(u))(!e.additionalItems||typeof e.additionalItems!="object")&&(e.additionalItems={}),D(e.additionalItems,o,n,[...s,r]);else{if(Array.isArray(e.items)){console.warn("[rjsf-formulas] mergeReadOnly: cannot inject ui:readonly for uniform array because existing uiSchema has items as an array. Skipping.");return}(!e.items||typeof e.items!="object")&&(e.items={}),D(e.items,o,n,[...s,r])}}function le(e){const{schema:t,formData:n,uiSchema:s,evaluator:r,Form:o=W,formulaKey:u="x-formula",formulaContextKey:i="x-formula-context",formulaDataKey:f="__formData__",formulaPathKey:a="__path__",maxConvergencePasses:l=10,debounceMs:y=300,onFormulaError:p,onLoadingChange:g,onChange:d,...R}=e,m=c.useMemo(()=>B(t,{formulaKey:u,formulaContextKey:i}),[t,u,i]),K=c.useMemo(()=>({formulaDataKey:f,formulaPathKey:a}),[f,a]),M=c.useMemo(()=>ie(s,m),[s,m]),{enrichedFormData:x,handleInput:A}=ce(n,m,r,y,l,p,g,K),b=c.useRef(d);c.useEffect(()=>{b.current=d},[d]),c.useEffect(()=>{var j;m.length!==0&&((j=b.current)==null||j.call(b,{formData:x},void 0))},[x]);const _=(j,v)=>{A(j.formData),d==null||d(j,v)};return V.jsx(o,{...R,schema:t,formData:x,uiSchema:M,onChange:_})}exports.FormulaForm=le;exports.analyzeSchema=B;
package/dist/index.mjs CHANGED
@@ -1,654 +1,376 @@
1
- import ye, { useState as _e, useRef as v, useEffect as j, useCallback as ce, useMemo as re } from "react";
2
- import be from "@rjsf/core";
3
- const Z = Symbol("arrayIndex");
4
- function Re(r, t) {
5
- const o = (t == null ? void 0 : t.formulaKey) ?? "x-formula", s = (t == null ? void 0 : t.formulaContextKey) ?? "x-formula-context", n = [];
6
- return H(r, [], n, o, s), n;
1
+ import { jsx as F } from "react/jsx-runtime";
2
+ import { useState as L, useRef as p, useEffect as j, useCallback as G, useMemo as T } from "react";
3
+ import tt from "@rjsf/core";
4
+ const N = Symbol("arrayIndex");
5
+ function et(t, e) {
6
+ const n = (e == null ? void 0 : e.formulaKey) ?? "x-formula", o = (e == null ? void 0 : e.formulaContextKey) ?? "x-formula-context", r = [];
7
+ return P(t, [], r, n, o), r;
7
8
  }
8
- const Ee = ["$ref", "oneOf", "anyOf", "allOf"];
9
- function ge(r, t) {
10
- return r === void 0 || r === "siblings" ? "siblings" : r === "extended" ? "extended" : (console.warn(
11
- `[rjsf-formulas] Unknown x-formula-context value "${r}" at path [${ne(t)}], treating as "siblings"`
9
+ const rt = ["$ref", "oneOf", "anyOf", "allOf"];
10
+ function nt(t, e) {
11
+ return t === void 0 || t === "siblings" ? "siblings" : t === "extended" ? "extended" : (console.warn(
12
+ `[rjsf-formulas] Unknown x-formula-context value "${t}" at path [${U(e)}], treating as "siblings"`
12
13
  ), "siblings");
13
14
  }
14
- function ne(r) {
15
- return r.map((t) => typeof t == "symbol" ? "[*]" : String(t)).join(", ");
15
+ function U(t) {
16
+ return t.map((e) => typeof e == "symbol" ? "[*]" : String(e)).join(", ");
16
17
  }
17
- function H(r, t, o, s, n) {
18
- for (const a of Ee)
19
- a in r && console.warn(
20
- `[rjsf-formulas] Schema composition operator "${a}" at path [${ne(t)}] is not supported and will be skipped. See issue 06.`
18
+ function P(t, e, n, o, r) {
19
+ for (const s of rt)
20
+ s in t && console.warn(
21
+ `[rjsf-formulas] Schema composition operator "${s}" at path [${U(e)}] is not supported and will be skipped. See issue 06.`
21
22
  );
22
- if (s in r) {
23
- const a = r[s];
24
- if (typeof a != "string") {
23
+ if (o in t) {
24
+ const s = t[o];
25
+ if (typeof s != "string") {
25
26
  console.warn(
26
- `[rjsf-formulas] Formula at path [${ne(t)}] is not a string (got ${typeof a}), skipping.`
27
+ `[rjsf-formulas] Formula at path [${U(e)}] is not a string (got ${typeof s}), skipping.`
27
28
  );
28
29
  return;
29
30
  }
30
- o.push({
31
- path: t,
32
- formula: a,
33
- contextMode: ge(
34
- r[n],
35
- t
31
+ n.push({
32
+ path: e,
33
+ formula: s,
34
+ contextMode: nt(
35
+ t[r],
36
+ e
36
37
  )
37
38
  });
38
39
  return;
39
40
  }
40
- if (r.type === "object" && r.properties)
41
- for (const [a, c] of Object.entries(r.properties))
42
- H(
41
+ if (t.type === "object" && t.properties)
42
+ for (const [s, c] of Object.entries(t.properties))
43
+ P(
43
44
  c,
44
- [...t, a],
45
+ [...e, s],
46
+ n,
45
47
  o,
46
- s,
47
- n
48
+ r
48
49
  );
49
- if (r.type === "array") {
50
- const a = r;
51
- Array.isArray(a.prefixItems) && a.prefixItems.forEach((f, i) => {
52
- H(f, [...t, i], o, s, n);
53
- }), r.items && !Array.isArray(r.items) && H(
54
- r.items,
55
- [...t, Z],
50
+ if (t.type === "array") {
51
+ const s = t;
52
+ Array.isArray(s.prefixItems) && s.prefixItems.forEach((i, f) => {
53
+ P(i, [...e, f], n, o, r);
54
+ }), t.items && !Array.isArray(t.items) && P(
55
+ t.items,
56
+ [...e, N],
57
+ n,
56
58
  o,
57
- s,
58
- n
59
+ r
59
60
  );
60
61
  }
61
62
  }
62
- function ve(r) {
63
- return r && r.__esModule && Object.prototype.hasOwnProperty.call(r, "default") ? r.default : r;
63
+ function ot(t) {
64
+ return t && t.__esModule && Object.prototype.hasOwnProperty.call(t, "default") ? t.default : t;
64
65
  }
65
- var B = { exports: {} }, W = {};
66
- /**
67
- * @license React
68
- * react-jsx-runtime.production.js
69
- *
70
- * Copyright (c) Meta Platforms, Inc. and affiliates.
71
- *
72
- * This source code is licensed under the MIT license found in the
73
- * LICENSE file in the root directory of this source tree.
74
- */
75
- var ie;
76
- function Oe() {
77
- if (ie) return W;
78
- ie = 1;
79
- var r = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
80
- function o(s, n, a) {
81
- var c = null;
82
- if (a !== void 0 && (c = "" + a), n.key !== void 0 && (c = "" + n.key), "key" in n) {
83
- a = {};
84
- for (var f in n)
85
- f !== "key" && (a[f] = n[f]);
86
- } else a = n;
87
- return n = a.ref, {
88
- $$typeof: r,
89
- type: s,
90
- key: c,
91
- ref: n !== void 0 ? n : null,
92
- props: a
93
- };
94
- }
95
- return W.Fragment = t, W.jsx = o, W.jsxs = o, W;
96
- }
97
- var J = {};
98
- /**
99
- * @license React
100
- * react-jsx-runtime.development.js
101
- *
102
- * Copyright (c) Meta Platforms, Inc. and affiliates.
103
- *
104
- * This source code is licensed under the MIT license found in the
105
- * LICENSE file in the root directory of this source tree.
106
- */
107
- var fe;
108
- function je() {
109
- return fe || (fe = 1, process.env.NODE_ENV !== "production" && (function() {
110
- function r(e) {
111
- if (e == null) return null;
112
- if (typeof e == "function")
113
- return e.$$typeof === Y ? null : e.displayName || e.name || null;
114
- if (typeof e == "string") return e;
115
- switch (e) {
116
- case E:
117
- return "Fragment";
118
- case $:
119
- return "Profiler";
120
- case D:
121
- return "StrictMode";
122
- case P:
123
- return "Suspense";
124
- case O:
125
- return "SuspenseList";
126
- case N:
127
- return "Activity";
128
- }
129
- if (typeof e == "object")
130
- switch (typeof e.tag == "number" && console.error(
131
- "Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."
132
- ), e.$$typeof) {
133
- case x:
134
- return "Portal";
135
- case S:
136
- return e.displayName || "Context";
137
- case T:
138
- return (e._context.displayName || "Context") + ".Consumer";
139
- case A:
140
- var u = e.render;
141
- return e = e.displayName, e || (e = u.displayName || u.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
142
- case C:
143
- return u = e.displayName || null, u !== null ? u : r(e.type) || "Memo";
144
- case M:
145
- u = e._payload, e = e._init;
146
- try {
147
- return r(e(u));
148
- } catch {
149
- }
150
- }
151
- return null;
152
- }
153
- function t(e) {
154
- return "" + e;
155
- }
156
- function o(e) {
157
- try {
158
- t(e);
159
- var u = !1;
160
- } catch {
161
- u = !0;
162
- }
163
- if (u) {
164
- u = console;
165
- var p = u.error, m = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object";
166
- return p.call(
167
- u,
168
- "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",
169
- m
170
- ), t(e);
171
- }
172
- }
173
- function s(e) {
174
- if (e === E) return "<>";
175
- if (typeof e == "object" && e !== null && e.$$typeof === M)
176
- return "<...>";
177
- try {
178
- var u = r(e);
179
- return u ? "<" + u + ">" : "<...>";
180
- } catch {
181
- return "<...>";
182
- }
183
- }
184
- function n() {
185
- var e = h.A;
186
- return e === null ? null : e.getOwner();
187
- }
188
- function a() {
189
- return Error("react-stack-top-frame");
190
- }
191
- function c(e) {
192
- if (F.call(e, "key")) {
193
- var u = Object.getOwnPropertyDescriptor(e, "key").get;
194
- if (u && u.isReactWarning) return !1;
195
- }
196
- return e.key !== void 0;
197
- }
198
- function f(e, u) {
199
- function p() {
200
- V || (V = !0, console.error(
201
- "%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)",
202
- u
203
- ));
204
- }
205
- p.isReactWarning = !0, Object.defineProperty(e, "key", {
206
- get: p,
207
- configurable: !0
208
- });
209
- }
210
- function i() {
211
- var e = r(this.type);
212
- return L[e] || (L[e] = !0, console.error(
213
- "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."
214
- )), e = this.props.ref, e !== void 0 ? e : null;
215
- }
216
- function l(e, u, p, m, G, Q) {
217
- var y = p.ref;
218
- return e = {
219
- $$typeof: R,
220
- type: e,
221
- key: u,
222
- props: p,
223
- _owner: m
224
- }, (y !== void 0 ? y : null) !== null ? Object.defineProperty(e, "ref", {
225
- enumerable: !1,
226
- get: i
227
- }) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", {
228
- configurable: !1,
229
- enumerable: !1,
230
- writable: !0,
231
- value: 0
232
- }), Object.defineProperty(e, "_debugInfo", {
233
- configurable: !1,
234
- enumerable: !1,
235
- writable: !0,
236
- value: null
237
- }), Object.defineProperty(e, "_debugStack", {
238
- configurable: !1,
239
- enumerable: !1,
240
- writable: !0,
241
- value: G
242
- }), Object.defineProperty(e, "_debugTask", {
243
- configurable: !1,
244
- enumerable: !1,
245
- writable: !0,
246
- value: Q
247
- }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
248
- }
249
- function d(e, u, p, m, G, Q) {
250
- var y = u.children;
251
- if (y !== void 0)
252
- if (m)
253
- if (I(y)) {
254
- for (m = 0; m < y.length; m++)
255
- _(y[m]);
256
- Object.freeze && Object.freeze(y);
257
- } else
258
- console.error(
259
- "React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."
260
- );
261
- else _(y);
262
- if (F.call(u, "key")) {
263
- y = r(e);
264
- var q = Object.keys(u).filter(function(me) {
265
- return me !== "key";
266
- });
267
- m = 0 < q.length ? "{key: someKey, " + q.join(": ..., ") + ": ...}" : "{key: someKey}", ue[y + m] || (q = 0 < q.length ? "{" + q.join(": ..., ") + ": ...}" : "{}", console.error(
268
- `A props object containing a "key" prop is being spread into JSX:
269
- let props = %s;
270
- <%s {...props} />
271
- React keys must be passed directly to JSX without using spread:
272
- let props = %s;
273
- <%s key={someKey} {...props} />`,
274
- m,
275
- y,
276
- q,
277
- y
278
- ), ue[y + m] = !0);
279
- }
280
- if (y = null, p !== void 0 && (o(p), y = "" + p), c(u) && (o(u.key), y = "" + u.key), "key" in u) {
281
- p = {};
282
- for (var ee in u)
283
- ee !== "key" && (p[ee] = u[ee]);
284
- } else p = u;
285
- return y && f(
286
- p,
287
- typeof e == "function" ? e.displayName || e.name || "Unknown" : e
288
- ), l(
289
- e,
290
- y,
291
- p,
292
- n(),
293
- G,
294
- Q
295
- );
296
- }
297
- function _(e) {
298
- b(e) ? e._store && (e._store.validated = 1) : typeof e == "object" && e !== null && e.$$typeof === M && (e._payload.status === "fulfilled" ? b(e._payload.value) && e._payload.value._store && (e._payload.value._store.validated = 1) : e._store && (e._store.validated = 1));
299
- }
300
- function b(e) {
301
- return typeof e == "object" && e !== null && e.$$typeof === R;
302
- }
303
- var g = ye, R = Symbol.for("react.transitional.element"), x = Symbol.for("react.portal"), E = Symbol.for("react.fragment"), D = Symbol.for("react.strict_mode"), $ = Symbol.for("react.profiler"), T = Symbol.for("react.consumer"), S = Symbol.for("react.context"), A = Symbol.for("react.forward_ref"), P = Symbol.for("react.suspense"), O = Symbol.for("react.suspense_list"), C = Symbol.for("react.memo"), M = Symbol.for("react.lazy"), N = Symbol.for("react.activity"), Y = Symbol.for("react.client.reference"), h = g.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, F = Object.prototype.hasOwnProperty, I = Array.isArray, k = console.createTask ? console.createTask : function() {
304
- return null;
305
- };
306
- g = {
307
- react_stack_bottom_frame: function(e) {
308
- return e();
309
- }
310
- };
311
- var V, L = {}, w = g.react_stack_bottom_frame.bind(
312
- g,
313
- a
314
- )(), X = k(s(a)), ue = {};
315
- J.Fragment = E, J.jsx = function(e, u, p) {
316
- var m = 1e4 > h.recentlyCreatedOwnerStacks++;
317
- return d(
318
- e,
319
- u,
320
- p,
321
- !1,
322
- m ? Error("react-stack-top-frame") : w,
323
- m ? k(s(e)) : X
324
- );
325
- }, J.jsxs = function(e, u, p) {
326
- var m = 1e4 > h.recentlyCreatedOwnerStacks++;
327
- return d(
328
- e,
329
- u,
330
- p,
331
- !0,
332
- m ? Error("react-stack-top-frame") : w,
333
- m ? k(s(e)) : X
334
- );
335
- };
336
- })()), J;
337
- }
338
- var le;
339
- function Ae() {
340
- return le || (le = 1, process.env.NODE_ENV === "production" ? B.exports = Oe() : B.exports = je()), B.exports;
341
- }
342
- var xe = Ae(), te, de;
343
- function Te() {
344
- return de || (de = 1, te = function r(t, o) {
345
- if (t === o) return !0;
346
- if (t && o && typeof t == "object" && typeof o == "object") {
347
- if (t.constructor !== o.constructor) return !1;
348
- var s, n, a;
349
- if (Array.isArray(t)) {
350
- if (s = t.length, s != o.length) return !1;
351
- for (n = s; n-- !== 0; )
352
- if (!r(t[n], o[n])) return !1;
66
+ var C, H;
67
+ function st() {
68
+ return H || (H = 1, C = function t(e, n) {
69
+ if (e === n) return !0;
70
+ if (e && n && typeof e == "object" && typeof n == "object") {
71
+ if (e.constructor !== n.constructor) return !1;
72
+ var o, r, s;
73
+ if (Array.isArray(e)) {
74
+ if (o = e.length, o != n.length) return !1;
75
+ for (r = o; r-- !== 0; )
76
+ if (!t(e[r], n[r])) return !1;
353
77
  return !0;
354
78
  }
355
- if (t.constructor === RegExp) return t.source === o.source && t.flags === o.flags;
356
- if (t.valueOf !== Object.prototype.valueOf) return t.valueOf() === o.valueOf();
357
- if (t.toString !== Object.prototype.toString) return t.toString() === o.toString();
358
- if (a = Object.keys(t), s = a.length, s !== Object.keys(o).length) return !1;
359
- for (n = s; n-- !== 0; )
360
- if (!Object.prototype.hasOwnProperty.call(o, a[n])) return !1;
361
- for (n = s; n-- !== 0; ) {
362
- var c = a[n];
363
- if (!r(t[c], o[c])) return !1;
79
+ if (e.constructor === RegExp) return e.source === n.source && e.flags === n.flags;
80
+ if (e.valueOf !== Object.prototype.valueOf) return e.valueOf() === n.valueOf();
81
+ if (e.toString !== Object.prototype.toString) return e.toString() === n.toString();
82
+ if (s = Object.keys(e), o = s.length, o !== Object.keys(n).length) return !1;
83
+ for (r = o; r-- !== 0; )
84
+ if (!Object.prototype.hasOwnProperty.call(n, s[r])) return !1;
85
+ for (r = o; r-- !== 0; ) {
86
+ var c = s[r];
87
+ if (!t(e[c], n[c])) return !1;
364
88
  }
365
89
  return !0;
366
90
  }
367
- return t !== t && o !== o;
368
- }), te;
91
+ return e !== e && n !== n;
92
+ }), C;
369
93
  }
370
- var he = Te();
371
- const ke = /* @__PURE__ */ ve(he);
372
- function we(r, t, o, s) {
373
- const a = t.slice(0, -1).reduce((f, i) => {
374
- if (!(f == null || typeof f != "object"))
375
- return f[i];
376
- }, o), c = a != null && typeof a == "object" && !Array.isArray(a) ? { ...a } : {};
377
- return r.contextMode === "extended" ? ((s.formulaDataKey in c || s.formulaPathKey in c) && console.warn(
378
- `[rjsf-formulas] Extended context key collision: sibling field named "${s.formulaDataKey in c ? s.formulaDataKey : s.formulaPathKey}" will be overwritten by the injected context. Use formulaDataKey/formulaPathKey props to choose a different key.`
94
+ var ft = st();
95
+ const ct = /* @__PURE__ */ ot(ft);
96
+ function it(t, e, n, o) {
97
+ const s = e.slice(0, -1).reduce((i, f) => {
98
+ if (!(i == null || typeof i != "object"))
99
+ return i[f];
100
+ }, n), c = s != null && typeof s == "object" && !Array.isArray(s) ? { ...s } : {};
101
+ return t.contextMode === "extended" ? ((o.formulaDataKey in c || o.formulaPathKey in c) && console.warn(
102
+ `[rjsf-formulas] Extended context key collision: sibling field named "${o.formulaDataKey in c ? o.formulaDataKey : o.formulaPathKey}" will be overwritten by the injected context. Use formulaDataKey/formulaPathKey props to choose a different key.`
379
103
  ), {
380
104
  ...c,
381
- [s.formulaDataKey]: o,
382
- [s.formulaPathKey]: [...t]
105
+ [o.formulaDataKey]: n,
106
+ [o.formulaPathKey]: [...e]
383
107
  }) : c;
384
108
  }
385
- function K(r, t) {
386
- return t.reduce((o, s) => {
387
- if (!(o == null || typeof o != "object"))
388
- return o[s];
389
- }, r);
109
+ function A(t, e) {
110
+ return e.reduce((n, o) => {
111
+ if (!(n == null || typeof n != "object"))
112
+ return n[o];
113
+ }, t);
390
114
  }
391
- function oe(r, t, o) {
392
- if (t.length === 0) return;
393
- const s = K(r, t.slice(0, -1));
394
- s != null && typeof s == "object" && (s[t[t.length - 1]] = o);
115
+ function Y(t, e, n) {
116
+ if (e.length === 0) return;
117
+ const o = A(t, e.slice(0, -1));
118
+ o != null && typeof o == "object" && (o[e[e.length - 1]] = n);
395
119
  }
396
- function U(r, t, o = []) {
397
- if (r.length === 0) return [o];
398
- const [s, ...n] = r;
399
- if (s === Z) {
400
- const a = K(t, o);
401
- if (!Array.isArray(a)) return [];
120
+ function _(t, e, n = []) {
121
+ if (t.length === 0) return [n];
122
+ const [o, ...r] = t;
123
+ if (o === N) {
124
+ const s = A(e, n);
125
+ if (!Array.isArray(s)) return [];
402
126
  const c = [];
403
- for (let f = 0; f < a.length; f++)
404
- c.push(...U(n, t, [...o, f]));
127
+ for (let i = 0; i < s.length; i++)
128
+ c.push(..._(r, e, [...n, i]));
405
129
  return c;
406
130
  }
407
- return U(n, t, [...o, s]);
131
+ return _(r, e, [...n, o]);
408
132
  }
409
- function ae(r, t) {
410
- return ke(r, t);
133
+ function J(t, e) {
134
+ return ct(t, e);
411
135
  }
412
- async function pe(r, t, o, s) {
413
- const n = structuredClone(r), a = [];
414
- for (const i of t)
415
- for (const l of U(i.path, n)) {
416
- const d = we(i, l, n, s);
417
- a.push({ field: i, concretePath: l, context: d });
136
+ async function Q(t, e, n, o) {
137
+ const r = structuredClone(t), s = [];
138
+ for (const f of e)
139
+ for (const u of _(f.path, r)) {
140
+ const a = it(f, u, r, o);
141
+ s.push({ field: f, concretePath: u, context: a });
418
142
  }
419
143
  const c = await Promise.allSettled(
420
- a.map(({ field: i, context: l }) => new Promise((d) => d(o(i.formula, l))))
421
- ), f = [];
422
- for (let i = 0; i < a.length; i++) {
423
- const { concretePath: l } = a[i], d = c[i];
424
- if (d.status === "fulfilled")
425
- oe(n, l, d.value);
144
+ s.map(({ field: f, context: u }) => new Promise((a) => a(n(f.formula, u))))
145
+ ), i = [];
146
+ for (let f = 0; f < s.length; f++) {
147
+ const { concretePath: u } = s[f], a = c[f];
148
+ if (a.status === "fulfilled")
149
+ Y(r, u, a.value);
426
150
  else {
427
- const _ = d.reason instanceof Error ? d.reason : new Error(String(d.reason));
428
- f.push({ path: l, error: _ }), oe(n, l, void 0);
151
+ const l = a.reason instanceof Error ? a.reason : new Error(String(a.reason));
152
+ i.push({ path: u, error: l }), Y(r, u, void 0);
429
153
  }
430
154
  }
431
- return { data: n, errors: f };
155
+ return { data: r, errors: i };
432
156
  }
433
- function Se(r, t, o) {
434
- for (const s of o)
435
- for (const n of U(s.path, t))
436
- if (!ae(K(r, n), K(t, n)))
157
+ function ut(t, e, n) {
158
+ for (const o of n)
159
+ for (const r of _(o.path, e))
160
+ if (!J(A(t, r), A(e, r)))
437
161
  return !1;
438
162
  return !0;
439
163
  }
440
- async function Pe(r, t, o, s, n, a = "__formData__", c = "__path__") {
441
- if (t.length === 0) return r;
442
- const f = { formulaDataKey: a, formulaPathKey: c };
443
- let i = r;
444
- for (let _ = 0; _ < s; _++) {
445
- const { data: b, errors: g } = await pe(i, t, o, f);
446
- if (Se(i, b, t)) {
447
- for (const { path: R, error: x } of g)
448
- n == null || n(R, x);
449
- return b;
164
+ async function at(t, e, n, o, r, s = "__formData__", c = "__path__") {
165
+ if (e.length === 0) return t;
166
+ const i = { formulaDataKey: s, formulaPathKey: c };
167
+ let f = t;
168
+ for (let l = 0; l < o; l++) {
169
+ const { data: y, errors: g } = await Q(f, e, n, i);
170
+ if (ut(f, y, e)) {
171
+ for (const { path: d, error: x } of g)
172
+ r == null || r(d, x);
173
+ return y;
450
174
  }
451
- i = b;
175
+ f = y;
452
176
  }
453
- const { data: l } = await pe(i, t, o, f), d = structuredClone(l);
454
- for (const _ of t)
455
- for (const b of U(_.path, l))
456
- ae(K(i, b), K(l, b)) || (n == null || n(
457
- b,
177
+ const { data: u } = await Q(f, e, n, i), a = structuredClone(u);
178
+ for (const l of e)
179
+ for (const y of _(l.path, u))
180
+ J(A(f, y), A(u, y)) || (r == null || r(
181
+ y,
458
182
  new Error(
459
- `[rjsf-formulas] Formula at [${b.join(", ")}] did not converge after ${s} passes`
183
+ `[rjsf-formulas] Formula at [${y.join(", ")}] did not converge after ${o} passes`
460
184
  )
461
- ), oe(d, b, void 0));
462
- return d;
185
+ ), Y(a, y, void 0));
186
+ return a;
463
187
  }
464
- function Ne(r) {
465
- return JSON.stringify(r);
188
+ function lt(t) {
189
+ return JSON.stringify(t);
466
190
  }
467
- function Ie(r, t, o, s, n, a, c, f) {
468
- const [i, l] = _e(r), d = v(r), _ = v(r), b = v(/* @__PURE__ */ new Map()), g = v(null), R = v(!1), x = v(a), E = v(c), D = v(o), $ = v(t), T = v(n), S = v(f);
191
+ function yt(t, e, n, o, r, s, c, i) {
192
+ const [f, u] = L(t), a = p(t), l = p(t), y = p(/* @__PURE__ */ new Map()), g = p(null), d = p(!1), x = p(s), m = p(c), v = p(n), M = p(e), O = p(r), w = p(i);
469
193
  j(() => {
470
- x.current = a;
471
- }, [a]), j(() => {
472
- E.current = c;
194
+ x.current = s;
195
+ }, [s]), j(() => {
196
+ m.current = c;
473
197
  }, [c]), j(() => {
474
- D.current = o;
475
- }, [o]), j(() => {
476
- $.current = t;
477
- }, [t]), j(() => {
478
- T.current = n;
198
+ v.current = n;
479
199
  }, [n]), j(() => {
480
- S.current = f;
481
- }, [f]);
482
- const A = ce(() => {
483
- if (R.current) return;
200
+ M.current = e;
201
+ }, [e]), j(() => {
202
+ O.current = r;
203
+ }, [r]), j(() => {
204
+ w.current = i;
205
+ }, [i]);
206
+ const h = G(() => {
207
+ if (d.current) return;
484
208
  (async () => {
485
- var N, Y;
486
- R.current = !0;
209
+ var R, E;
210
+ d.current = !0;
487
211
  try {
488
212
  for (; ; ) {
489
- const h = d.current, F = $.current, I = [];
490
- for (const w of F)
491
- for (const X of U(w.path, h))
492
- I.push(X);
493
- const k = b.current;
494
- k.clear();
495
- for (const w of I) k.set(Ne(w), "running");
496
- I.length > 0 && ((N = E.current) == null || N.call(E, I));
497
- const V = await Pe(
498
- h,
499
- F,
500
- D.current,
501
- T.current,
213
+ const S = a.current, B = M.current, I = [];
214
+ for (const D of B)
215
+ for (const Z of _(D.path, S))
216
+ I.push(Z);
217
+ const $ = y.current;
218
+ $.clear();
219
+ for (const D of I) $.set(lt(D), "running");
220
+ I.length > 0 && ((R = m.current) == null || R.call(m, I));
221
+ const V = await at(
222
+ S,
223
+ B,
224
+ v.current,
225
+ O.current,
502
226
  x.current,
503
- S.current.formulaDataKey,
504
- S.current.formulaPathKey
227
+ w.current.formulaDataKey,
228
+ w.current.formulaPathKey
505
229
  );
506
- if (C.current) break;
507
- const L = [...k.values()].some((w) => w === "dirty");
508
- if (k.clear(), !L) {
509
- l(V), I.length > 0 && ((Y = E.current) == null || Y.call(E, []));
230
+ if (q.current) break;
231
+ const W = [...$.values()].some((D) => D === "dirty");
232
+ if ($.clear(), !W) {
233
+ u(V), I.length > 0 && ((E = m.current) == null || E.call(m, []));
510
234
  break;
511
235
  }
512
236
  }
513
237
  } finally {
514
- R.current = !1;
238
+ d.current = !1;
515
239
  }
516
240
  })();
517
- }, []), P = ce(
518
- (M) => {
519
- d.current = M;
520
- const N = b.current;
521
- for (const [Y, h] of N)
522
- h === "running" && N.set(Y, "dirty");
523
- R.current || (g.current !== null && clearTimeout(g.current), g.current = setTimeout(() => {
524
- g.current = null, A();
525
- }, s));
241
+ }, []), k = G(
242
+ (X) => {
243
+ a.current = X;
244
+ const R = y.current;
245
+ for (const [E, S] of R)
246
+ S === "running" && R.set(E, "dirty");
247
+ d.current || (g.current !== null && clearTimeout(g.current), g.current = setTimeout(() => {
248
+ g.current = null, h();
249
+ }, o));
526
250
  },
527
- [s, A]
528
- ), O = v(!1), C = v(!1);
251
+ [o, h]
252
+ ), b = p(!1), q = p(!1);
529
253
  return j(() => {
530
- O.current ? ae(r, _.current) || (_.current = r, P(r)) : (O.current = !0, _.current = r, P(r));
254
+ b.current ? J(t, l.current) || (l.current = t, k(t)) : (b.current = !0, l.current = t, k(t));
531
255
  }), j(
532
256
  () => () => {
533
- C.current = !0, g.current !== null && clearTimeout(g.current);
257
+ q.current = !0, g.current !== null && clearTimeout(g.current);
534
258
  },
535
259
  []
536
- ), { enrichedFormData: i, handleInput: P };
260
+ ), { enrichedFormData: f, handleInput: k };
537
261
  }
538
- function De(r, t) {
539
- const o = structuredClone(r ?? {}), s = $e(t);
540
- for (const n of t)
541
- z(o, n.path, s);
542
- return o;
262
+ function pt(t, e) {
263
+ const n = structuredClone(t ?? {}), o = dt(e);
264
+ for (const r of e)
265
+ K(n, r.path, o);
266
+ return n;
543
267
  }
544
- function $e(r) {
545
- const t = /* @__PURE__ */ new Set();
546
- for (let o = 0; o < r.length; o++) {
547
- const s = r[o].path;
548
- for (let n = 0; n < s.length; n++) {
549
- const a = s[n];
550
- if (typeof a != "number" && a !== Z) continue;
551
- const c = se(s.slice(0, n)), f = typeof a == "number";
552
- r.some((l, d) => {
553
- if (d === o || l.path.length <= n || se(l.path.slice(0, n)) !== c) return !1;
554
- const _ = l.path[n];
555
- return f ? _ === Z : typeof _ == "number";
556
- }) && t.add(c);
268
+ function dt(t) {
269
+ const e = /* @__PURE__ */ new Set();
270
+ for (let n = 0; n < t.length; n++) {
271
+ const o = t[n].path;
272
+ for (let r = 0; r < o.length; r++) {
273
+ const s = o[r];
274
+ if (typeof s != "number" && s !== N) continue;
275
+ const c = z(o.slice(0, r)), i = typeof s == "number";
276
+ t.some((u, a) => {
277
+ if (a === n || u.path.length <= r || z(u.path.slice(0, r)) !== c) return !1;
278
+ const l = u.path[r];
279
+ return i ? l === N : typeof l == "number";
280
+ }) && e.add(c);
557
281
  }
558
282
  }
559
- return t;
283
+ return e;
560
284
  }
561
- function se(r) {
562
- return JSON.stringify(r.map((t) => typeof t == "symbol" ? "__ARRAY_INDEX__" : t));
285
+ function z(t) {
286
+ return JSON.stringify(t.map((e) => typeof e == "symbol" ? "__ARRAY_INDEX__" : e));
563
287
  }
564
- function z(r, t, o, s = []) {
565
- if (t.length === 0) {
566
- r["ui:readonly"] = !0;
288
+ function K(t, e, n, o = []) {
289
+ if (e.length === 0) {
290
+ t["ui:readonly"] = !0;
567
291
  return;
568
292
  }
569
- const [n, ...a] = t;
570
- if (typeof n == "string") {
571
- (!r[n] || typeof r[n] != "object" || Array.isArray(r[n])) && (r[n] = {}), z(r[n], a, o, [...s, n]);
293
+ const [r, ...s] = e;
294
+ if (typeof r == "string") {
295
+ (!t[r] || typeof t[r] != "object" || Array.isArray(t[r])) && (t[r] = {}), K(t[r], s, n, [...o, r]);
572
296
  return;
573
297
  }
574
- if (typeof n == "number") {
575
- if (r.items !== void 0 && !Array.isArray(r.items)) {
298
+ if (typeof r == "number") {
299
+ if (t.items !== void 0 && !Array.isArray(t.items)) {
576
300
  console.warn("[rjsf-formulas] mergeReadOnly: cannot inject ui:readonly at tuple index because existing uiSchema has items as an object. Skipping.");
577
301
  return;
578
302
  }
579
- Array.isArray(r.items) || (r.items = []);
580
- const i = r.items;
581
- (!i[n] || typeof i[n] != "object") && (i[n] = {}), z(i[n], a, o, [...s, n]);
303
+ Array.isArray(t.items) || (t.items = []);
304
+ const f = t.items;
305
+ (!f[r] || typeof f[r] != "object") && (f[r] = {}), K(f[r], s, n, [...o, r]);
582
306
  return;
583
307
  }
584
- const c = se(s);
585
- if (o.has(c))
586
- (!r.additionalItems || typeof r.additionalItems != "object") && (r.additionalItems = {}), z(r.additionalItems, a, o, [...s, n]);
308
+ const c = z(o);
309
+ if (n.has(c))
310
+ (!t.additionalItems || typeof t.additionalItems != "object") && (t.additionalItems = {}), K(t.additionalItems, s, n, [...o, r]);
587
311
  else {
588
- if (Array.isArray(r.items)) {
312
+ if (Array.isArray(t.items)) {
589
313
  console.warn("[rjsf-formulas] mergeReadOnly: cannot inject ui:readonly for uniform array because existing uiSchema has items as an array. Skipping.");
590
314
  return;
591
315
  }
592
- (!r.items || typeof r.items != "object") && (r.items = {}), z(r.items, a, o, [...s, n]);
316
+ (!t.items || typeof t.items != "object") && (t.items = {}), K(t.items, s, n, [...o, r]);
593
317
  }
594
318
  }
595
- function Ye(r) {
319
+ function bt(t) {
596
320
  const {
597
- schema: t,
598
- formData: o,
599
- uiSchema: s,
600
- evaluator: n,
601
- Form: a = be,
321
+ schema: e,
322
+ formData: n,
323
+ uiSchema: o,
324
+ evaluator: r,
325
+ Form: s = tt,
602
326
  formulaKey: c = "x-formula",
603
- formulaContextKey: f = "x-formula-context",
604
- formulaDataKey: i = "__formData__",
605
- formulaPathKey: l = "__path__",
606
- maxConvergencePasses: d = 10,
607
- debounceMs: _ = 300,
608
- onFormulaError: b,
327
+ formulaContextKey: i = "x-formula-context",
328
+ formulaDataKey: f = "__formData__",
329
+ formulaPathKey: u = "__path__",
330
+ maxConvergencePasses: a = 10,
331
+ debounceMs: l = 300,
332
+ onFormulaError: y,
609
333
  onLoadingChange: g,
610
- onChange: R,
334
+ onChange: d,
611
335
  ...x
612
- } = r, E = re(
613
- () => Re(t, { formulaKey: c, formulaContextKey: f }),
614
- [t, c, f]
615
- ), D = re(
616
- () => ({ formulaDataKey: i, formulaPathKey: l }),
617
- [i, l]
618
- ), $ = re(
619
- () => De(s, E),
620
- [s, E]
621
- ), { enrichedFormData: T, handleInput: S } = Ie(
622
- o,
623
- E,
336
+ } = t, m = T(
337
+ () => et(e, { formulaKey: c, formulaContextKey: i }),
338
+ [e, c, i]
339
+ ), v = T(
340
+ () => ({ formulaDataKey: f, formulaPathKey: u }),
341
+ [f, u]
342
+ ), M = T(
343
+ () => pt(o, m),
344
+ [o, m]
345
+ ), { enrichedFormData: O, handleInput: w } = yt(
624
346
  n,
625
- _,
626
- d,
627
- b,
628
- g,
629
- D
630
- ), A = v(R);
631
- j(() => {
632
- A.current = R;
633
- }, [R]), j(() => {
634
- var O;
635
- E.length !== 0 && ((O = A.current) == null || O.call(A, { formData: T }, void 0));
636
- }, [T]);
637
- const P = (O, C) => {
638
- S(O.formData), R == null || R(O, C);
639
- };
640
- return /* @__PURE__ */ xe.jsx(
347
+ m,
348
+ r,
349
+ l,
641
350
  a,
351
+ y,
352
+ g,
353
+ v
354
+ ), h = p(d);
355
+ return j(() => {
356
+ h.current = d;
357
+ }, [d]), j(() => {
358
+ var b;
359
+ m.length !== 0 && ((b = h.current) == null || b.call(h, { formData: O }, void 0));
360
+ }, [O]), /* @__PURE__ */ F(
361
+ s,
642
362
  {
643
363
  ...x,
644
- schema: t,
645
- formData: T,
646
- uiSchema: $,
647
- onChange: P
364
+ schema: e,
365
+ formData: O,
366
+ uiSchema: M,
367
+ onChange: (b, q) => {
368
+ w(b.formData), d == null || d(b, q);
369
+ }
648
370
  }
649
371
  );
650
372
  }
651
373
  export {
652
- Ye as FormulaForm,
653
- Re as analyzeSchema
374
+ bt as FormulaForm,
375
+ et as analyzeSchema
654
376
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a-ludi/rjsf-formulas",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "RJSF extension for computed fields driven by formulas",
5
5
  "author": "Arne Ludwig <arne.ludwig@posteo.de>",
6
6
  "license": "MIT",
@@ -41,7 +41,8 @@
41
41
  "peerDependencies": {
42
42
  "@rjsf/core": ">=5",
43
43
  "@rjsf/utils": ">=5",
44
- "react": ">=17"
44
+ "react": ">=17",
45
+ "react-dom": ">=17"
45
46
  },
46
47
  "devDependencies": {
47
48
  "@rjsf/core": "^6",
@@ -49,12 +50,14 @@
49
50
  "@rjsf/validator-ajv8": "^6",
50
51
  "@testing-library/jest-dom": "^6.9.1",
51
52
  "@testing-library/react": "^16.3.2",
53
+ "@types/node": "^22",
52
54
  "@types/react": "^18",
53
55
  "fast-deep-equal": "^3.1.3",
54
56
  "husky": "^9",
55
57
  "jsdom": "^29.1.1",
56
58
  "publint": "^0.3",
57
59
  "react": "^19",
60
+ "react-dom": "^19",
58
61
  "rjsf-core-v5": "npm:@rjsf/core@^5",
59
62
  "rjsf-utils-v5": "npm:@rjsf/utils@^5",
60
63
  "rjsf-validator-v5": "npm:@rjsf/validator-ajv8@^5",
@@ -65,8 +68,5 @@
65
68
  "vite-plugin-dts": "^4",
66
69
  "vitepress": "^1.6.4",
67
70
  "vitest": "^3"
68
- },
69
- "dependencies": {
70
- "react-dom": "^19"
71
71
  }
72
72
  }