@a-ludi/rjsf-formulas 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const se=require("@rjsf/utils"),fe=require("react/jsx-runtime"),i=require("react"),oe=require("@rjsf/core"),N=Symbol("arrayIndex");function C(e,t){const n=(t==null?void 0:t.formulaKey)??"x-formula",s=(t==null?void 0:t.formulaContextKey)??"x-formula-context",r=(t==null?void 0:t.formulaConflictBehavior)??"warn",f=[];return w(e,[],f,n,s,r,!0,e),f}function J(e,t){if(e===!0)return t;const n=e;return Array.isArray(n.allOf)?{allOf:[...n.allOf,t]}:{allOf:[e,t]}}function ce(e,t){return e===void 0||e==="siblings"?"siblings":e==="extended"?"extended":(console.warn(`[rjsf-formulas] Unknown x-formula-context value "${e}" at path [${U(t)}], treating as "siblings"`),"siblings")}function U(e){return e.map(t=>typeof t=="symbol"?"[*]":String(t)).join(", ")}function Y(e){return e.map(t=>typeof t=="symbol"?"[*]":String(t)).join(".")}function ue(e,t,n){const s=Y(e.path);if(n==="error")throw new TypeError(`[rjsf-formulas] Formula conflict at path [${U(e.path)}]: "${e.formula}" vs "${t.formula}". Resolve the conflict or set formulaConflictBehavior to 'ignore' or 'warn'.`);n==="warn"&&console.warn(`[rjsf-formulas] Formula conflict at path [${s}]: "${e.formula}" (earlier allOf branch) is overridden by "${t.formula}" (later allOf branch).`)}function ie(e,t,n){for(const s of t){const r=Y(s.path),f=e.findIndex(u=>Y(u.path)===r);f!==-1?(ue(e[f],s,n),e[f]=s):e.push(s)}}function w(e,t,n,s,r,f,u,a){const o=e;if(typeof o.$ref=="string"){const c=se.findSchemaDefinition(o.$ref,a);w(c,t,n,s,r,f,u,a);return}if(s in e){const c=o[s];if(typeof c!="string"){console.warn(`[rjsf-formulas] Formula at path [${U(t)}] is not a string (got ${typeof c}), skipping.`);return}n.push({path:t,formula:c,contextMode:ce(o[r],t),condition:u});return}if(e.properties&&e.type!=="array")for(const[c,l]of Object.entries(e.properties))w(l,[...t,c],n,s,r,f,u,a);if(e.type==="array"&&(Array.isArray(o.prefixItems)&&o.prefixItems.forEach((l,y)=>{w(l,[...t,y],n,s,r,f,u,a)}),e.items&&!Array.isArray(e.items)&&w(e.items,[...t,N],n,s,r,f,u,a)),Array.isArray(o.allOf)){const c=o.allOf;for(const l of c){const y=[];w(l,t,y,s,r,f,u,a),ie(n,y,f)}}for(const c of["oneOf","anyOf"])if(Array.isArray(o[c])){const l=o[c];for(const y of l){const m=J(u,y);w(y,t,n,s,r,f,m,a)}}if("if"in o){const c=o.if,l=o.then,y=o.else;if(s in c&&console.warn(`[rjsf-formulas] Formula key "${s}" found inside an "if" schema at path [${U(t)}]. The "if" schema is a validation predicate and is never rendered — the formula will be ignored.`),l!==void 0){const m=J(u,c);w(l,t,n,s,r,f,m,a)}if(y!==void 0){const m=J(u,{not:c});w(y,t,n,s,r,f,m,a)}}}function ae(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var X,Z;function le(){return Z||(Z=1,X=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,f;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(f=Object.keys(t),s=f.length,s!==Object.keys(n).length)return!1;for(r=s;r--!==0;)if(!Object.prototype.hasOwnProperty.call(n,f[r]))return!1;for(r=s;r--!==0;){var u=f[r];if(!e(t[u],n[u]))return!1}return!0}return t!==t&&n!==n}),X}var ye=le();const de=ae(ye);function pe(e,t,n,s){const f=t.slice(0,-1).reduce((a,o)=>{if(!(a==null||typeof a!="object"))return a[o]},n),u=f!=null&&typeof f=="object"&&!Array.isArray(f)?{...f}:{};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 B(e){return e.map(t=>typeof t=="symbol"?"[*]":String(t)).join(", ")}function I(e,t){return t.reduce((n,s)=>{if(!(n==null||typeof n!="object"))return n[s]},e)}function V(e,t,n){if(t.length===0)return;const s=I(e,t.slice(0,-1));s!=null&&typeof s=="object"&&(s[t[t.length-1]]=n)}function k(e,t,n=[]){if(e.length===0)return[n];const[s,...r]=e;if(s===N){const f=I(t,n);if(!Array.isArray(f))return[];const u=[];for(let a=0;a<f.length;a++)u.push(...k(r,t,[...n,a]));return u}return k(r,t,[...n,s])}function H(e,t){return de(e,t)}async function L(e,t,n,s){const r=structuredClone(e),f=[];for(const o of t)for(const c of k(o.path,r)){const l=pe(o,c,r,s);f.push({field:o,concretePath:c,context:l})}const u=await Promise.allSettled(f.map(({field:o,context:c})=>new Promise(l=>l(n(o.formula,c))))),a=[];for(let o=0;o<f.length;o++){const{concretePath:c}=f[o],l=u[o];if(l.status==="fulfilled")V(r,c,l.value);else{const y=l.reason instanceof Error?l.reason:new Error(String(l.reason));a.push({path:c,error:y}),V(r,c,void 0)}}return{data:r,errors:a}}function ge(e,t,n){for(const s of n)for(const r of k(s.path,t))if(!H(I(e,r),I(t,r)))return!1;return!0}async function me(e,t,n,s,r,f,u,a,o){const c=t.filter(d=>d.condition===!0||a(d.condition,e));if(c.length===0)return e;const l=new Map,y=[];for(const d of c){const p=JSON.stringify(d.path);if(l.has(p)){if(o==="error")throw new TypeError(`[rjsf-formulas] Formula conflict: two active fields share path [${B(d.path)}]`);o==="warn"&&console.warn(`[rjsf-formulas] Formula conflict: two active fields share path [${B(d.path)}]; taking last`);const g=y.indexOf(l.get(p));y[g]=d,l.set(p,d)}else l.set(p,d),y.push(d)}const m={formulaDataKey:f,formulaPathKey:u};let h=e;for(let d=0;d<s;d++){const{data:p,errors:g}=await L(h,y,n,m);if(ge(h,p,y)){for(const{path:v,error:$}of g)r==null||r(v,$);return p}h=p}const{data:O}=await L(h,y,n,m),j=structuredClone(O);for(const d of y)for(const p of k(d.path,O))H(I(h,p),I(O,p))||(r==null||r(p,new Error(`[rjsf-formulas] Formula at [${p.join(", ")}] did not converge after ${s} passes`)),V(j,p,void 0));return j}function he(e){return JSON.stringify(e)}function je(e,t,n,s,r,f,u,a,o,c){const[l,y]=i.useState(e),m=i.useRef(e),h=i.useRef(e),O=i.useRef(new Map),j=i.useRef(null),d=i.useRef(!1),p=i.useRef(f),g=i.useRef(u),v=i.useRef(n),$=i.useRef(t),P=i.useRef(r),b=i.useRef(a),M=i.useRef(o),x=i.useRef(c);i.useEffect(()=>{p.current=f},[f]),i.useEffect(()=>{g.current=u},[u]),i.useEffect(()=>{v.current=n},[n]),i.useEffect(()=>{$.current=t},[t]),i.useEffect(()=>{P.current=r},[r]),i.useEffect(()=>{b.current=a},[a]),i.useEffect(()=>{M.current=o},[o]),i.useEffect(()=>{x.current=c},[c]);const S=i.useCallback(()=>{if(d.current)return;(async()=>{var _,T;d.current=!0;try{for(;;){const D=m.current,W=$.current,ee=W.filter(A=>A.condition===!0||M.current(A.condition,D)),q=[];for(const A of ee)for(const ne of k(A.path,D))q.push(ne);const K=O.current;K.clear();for(const A of q)K.set(he(A),"running");q.length>0&&((_=g.current)==null||_.call(g,q));const te=await me(D,W,v.current,P.current,p.current,b.current.formulaDataKey,b.current.formulaPathKey,M.current,x.current);if(z.current)break;const re=[...K.values()].some(A=>A==="dirty");if(K.clear(),!re){y(te),q.length>0&&((T=g.current)==null||T.call(g,[]));break}}}finally{d.current=!1}})()},[]),R=i.useCallback(Q=>{m.current=Q;const _=O.current;for(const[T,D]of _)D==="running"&&_.set(T,"dirty");d.current||(j.current!==null&&clearTimeout(j.current),j.current=setTimeout(()=>{j.current=null,S()},s))},[s,S]),E=i.useRef(!1),z=i.useRef(!1);return i.useEffect(()=>{E.current?H(e,h.current)||(h.current=e,R(e)):(E.current=!0,h.current=e,R(e))}),i.useEffect(()=>(z.current=!1,()=>{z.current=!0,E.current=!1,j.current!==null&&clearTimeout(j.current)}),[]),{enrichedFormData:l,handleInput:R}}function Re(e,t){const n=structuredClone(e??{}),s=we(t);for(const r of t)F(n,r.path,s);return n}function we(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 f=s[r];if(typeof f!="number"&&f!==N)continue;const u=G(s.slice(0,r)),a=typeof f=="number";e.some((c,l)=>{if(l===n||c.path.length<=r||G(c.path.slice(0,r))!==u)return!1;const y=c.path[r];return a?y===N:typeof y=="number"})&&t.add(u)}}return t}function G(e){return JSON.stringify(e.map(t=>typeof t=="symbol"?"__ARRAY_INDEX__":t))}function F(e,t,n,s=[]){if(t.length===0){e["ui:readonly"]=!0;return}const[r,...f]=t;if(typeof r=="string"){(!e[r]||typeof e[r]!="object"||Array.isArray(e[r]))&&(e[r]={}),F(e[r],f,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 o=e.items;(!o[r]||typeof o[r]!="object")&&(o[r]={}),F(o[r],f,n,[...s,r]);return}const u=G(s);if(n.has(u))(!e.additionalItems||typeof e.additionalItems!="object")&&(e.additionalItems={}),F(e.additionalItems,f,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={}),F(e.items,f,n,[...s,r])}}function Oe(e){const{schema:t,formData:n,validator:s,uiSchema:r,evaluator:f,Form:u=oe,formulaKey:a="x-formula",formulaContextKey:o="x-formula-context",formulaDataKey:c="__formData__",formulaPathKey:l="__path__",maxConvergencePasses:y=10,debounceMs:m=300,onFormulaError:h,onLoadingChange:O,formulaConflictBehavior:j="warn",onChange:d,...p}=e,g=i.useMemo(()=>C(t,{formulaKey:a,formulaContextKey:o}),[t,a,o]),v=i.useMemo(()=>({formulaDataKey:c,formulaPathKey:l}),[c,l]),$=i.useMemo(()=>Re(r,g),[r,g]),P=i.useCallback((R,E)=>s.isValid(R,E,t),[s,t]),{enrichedFormData:b,handleInput:M}=je(n,g,f,m,y,h,O,v,P,j),x=i.useRef(d);i.useEffect(()=>{x.current=d},[d]),i.useEffect(()=>{var R;g.length!==0&&((R=x.current)==null||R.call(x,{formData:b},void 0))},[b]);const S=(R,E)=>{M(R.formData),d==null||d(R,E)};return fe.jsx(u,{...p,schema:t,validator:s,formData:b,uiSchema:$,onChange:S})}exports.FormulaForm=Oe;exports.analyzeSchema=C;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const se=require("@rjsf/utils"),oe=require("react/jsx-runtime"),i=require("react"),fe=require("@rjsf/core"),U=Symbol("arrayIndex");function C(e,t){const r=(t==null?void 0:t.formulaKey)??"x-formula",s=(t==null?void 0:t.formulaContextKey)??"x-formula-context",n=(t==null?void 0:t.formulaConflictBehavior)??"warn",o=[];return O(e,[],o,r,s,n,!0,e),o}function J(e,t){if(e===!0)return t;const r=e;return Array.isArray(r.allOf)?{allOf:[...r.allOf,t]}:{allOf:[e,t]}}function ce(e,t){return e===void 0||e==="siblings"?"siblings":e==="extended"?"extended":(console.warn(`[rjsf-formulas] Unknown x-formula-context value "${e}" at path [${z(t)}], treating as "siblings"`),"siblings")}function z(e){return e.map(t=>typeof t=="symbol"?"[*]":String(t)).join(", ")}function Y(e){return e.map(t=>typeof t=="symbol"?"[*]":String(t)).join(".")}function ue(e,t,r){const s=Y(e.path);if(r==="error")throw new TypeError(`[rjsf-formulas] Formula conflict at path [${z(e.path)}]: "${e.formula}" vs "${t.formula}". Resolve the conflict or set formulaConflictBehavior to 'ignore' or 'warn'.`);r==="warn"&&console.warn(`[rjsf-formulas] Formula conflict at path [${s}]: "${e.formula}" (earlier allOf branch) is overridden by "${t.formula}" (later allOf branch).`)}function ie(e,t,r){for(const s of t){const n=Y(s.path),o=e.findIndex(u=>Y(u.path)===n);o!==-1?(ue(e[o],s,r),e[o]=s):e.push(s)}}function O(e,t,r,s,n,o,u,l){const f=e;if(typeof f.$ref=="string"){const c=se.findSchemaDefinition(f.$ref,l);O(c,t,r,s,n,o,u,l);return}if(s in e){const c=f[s];if(typeof c!="string"){console.warn(`[rjsf-formulas] Formula at path [${z(t)}] is not a string (got ${typeof c}), skipping.`);return}r.push({path:t,formula:c,contextMode:ce(f[n],t),condition:u});return}if(e.properties&&e.type!=="array")for(const[c,a]of Object.entries(e.properties))O(a,[...t,c],r,s,n,o,u,l);if(e.type==="array"&&(Array.isArray(f.prefixItems)&&f.prefixItems.forEach((a,y)=>{O(a,[...t,y],r,s,n,o,u,l)}),e.items&&!Array.isArray(e.items)&&O(e.items,[...t,U],r,s,n,o,u,l)),Array.isArray(f.allOf)){const c=f.allOf;for(const a of c){const y=[];O(a,t,y,s,n,o,u,l),ie(r,y,o)}}for(const c of["oneOf","anyOf"])if(Array.isArray(f[c])){const a=f[c];for(const y of a){const m=J(u,y);O(y,t,r,s,n,o,m,l)}}if("if"in f){const c=f.if,a=f.then,y=f.else;if(s in c&&console.warn(`[rjsf-formulas] Formula key "${s}" found inside an "if" schema at path [${z(t)}]. The "if" schema is a validation predicate and is never rendered — the formula will be ignored.`),a!==void 0){const m=J(u,c);O(a,t,r,s,n,o,m,l)}if(y!==void 0){const m=J(u,{not:c});O(y,t,r,s,n,o,m,l)}}}function ae(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var X,Z;function le(){return Z||(Z=1,X=function e(t,r){if(t===r)return!0;if(t&&r&&typeof t=="object"&&typeof r=="object"){if(t.constructor!==r.constructor)return!1;var s,n,o;if(Array.isArray(t)){if(s=t.length,s!=r.length)return!1;for(n=s;n--!==0;)if(!e(t[n],r[n]))return!1;return!0}if(t.constructor===RegExp)return t.source===r.source&&t.flags===r.flags;if(t.valueOf!==Object.prototype.valueOf)return t.valueOf()===r.valueOf();if(t.toString!==Object.prototype.toString)return t.toString()===r.toString();if(o=Object.keys(t),s=o.length,s!==Object.keys(r).length)return!1;for(n=s;n--!==0;)if(!Object.prototype.hasOwnProperty.call(r,o[n]))return!1;for(n=s;n--!==0;){var u=o[n];if(!e(t[u],r[u]))return!1}return!0}return t!==t&&r!==r}),X}var ye=le();const de=ae(ye);function pe(e,t,r,s){const o=t.slice(0,-1).reduce((l,f)=>{if(!(l==null||typeof l!="object"))return l[f]},r),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]:r,[s.formulaPathKey]:[...t]}):u}function B(e){return e.map(t=>typeof t=="symbol"?"[*]":String(t)).join(", ")}function I(e,t){return t.reduce((r,s)=>{if(!(r==null||typeof r!="object"))return r[s]},e)}function V(e,t,r){if(t.length===0)return;const s=I(e,t.slice(0,-1));s!=null&&typeof s=="object"&&(s[t[t.length-1]]=r)}function k(e,t,r=[]){if(e.length===0)return[r];const[s,...n]=e;if(s===U){const o=I(t,r);if(!Array.isArray(o))return[];const u=[];for(let l=0;l<o.length;l++)u.push(...k(n,t,[...r,l]));return u}return k(n,t,[...r,s])}function H(e,t){return de(e,t)}async function L(e,t,r,s){const n=structuredClone(e),o=[];for(const f of t)for(const c of k(f.path,n)){const a=pe(f,c,n,s);o.push({field:f,concretePath:c,context:a})}const u=await Promise.allSettled(o.map(({field:f,context:c})=>new Promise(a=>a(r(f.formula,c))))),l=[];for(let f=0;f<o.length;f++){const{concretePath:c}=o[f],a=u[f];if(a.status==="fulfilled")V(n,c,a.value);else{const y=a.reason instanceof Error?a.reason:new Error(String(a.reason));l.push({path:c,error:y}),V(n,c,void 0)}}return{data:n,errors:l}}function me(e,t,r){for(const s of r)for(const n of k(s.path,t))if(!H(I(e,n),I(t,n)))return!1;return!0}async function ge(e,t,r,s,n,o,u,l,f){const c=t.filter(p=>p.condition===!0||l(p.condition,e));if(c.length===0)return e;const a=new Map,y=[];for(const p of c){const d=JSON.stringify(p.path);if(a.has(d)){if(f==="error")throw new TypeError(`[rjsf-formulas] Formula conflict: two active fields share path [${B(p.path)}]`);f==="warn"&&console.warn(`[rjsf-formulas] Formula conflict: two active fields share path [${B(p.path)}]; taking last`);const g=y.indexOf(a.get(d));y[g]=p,a.set(d,p)}else a.set(d,p),y.push(p)}const m={formulaDataKey:o,formulaPathKey:u};let h=e;for(let p=0;p<s;p++){const{data:d,errors:g}=await L(h,y,r,m);if(me(h,d,y)){for(const{path:w,error:$}of g)n==null||n(w,$);return d}h=d}const{data:A}=await L(h,y,r,m),j=structuredClone(A);for(const p of y)for(const d of k(p.path,A))H(I(h,d),I(A,d))||(n==null||n(d,new Error(`[rjsf-formulas] Formula at [${d.join(", ")}] did not converge after ${s} passes`)),V(j,d,void 0));return j}function he(e){return JSON.stringify(e)}function je(e,t,r,s,n,o,u,l,f,c){const[a,y]=i.useState(e),m=i.useRef(e),h=i.useRef(e),A=i.useRef(new Map),j=i.useRef(null),p=i.useRef(!1),d=i.useRef(o),g=i.useRef(u),w=i.useRef(r),$=i.useRef(t),S=i.useRef(n),F=i.useRef(l),x=i.useRef(f),T=i.useRef(c);i.useEffect(()=>{d.current=o},[o]),i.useEffect(()=>{g.current=u},[u]),i.useEffect(()=>{w.current=r},[r]),i.useEffect(()=>{$.current=t},[t]),i.useEffect(()=>{S.current=n},[n]),i.useEffect(()=>{F.current=l},[l]),i.useEffect(()=>{x.current=f},[f]),i.useEffect(()=>{T.current=c},[c]);const E=i.useCallback(()=>{if(p.current)return;(async()=>{var _,K;p.current=!0;try{for(;;){const D=m.current,W=$.current,ee=W.filter(b=>b.condition===!0||x.current(b.condition,D)),q=[];for(const b of ee)for(const ne of k(b.path,D))q.push(ne);const N=A.current;N.clear();for(const b of q)N.set(he(b),"running");q.length>0&&((_=g.current)==null||_.call(g,q));const te=await ge(D,W,w.current,S.current,d.current,F.current.formulaDataKey,F.current.formulaPathKey,x.current,T.current);if(v.current)break;const re=[...N.values()].some(b=>b==="dirty");if(N.clear(),!re){y(te),q.length>0&&((K=g.current)==null||K.call(g,[]));break}}}finally{p.current=!1}})()},[]),M=i.useCallback(Q=>{m.current=Q;const _=A.current;for(const[K,D]of _)D==="running"&&_.set(K,"dirty");p.current||(j.current!==null&&clearTimeout(j.current),j.current=setTimeout(()=>{j.current=null,E()},s))},[s,E]),R=i.useRef(!1),v=i.useRef(!1);return i.useEffect(()=>{R.current?H(e,h.current)||(h.current=e,M(e)):(R.current=!0,h.current=e,M(e))}),i.useEffect(()=>(v.current=!1,()=>{v.current=!0,R.current=!1,j.current!==null&&clearTimeout(j.current)}),[]),{enrichedFormData:a,handleInput:M}}function Re(e,t){const r=structuredClone(e??{}),s=we(t);for(const n of t)P(r,n.path,s);return r}function we(e){const t=new Set;for(let r=0;r<e.length;r++){const s=e[r].path;for(let n=0;n<s.length;n++){const o=s[n];if(typeof o!="number"&&o!==U)continue;const u=G(s.slice(0,n)),l=typeof o=="number";e.some((c,a)=>{if(a===r||c.path.length<=n||G(c.path.slice(0,n))!==u)return!1;const y=c.path[n];return l?y===U:typeof y=="number"})&&t.add(u)}}return t}function G(e){return JSON.stringify(e.map(t=>typeof t=="symbol"?"__ARRAY_INDEX__":t))}function P(e,t,r,s=[]){if(t.length===0){e["ui:readonly"]=!0;return}const[n,...o]=t;if(typeof n=="string"){(!e[n]||typeof e[n]!="object"||Array.isArray(e[n]))&&(e[n]={}),P(e[n],o,r,[...s,n]);return}if(typeof n=="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[n]||typeof f[n]!="object")&&(f[n]={}),P(f[n],o,r,[...s,n]);return}const u=G(s);if(r.has(u))(!e.additionalItems||typeof e.additionalItems!="object")&&(e.additionalItems={}),P(e.additionalItems,o,r,[...s,n]);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={}),P(e.items,o,r,[...s,n])}}function Oe(e,t){const{schema:r,formData:s,validator:n,uiSchema:o,evaluator:u,Form:l=fe,formulaKey:f="x-formula",formulaContextKey:c="x-formula-context",formulaDataKey:a="__formData__",formulaPathKey:y="__path__",maxConvergencePasses:m=10,debounceMs:h=300,onFormulaError:A,onLoadingChange:j,formulaConflictBehavior:p="warn",onChange:d,...g}=e,w=i.useMemo(()=>C(r,{formulaKey:f,formulaContextKey:c}),[r,f,c]),$=i.useMemo(()=>({formulaDataKey:a,formulaPathKey:y}),[a,y]),S=i.useMemo(()=>Re(o,w),[o,w]),F=i.useCallback((R,v)=>n.isValid(R,v,r),[n,r]),{enrichedFormData:x,handleInput:T}=je(s,w,u,h,m,A,j,$,F,p),E=i.useRef(d);i.useEffect(()=>{E.current=d},[d]),i.useEffect(()=>{var R;w.length!==0&&((R=E.current)==null||R.call(E,{formData:x},void 0))},[x]);const M=(R,v)=>{T(R.formData),d==null||d(R,v)};return oe.jsx(l,{ref:t,...g,schema:r,validator:n,formData:x,uiSchema:S,onChange:M})}const Ae=i.forwardRef(Oe);exports.FormulaForm=Ae;exports.analyzeSchema=C;
package/dist/index.d.cts CHANGED
@@ -88,43 +88,7 @@ export declare type FormulaField = {
88
88
  condition: RJSFSchema | true;
89
89
  };
90
90
 
91
- /**
92
- * Drop-in replacement for RJSF's `<Form>` with support for computed fields.
93
- *
94
- * @remarks
95
- * Fields marked with a formula key in the JSON schema are evaluated automatically
96
- * on every data change and rendered as read-only. The `evaluator` prop receives
97
- * the formula string and a context object, and returns the computed value.
98
- *
99
- * @example
100
- * ```tsx
101
- * import { FormulaForm } from '@a-ludi/rjsf-formulas'
102
- * import validator from '@rjsf/validator-ajv8'
103
- * import { Parser } from 'expr-eval'
104
- *
105
- * const parser = new Parser()
106
- *
107
- * const schema = {
108
- * type: 'object' as const,
109
- * properties: {
110
- * price: { type: 'number' as const },
111
- * quantity: { type: 'number' as const },
112
- * total: { type: 'number' as const, 'x-formula': 'price * quantity' },
113
- * },
114
- * }
115
- *
116
- * function MyForm() {
117
- * return (
118
- * <FormulaForm
119
- * schema={schema}
120
- * validator={validator}
121
- * evaluator={(formula, ctx) => parser.evaluate(formula, ctx)}
122
- * />
123
- * )
124
- * }
125
- * ```
126
- */
127
- export declare function FormulaForm<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(props: FormulaFormProps<T, S, F>): JSX.Element;
91
+ export declare const FormulaForm: <T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(props: FormulaFormProps<T, S, F>) => JSX.Element;
128
92
 
129
93
  /**
130
94
  * Props accepted by {@link FormulaForm}.
package/dist/index.d.mts CHANGED
@@ -88,43 +88,7 @@ export declare type FormulaField = {
88
88
  condition: RJSFSchema | true;
89
89
  };
90
90
 
91
- /**
92
- * Drop-in replacement for RJSF's `<Form>` with support for computed fields.
93
- *
94
- * @remarks
95
- * Fields marked with a formula key in the JSON schema are evaluated automatically
96
- * on every data change and rendered as read-only. The `evaluator` prop receives
97
- * the formula string and a context object, and returns the computed value.
98
- *
99
- * @example
100
- * ```tsx
101
- * import { FormulaForm } from '@a-ludi/rjsf-formulas'
102
- * import validator from '@rjsf/validator-ajv8'
103
- * import { Parser } from 'expr-eval'
104
- *
105
- * const parser = new Parser()
106
- *
107
- * const schema = {
108
- * type: 'object' as const,
109
- * properties: {
110
- * price: { type: 'number' as const },
111
- * quantity: { type: 'number' as const },
112
- * total: { type: 'number' as const, 'x-formula': 'price * quantity' },
113
- * },
114
- * }
115
- *
116
- * function MyForm() {
117
- * return (
118
- * <FormulaForm
119
- * schema={schema}
120
- * validator={validator}
121
- * evaluator={(formula, ctx) => parser.evaluate(formula, ctx)}
122
- * />
123
- * )
124
- * }
125
- * ```
126
- */
127
- export declare function FormulaForm<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(props: FormulaFormProps<T, S, F>): JSX.Element;
91
+ export declare const FormulaForm: <T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(props: FormulaFormProps<T, S, F>) => JSX.Element;
128
92
 
129
93
  /**
130
94
  * Props accepted by {@link FormulaForm}.
package/dist/index.mjs CHANGED
@@ -1,363 +1,363 @@
1
1
  import { findSchemaDefinition as ft } from "@rjsf/utils";
2
2
  import { jsx as ct } from "react/jsx-runtime";
3
- import { useState as it, useRef as m, useEffect as h, useCallback as G, useMemo as X } from "react";
4
- import ut from "@rjsf/core";
5
- const N = Symbol("arrayIndex");
6
- function at(t, e) {
7
- const n = (e == null ? void 0 : e.formulaKey) ?? "x-formula", s = (e == null ? void 0 : e.formulaContextKey) ?? "x-formula-context", r = (e == null ? void 0 : e.formulaConflictBehavior) ?? "warn", o = [];
8
- return A(t, [], o, n, s, r, !0, t), o;
3
+ import { useState as it, useRef as d, useEffect as h, useCallback as G, forwardRef as ut, useMemo as X } from "react";
4
+ import at from "@rjsf/core";
5
+ const J = Symbol("arrayIndex");
6
+ function lt(t, e) {
7
+ const r = (e == null ? void 0 : e.formulaKey) ?? "x-formula", o = (e == null ? void 0 : e.formulaContextKey) ?? "x-formula-context", n = (e == null ? void 0 : e.formulaConflictBehavior) ?? "warn", s = [];
8
+ return x(t, [], s, r, o, n, !0, t), s;
9
9
  }
10
10
  function Y(t, e) {
11
11
  if (t === !0) return e;
12
- const n = t;
13
- return Array.isArray(n.allOf) ? { allOf: [...n.allOf, e] } : { allOf: [t, e] };
12
+ const r = t;
13
+ return Array.isArray(r.allOf) ? { allOf: [...r.allOf, e] } : { allOf: [t, e] };
14
14
  }
15
- function lt(t, e) {
15
+ function pt(t, e) {
16
16
  return t === void 0 || t === "siblings" ? "siblings" : t === "extended" ? "extended" : (console.warn(
17
- `[rjsf-formulas] Unknown x-formula-context value "${t}" at path [${U(e)}], treating as "siblings"`
17
+ `[rjsf-formulas] Unknown x-formula-context value "${t}" at path [${z(e)}], treating as "siblings"`
18
18
  ), "siblings");
19
19
  }
20
- function U(t) {
20
+ function z(t) {
21
21
  return t.map((e) => typeof e == "symbol" ? "[*]" : String(e)).join(", ");
22
22
  }
23
23
  function H(t) {
24
24
  return t.map((e) => typeof e == "symbol" ? "[*]" : String(e)).join(".");
25
25
  }
26
- function pt(t, e, n) {
27
- const s = H(t.path);
28
- if (n === "error")
26
+ function yt(t, e, r) {
27
+ const o = H(t.path);
28
+ if (r === "error")
29
29
  throw new TypeError(
30
- `[rjsf-formulas] Formula conflict at path [${U(t.path)}]: "${t.formula}" vs "${e.formula}". Resolve the conflict or set formulaConflictBehavior to 'ignore' or 'warn'.`
30
+ `[rjsf-formulas] Formula conflict at path [${z(t.path)}]: "${t.formula}" vs "${e.formula}". Resolve the conflict or set formulaConflictBehavior to 'ignore' or 'warn'.`
31
31
  );
32
- n === "warn" && console.warn(
33
- `[rjsf-formulas] Formula conflict at path [${s}]: "${t.formula}" (earlier allOf branch) is overridden by "${e.formula}" (later allOf branch).`
32
+ r === "warn" && console.warn(
33
+ `[rjsf-formulas] Formula conflict at path [${o}]: "${t.formula}" (earlier allOf branch) is overridden by "${e.formula}" (later allOf branch).`
34
34
  );
35
35
  }
36
- function yt(t, e, n) {
37
- for (const s of e) {
38
- const r = H(s.path), o = t.findIndex((i) => H(i.path) === r);
39
- o !== -1 ? (pt(t[o], s, n), t[o] = s) : t.push(s);
36
+ function dt(t, e, r) {
37
+ for (const o of e) {
38
+ const n = H(o.path), s = t.findIndex((i) => H(i.path) === n);
39
+ s !== -1 ? (yt(t[s], o, r), t[s] = o) : t.push(o);
40
40
  }
41
41
  }
42
- function A(t, e, n, s, r, o, i, u) {
42
+ function x(t, e, r, o, n, s, i, a) {
43
43
  const f = t;
44
44
  if (typeof f.$ref == "string") {
45
- const c = ft(f.$ref, u);
46
- A(c, e, n, s, r, o, i, u);
45
+ const c = ft(f.$ref, a);
46
+ x(c, e, r, o, n, s, i, a);
47
47
  return;
48
48
  }
49
- if (s in t) {
50
- const c = f[s];
49
+ if (o in t) {
50
+ const c = f[o];
51
51
  if (typeof c != "string") {
52
52
  console.warn(
53
- `[rjsf-formulas] Formula at path [${U(e)}] is not a string (got ${typeof c}), skipping.`
53
+ `[rjsf-formulas] Formula at path [${z(e)}] is not a string (got ${typeof c}), skipping.`
54
54
  );
55
55
  return;
56
56
  }
57
- n.push({
57
+ r.push({
58
58
  path: e,
59
59
  formula: c,
60
- contextMode: lt(f[r], e),
60
+ contextMode: pt(f[n], e),
61
61
  condition: i
62
62
  });
63
63
  return;
64
64
  }
65
65
  if (t.properties && t.type !== "array")
66
- for (const [c, a] of Object.entries(t.properties))
67
- A(
68
- a,
66
+ for (const [c, u] of Object.entries(t.properties))
67
+ x(
68
+ u,
69
69
  [...e, c],
70
- n,
71
- s,
72
70
  r,
73
71
  o,
72
+ n,
73
+ s,
74
74
  i,
75
- u
75
+ a
76
76
  );
77
- if (t.type === "array" && (Array.isArray(f.prefixItems) && f.prefixItems.forEach((a, l) => {
78
- A(a, [...e, l], n, s, r, o, i, u);
79
- }), t.items && !Array.isArray(t.items) && A(
77
+ if (t.type === "array" && (Array.isArray(f.prefixItems) && f.prefixItems.forEach((u, l) => {
78
+ x(u, [...e, l], r, o, n, s, i, a);
79
+ }), t.items && !Array.isArray(t.items) && x(
80
80
  t.items,
81
- [...e, N],
82
- n,
83
- s,
81
+ [...e, J],
84
82
  r,
85
83
  o,
84
+ n,
85
+ s,
86
86
  i,
87
- u
87
+ a
88
88
  )), Array.isArray(f.allOf)) {
89
89
  const c = f.allOf;
90
- for (const a of c) {
90
+ for (const u of c) {
91
91
  const l = [];
92
- A(a, e, l, s, r, o, i, u), yt(n, l, o);
92
+ x(u, e, l, o, n, s, i, a), dt(r, l, s);
93
93
  }
94
94
  }
95
95
  for (const c of ["oneOf", "anyOf"])
96
96
  if (Array.isArray(f[c])) {
97
- const a = f[c];
98
- for (const l of a) {
99
- const g = Y(i, l);
100
- A(l, e, n, s, r, o, g, u);
97
+ const u = f[c];
98
+ for (const l of u) {
99
+ const m = Y(i, l);
100
+ x(l, e, r, o, n, s, m, a);
101
101
  }
102
102
  }
103
103
  if ("if" in f) {
104
- const c = f.if, a = f.then, l = f.else;
105
- if (s in c && console.warn(
106
- `[rjsf-formulas] Formula key "${s}" found inside an "if" schema at path [${U(e)}]. The "if" schema is a validation predicate and is never rendered — the formula will be ignored.`
107
- ), a !== void 0) {
108
- const g = Y(i, c);
109
- A(a, e, n, s, r, o, g, u);
104
+ const c = f.if, u = f.then, l = f.else;
105
+ if (o in c && console.warn(
106
+ `[rjsf-formulas] Formula key "${o}" found inside an "if" schema at path [${z(e)}]. The "if" schema is a validation predicate and is never rendered — the formula will be ignored.`
107
+ ), u !== void 0) {
108
+ const m = Y(i, c);
109
+ x(u, e, r, o, n, s, m, a);
110
110
  }
111
111
  if (l !== void 0) {
112
- const g = Y(i, { not: c });
113
- A(l, e, n, s, r, o, g, u);
112
+ const m = Y(i, { not: c });
113
+ x(l, e, r, o, n, s, m, a);
114
114
  }
115
115
  }
116
116
  }
117
- function dt(t) {
117
+ function mt(t) {
118
118
  return t && t.__esModule && Object.prototype.hasOwnProperty.call(t, "default") ? t.default : t;
119
119
  }
120
120
  var V, C;
121
- function mt() {
122
- return C || (C = 1, V = function t(e, n) {
123
- if (e === n) return !0;
124
- if (e && n && typeof e == "object" && typeof n == "object") {
125
- if (e.constructor !== n.constructor) return !1;
126
- var s, r, o;
121
+ function gt() {
122
+ return C || (C = 1, V = function t(e, r) {
123
+ if (e === r) return !0;
124
+ if (e && r && typeof e == "object" && typeof r == "object") {
125
+ if (e.constructor !== r.constructor) return !1;
126
+ var o, n, s;
127
127
  if (Array.isArray(e)) {
128
- if (s = e.length, s != n.length) return !1;
129
- for (r = s; r-- !== 0; )
130
- if (!t(e[r], n[r])) return !1;
128
+ if (o = e.length, o != r.length) return !1;
129
+ for (n = o; n-- !== 0; )
130
+ if (!t(e[n], r[n])) return !1;
131
131
  return !0;
132
132
  }
133
- if (e.constructor === RegExp) return e.source === n.source && e.flags === n.flags;
134
- if (e.valueOf !== Object.prototype.valueOf) return e.valueOf() === n.valueOf();
135
- if (e.toString !== Object.prototype.toString) return e.toString() === n.toString();
136
- if (o = Object.keys(e), s = o.length, s !== Object.keys(n).length) return !1;
137
- for (r = s; r-- !== 0; )
138
- if (!Object.prototype.hasOwnProperty.call(n, o[r])) return !1;
139
- for (r = s; r-- !== 0; ) {
140
- var i = o[r];
141
- if (!t(e[i], n[i])) return !1;
133
+ if (e.constructor === RegExp) return e.source === r.source && e.flags === r.flags;
134
+ if (e.valueOf !== Object.prototype.valueOf) return e.valueOf() === r.valueOf();
135
+ if (e.toString !== Object.prototype.toString) return e.toString() === r.toString();
136
+ if (s = Object.keys(e), o = s.length, o !== Object.keys(r).length) return !1;
137
+ for (n = o; n-- !== 0; )
138
+ if (!Object.prototype.hasOwnProperty.call(r, s[n])) return !1;
139
+ for (n = o; n-- !== 0; ) {
140
+ var i = s[n];
141
+ if (!t(e[i], r[i])) return !1;
142
142
  }
143
143
  return !0;
144
144
  }
145
- return e !== e && n !== n;
145
+ return e !== e && r !== r;
146
146
  }), V;
147
147
  }
148
- var gt = mt();
149
- const ht = /* @__PURE__ */ dt(gt);
150
- function jt(t, e, n, s) {
151
- const o = e.slice(0, -1).reduce((u, f) => {
152
- if (!(u == null || typeof u != "object"))
153
- return u[f];
154
- }, n), i = o != null && typeof o == "object" && !Array.isArray(o) ? { ...o } : {};
155
- return t.contextMode === "extended" ? ((s.formulaDataKey in i || s.formulaPathKey in i) && console.warn(
156
- `[rjsf-formulas] Extended context key collision: sibling field named "${s.formulaDataKey in i ? s.formulaDataKey : s.formulaPathKey}" will be overwritten by the injected context. Use formulaDataKey/formulaPathKey props to choose a different key.`
148
+ var ht = gt();
149
+ const jt = /* @__PURE__ */ mt(ht);
150
+ function wt(t, e, r, o) {
151
+ const s = e.slice(0, -1).reduce((a, f) => {
152
+ if (!(a == null || typeof a != "object"))
153
+ return a[f];
154
+ }, r), i = s != null && typeof s == "object" && !Array.isArray(s) ? { ...s } : {};
155
+ return t.contextMode === "extended" ? ((o.formulaDataKey in i || o.formulaPathKey in i) && console.warn(
156
+ `[rjsf-formulas] Extended context key collision: sibling field named "${o.formulaDataKey in i ? o.formulaDataKey : o.formulaPathKey}" will be overwritten by the injected context. Use formulaDataKey/formulaPathKey props to choose a different key.`
157
157
  ), {
158
158
  ...i,
159
- [s.formulaDataKey]: n,
160
- [s.formulaPathKey]: [...e]
159
+ [o.formulaDataKey]: r,
160
+ [o.formulaPathKey]: [...e]
161
161
  }) : i;
162
162
  }
163
163
  function tt(t) {
164
164
  return t.map((e) => typeof e == "symbol" ? "[*]" : String(e)).join(", ");
165
165
  }
166
166
  function k(t, e) {
167
- return e.reduce((n, s) => {
168
- if (!(n == null || typeof n != "object"))
169
- return n[s];
167
+ return e.reduce((r, o) => {
168
+ if (!(r == null || typeof r != "object"))
169
+ return r[o];
170
170
  }, t);
171
171
  }
172
- function Q(t, e, n) {
172
+ function Q(t, e, r) {
173
173
  if (e.length === 0) return;
174
- const s = k(t, e.slice(0, -1));
175
- s != null && typeof s == "object" && (s[e[e.length - 1]] = n);
174
+ const o = k(t, e.slice(0, -1));
175
+ o != null && typeof o == "object" && (o[e[e.length - 1]] = r);
176
176
  }
177
- function D(t, e, n = []) {
178
- if (t.length === 0) return [n];
179
- const [s, ...r] = t;
180
- if (s === N) {
181
- const o = k(e, n);
182
- if (!Array.isArray(o)) return [];
177
+ function D(t, e, r = []) {
178
+ if (t.length === 0) return [r];
179
+ const [o, ...n] = t;
180
+ if (o === J) {
181
+ const s = k(e, r);
182
+ if (!Array.isArray(s)) return [];
183
183
  const i = [];
184
- for (let u = 0; u < o.length; u++)
185
- i.push(...D(r, e, [...n, u]));
184
+ for (let a = 0; a < s.length; a++)
185
+ i.push(...D(n, e, [...r, a]));
186
186
  return i;
187
187
  }
188
- return D(r, e, [...n, s]);
188
+ return D(n, e, [...r, o]);
189
189
  }
190
190
  function Z(t, e) {
191
- return ht(t, e);
191
+ return jt(t, e);
192
192
  }
193
- async function et(t, e, n, s) {
194
- const r = structuredClone(t), o = [];
193
+ async function et(t, e, r, o) {
194
+ const n = structuredClone(t), s = [];
195
195
  for (const f of e)
196
- for (const c of D(f.path, r)) {
197
- const a = jt(f, c, r, s);
198
- o.push({ field: f, concretePath: c, context: a });
196
+ for (const c of D(f.path, n)) {
197
+ const u = wt(f, c, n, o);
198
+ s.push({ field: f, concretePath: c, context: u });
199
199
  }
200
200
  const i = await Promise.allSettled(
201
- o.map(({ field: f, context: c }) => new Promise((a) => a(n(f.formula, c))))
202
- ), u = [];
203
- for (let f = 0; f < o.length; f++) {
204
- const { concretePath: c } = o[f], a = i[f];
205
- if (a.status === "fulfilled")
206
- Q(r, c, a.value);
201
+ s.map(({ field: f, context: c }) => new Promise((u) => u(r(f.formula, c))))
202
+ ), a = [];
203
+ for (let f = 0; f < s.length; f++) {
204
+ const { concretePath: c } = s[f], u = i[f];
205
+ if (u.status === "fulfilled")
206
+ Q(n, c, u.value);
207
207
  else {
208
- const l = a.reason instanceof Error ? a.reason : new Error(String(a.reason));
209
- u.push({ path: c, error: l }), Q(r, c, void 0);
208
+ const l = u.reason instanceof Error ? u.reason : new Error(String(u.reason));
209
+ a.push({ path: c, error: l }), Q(n, c, void 0);
210
210
  }
211
211
  }
212
- return { data: r, errors: u };
212
+ return { data: n, errors: a };
213
213
  }
214
- function wt(t, e, n) {
215
- for (const s of n)
216
- for (const r of D(s.path, e))
217
- if (!Z(k(t, r), k(e, r)))
214
+ function Ot(t, e, r) {
215
+ for (const o of r)
216
+ for (const n of D(o.path, e))
217
+ if (!Z(k(t, n), k(e, n)))
218
218
  return !1;
219
219
  return !0;
220
220
  }
221
- async function Ot(t, e, n, s, r, o, i, u, f) {
221
+ async function At(t, e, r, o, n, s, i, a, f) {
222
222
  const c = e.filter(
223
- (p) => p.condition === !0 || u(p.condition, t)
223
+ (y) => y.condition === !0 || a(y.condition, t)
224
224
  );
225
225
  if (c.length === 0) return t;
226
- const a = /* @__PURE__ */ new Map(), l = [];
227
- for (const p of c) {
228
- const y = JSON.stringify(p.path);
229
- if (a.has(y)) {
226
+ const u = /* @__PURE__ */ new Map(), l = [];
227
+ for (const y of c) {
228
+ const p = JSON.stringify(y.path);
229
+ if (u.has(p)) {
230
230
  if (f === "error")
231
231
  throw new TypeError(
232
- `[rjsf-formulas] Formula conflict: two active fields share path [${tt(p.path)}]`
232
+ `[rjsf-formulas] Formula conflict: two active fields share path [${tt(y.path)}]`
233
233
  );
234
234
  f === "warn" && console.warn(
235
- `[rjsf-formulas] Formula conflict: two active fields share path [${tt(p.path)}]; taking last`
235
+ `[rjsf-formulas] Formula conflict: two active fields share path [${tt(y.path)}]; taking last`
236
236
  );
237
- const d = l.indexOf(a.get(y));
238
- l[d] = p, a.set(y, p);
237
+ const g = l.indexOf(u.get(p));
238
+ l[g] = y, u.set(p, y);
239
239
  } else
240
- a.set(y, p), l.push(p);
240
+ u.set(p, y), l.push(y);
241
241
  }
242
- const g = { formulaDataKey: o, formulaPathKey: i };
242
+ const m = { formulaDataKey: s, formulaPathKey: i };
243
243
  let j = t;
244
- for (let p = 0; p < s; p++) {
245
- const { data: y, errors: d } = await et(j, l, n, g);
246
- if (wt(j, y, l)) {
247
- for (const { path: I, error: _ } of d)
248
- r == null || r(I, _);
249
- return y;
244
+ for (let y = 0; y < o; y++) {
245
+ const { data: p, errors: g } = await et(j, l, r, m);
246
+ if (Ot(j, p, l)) {
247
+ for (const { path: A, error: _ } of g)
248
+ n == null || n(A, _);
249
+ return p;
250
250
  }
251
- j = y;
251
+ j = p;
252
252
  }
253
- const { data: x } = await et(j, l, n, g), w = structuredClone(x);
254
- for (const p of l)
255
- for (const y of D(p.path, x))
256
- Z(k(j, y), k(x, y)) || (r == null || r(
257
- y,
253
+ const { data: b } = await et(j, l, r, m), w = structuredClone(b);
254
+ for (const y of l)
255
+ for (const p of D(y.path, b))
256
+ Z(k(j, p), k(b, p)) || (n == null || n(
257
+ p,
258
258
  new Error(
259
- `[rjsf-formulas] Formula at [${y.join(", ")}] did not converge after ${s} passes`
259
+ `[rjsf-formulas] Formula at [${p.join(", ")}] did not converge after ${o} passes`
260
260
  )
261
- ), Q(w, y, void 0));
261
+ ), Q(w, p, void 0));
262
262
  return w;
263
263
  }
264
- function At(t) {
264
+ function xt(t) {
265
265
  return JSON.stringify(t);
266
266
  }
267
- function xt(t, e, n, s, r, o, i, u, f, c) {
268
- const [a, l] = it(t), g = m(t), j = m(t), x = m(/* @__PURE__ */ new Map()), w = m(null), p = m(!1), y = m(o), d = m(i), I = m(n), _ = m(e), T = m(r), R = m(u), M = m(f), $ = m(c);
267
+ function bt(t, e, r, o, n, s, i, a, f, c) {
268
+ const [u, l] = it(t), m = d(t), j = d(t), b = d(/* @__PURE__ */ new Map()), w = d(null), y = d(!1), p = d(s), g = d(i), A = d(r), _ = d(e), T = d(n), F = d(a), $ = d(f), S = d(c);
269
269
  h(() => {
270
- y.current = o;
271
- }, [o]), h(() => {
272
- d.current = i;
270
+ p.current = s;
271
+ }, [s]), h(() => {
272
+ g.current = i;
273
273
  }, [i]), h(() => {
274
- I.current = n;
275
- }, [n]), h(() => {
274
+ A.current = r;
275
+ }, [r]), h(() => {
276
276
  _.current = e;
277
277
  }, [e]), h(() => {
278
- T.current = r;
279
- }, [r]), h(() => {
280
- R.current = u;
281
- }, [u]), h(() => {
282
- M.current = f;
278
+ T.current = n;
279
+ }, [n]), h(() => {
280
+ F.current = a;
281
+ }, [a]), h(() => {
282
+ $.current = f;
283
283
  }, [f]), h(() => {
284
- $.current = c;
284
+ S.current = c;
285
285
  }, [c]);
286
- const J = G(() => {
287
- if (p.current) return;
286
+ const v = G(() => {
287
+ if (y.current) return;
288
288
  (async () => {
289
- var F, S;
290
- p.current = !0;
289
+ var M, N;
290
+ y.current = !0;
291
291
  try {
292
292
  for (; ; ) {
293
- const E = g.current, L = _.current, rt = L.filter(
294
- (b) => b.condition === !0 || M.current(b.condition, E)
293
+ const E = m.current, L = _.current, rt = L.filter(
294
+ (R) => R.condition === !0 || $.current(R.condition, E)
295
295
  ), q = [];
296
- for (const b of rt)
297
- for (const ot of D(b.path, E))
298
- q.push(ot);
299
- const K = x.current;
300
- K.clear();
301
- for (const b of q) K.set(At(b), "running");
302
- q.length > 0 && ((F = d.current) == null || F.call(d, q));
303
- const nt = await Ot(
296
+ for (const R of rt)
297
+ for (const st of D(R.path, E))
298
+ q.push(st);
299
+ const U = b.current;
300
+ U.clear();
301
+ for (const R of q) U.set(xt(R), "running");
302
+ q.length > 0 && ((M = g.current) == null || M.call(g, q));
303
+ const nt = await At(
304
304
  E,
305
305
  L,
306
- I.current,
306
+ A.current,
307
307
  T.current,
308
- y.current,
309
- R.current.formulaDataKey,
310
- R.current.formulaPathKey,
311
- M.current,
312
- $.current
308
+ p.current,
309
+ F.current.formulaDataKey,
310
+ F.current.formulaPathKey,
311
+ $.current,
312
+ S.current
313
313
  );
314
- if (z.current) break;
315
- const st = [...K.values()].some((b) => b === "dirty");
316
- if (K.clear(), !st) {
317
- l(nt), q.length > 0 && ((S = d.current) == null || S.call(d, []));
314
+ if (I.current) break;
315
+ const ot = [...U.values()].some((R) => R === "dirty");
316
+ if (U.clear(), !ot) {
317
+ l(nt), q.length > 0 && ((N = g.current) == null || N.call(g, []));
318
318
  break;
319
319
  }
320
320
  }
321
321
  } finally {
322
- p.current = !1;
322
+ y.current = !1;
323
323
  }
324
324
  })();
325
- }, []), O = G(
325
+ }, []), K = G(
326
326
  (B) => {
327
- g.current = B;
328
- const F = x.current;
329
- for (const [S, E] of F)
330
- E === "running" && F.set(S, "dirty");
331
- p.current || (w.current !== null && clearTimeout(w.current), w.current = setTimeout(() => {
332
- w.current = null, J();
333
- }, s));
327
+ m.current = B;
328
+ const M = b.current;
329
+ for (const [N, E] of M)
330
+ E === "running" && M.set(N, "dirty");
331
+ y.current || (w.current !== null && clearTimeout(w.current), w.current = setTimeout(() => {
332
+ w.current = null, v();
333
+ }, o));
334
334
  },
335
- [s, J]
336
- ), v = m(!1), z = m(!1);
335
+ [o, v]
336
+ ), O = d(!1), I = d(!1);
337
337
  return h(() => {
338
- v.current ? Z(t, j.current) || (j.current = t, O(t)) : (v.current = !0, j.current = t, O(t));
339
- }), h(() => (z.current = !1, () => {
340
- z.current = !0, v.current = !1, w.current !== null && clearTimeout(w.current);
341
- }), []), { enrichedFormData: a, handleInput: O };
338
+ O.current ? Z(t, j.current) || (j.current = t, K(t)) : (O.current = !0, j.current = t, K(t));
339
+ }), h(() => (I.current = !1, () => {
340
+ I.current = !0, O.current = !1, w.current !== null && clearTimeout(w.current);
341
+ }), []), { enrichedFormData: u, handleInput: K };
342
342
  }
343
- function bt(t, e) {
344
- const n = structuredClone(t ?? {}), s = Rt(e);
345
- for (const r of e)
346
- P(n, r.path, s);
347
- return n;
343
+ function Rt(t, e) {
344
+ const r = structuredClone(t ?? {}), o = $t(e);
345
+ for (const n of e)
346
+ P(r, n.path, o);
347
+ return r;
348
348
  }
349
- function Rt(t) {
349
+ function $t(t) {
350
350
  const e = /* @__PURE__ */ new Set();
351
- for (let n = 0; n < t.length; n++) {
352
- const s = t[n].path;
353
- for (let r = 0; r < s.length; r++) {
354
- const o = s[r];
355
- if (typeof o != "number" && o !== N) continue;
356
- const i = W(s.slice(0, r)), u = typeof o == "number";
357
- t.some((c, a) => {
358
- if (a === n || c.path.length <= r || W(c.path.slice(0, r)) !== i) return !1;
359
- const l = c.path[r];
360
- return u ? l === N : typeof l == "number";
351
+ for (let r = 0; r < t.length; r++) {
352
+ const o = t[r].path;
353
+ for (let n = 0; n < o.length; n++) {
354
+ const s = o[n];
355
+ if (typeof s != "number" && s !== J) continue;
356
+ const i = W(o.slice(0, n)), a = typeof s == "number";
357
+ t.some((c, u) => {
358
+ if (u === r || c.path.length <= n || W(c.path.slice(0, n)) !== i) return !1;
359
+ const l = c.path[n];
360
+ return a ? l === J : typeof l == "number";
361
361
  }) && e.add(i);
362
362
  }
363
363
  }
@@ -366,100 +366,102 @@ function Rt(t) {
366
366
  function W(t) {
367
367
  return JSON.stringify(t.map((e) => typeof e == "symbol" ? "__ARRAY_INDEX__" : e));
368
368
  }
369
- function P(t, e, n, s = []) {
369
+ function P(t, e, r, o = []) {
370
370
  if (e.length === 0) {
371
371
  t["ui:readonly"] = !0;
372
372
  return;
373
373
  }
374
- const [r, ...o] = e;
375
- if (typeof r == "string") {
376
- (!t[r] || typeof t[r] != "object" || Array.isArray(t[r])) && (t[r] = {}), P(t[r], o, n, [...s, r]);
374
+ const [n, ...s] = e;
375
+ if (typeof n == "string") {
376
+ (!t[n] || typeof t[n] != "object" || Array.isArray(t[n])) && (t[n] = {}), P(t[n], s, r, [...o, n]);
377
377
  return;
378
378
  }
379
- if (typeof r == "number") {
379
+ if (typeof n == "number") {
380
380
  if (t.items !== void 0 && !Array.isArray(t.items)) {
381
381
  console.warn("[rjsf-formulas] mergeReadOnly: cannot inject ui:readonly at tuple index because existing uiSchema has items as an object. Skipping.");
382
382
  return;
383
383
  }
384
384
  Array.isArray(t.items) || (t.items = []);
385
385
  const f = t.items;
386
- (!f[r] || typeof f[r] != "object") && (f[r] = {}), P(f[r], o, n, [...s, r]);
386
+ (!f[n] || typeof f[n] != "object") && (f[n] = {}), P(f[n], s, r, [...o, n]);
387
387
  return;
388
388
  }
389
- const i = W(s);
390
- if (n.has(i))
391
- (!t.additionalItems || typeof t.additionalItems != "object") && (t.additionalItems = {}), P(t.additionalItems, o, n, [...s, r]);
389
+ const i = W(o);
390
+ if (r.has(i))
391
+ (!t.additionalItems || typeof t.additionalItems != "object") && (t.additionalItems = {}), P(t.additionalItems, s, r, [...o, n]);
392
392
  else {
393
393
  if (Array.isArray(t.items)) {
394
394
  console.warn("[rjsf-formulas] mergeReadOnly: cannot inject ui:readonly for uniform array because existing uiSchema has items as an array. Skipping.");
395
395
  return;
396
396
  }
397
- (!t.items || typeof t.items != "object") && (t.items = {}), P(t.items, o, n, [...s, r]);
397
+ (!t.items || typeof t.items != "object") && (t.items = {}), P(t.items, s, r, [...o, n]);
398
398
  }
399
399
  }
400
- function kt(t) {
400
+ function vt(t, e) {
401
401
  const {
402
- schema: e,
403
- formData: n,
404
- validator: s,
405
- uiSchema: r,
406
- evaluator: o,
407
- Form: i = ut,
408
- formulaKey: u = "x-formula",
409
- formulaContextKey: f = "x-formula-context",
410
- formulaDataKey: c = "__formData__",
411
- formulaPathKey: a = "__path__",
412
- maxConvergencePasses: l = 10,
413
- debounceMs: g = 300,
414
- onFormulaError: j,
415
- onLoadingChange: x,
416
- formulaConflictBehavior: w = "warn",
402
+ schema: r,
403
+ formData: o,
404
+ validator: n,
405
+ uiSchema: s,
406
+ evaluator: i,
407
+ Form: a = at,
408
+ formulaKey: f = "x-formula",
409
+ formulaContextKey: c = "x-formula-context",
410
+ formulaDataKey: u = "__formData__",
411
+ formulaPathKey: l = "__path__",
412
+ maxConvergencePasses: m = 10,
413
+ debounceMs: j = 300,
414
+ onFormulaError: b,
415
+ onLoadingChange: w,
416
+ formulaConflictBehavior: y = "warn",
417
417
  onChange: p,
418
- ...y
419
- } = t, d = X(
420
- () => at(e, { formulaKey: u, formulaContextKey: f }),
421
- [e, u, f]
422
- ), I = X(
423
- () => ({ formulaDataKey: c, formulaPathKey: a }),
424
- [c, a]
418
+ ...g
419
+ } = t, A = X(
420
+ () => lt(r, { formulaKey: f, formulaContextKey: c }),
421
+ [r, f, c]
425
422
  ), _ = X(
426
- () => bt(r, d),
427
- [r, d]
428
- ), T = G(
429
- (O, v) => s.isValid(O, v, e),
430
- [s, e]
431
- ), { enrichedFormData: R, handleInput: M } = xt(
432
- n,
433
- d,
423
+ () => ({ formulaDataKey: u, formulaPathKey: l }),
424
+ [u, l]
425
+ ), T = X(
426
+ () => Rt(s, A),
427
+ [s, A]
428
+ ), F = G(
429
+ (O, I) => n.isValid(O, I, r),
430
+ [n, r]
431
+ ), { enrichedFormData: $, handleInput: S } = bt(
434
432
  o,
435
- g,
436
- l,
433
+ A,
434
+ i,
437
435
  j,
438
- x,
439
- I,
440
- T,
441
- w
442
- ), $ = m(p);
436
+ m,
437
+ b,
438
+ w,
439
+ _,
440
+ F,
441
+ y
442
+ ), v = d(p);
443
443
  return h(() => {
444
- $.current = p;
444
+ v.current = p;
445
445
  }, [p]), h(() => {
446
446
  var O;
447
- d.length !== 0 && ((O = $.current) == null || O.call($, { formData: R }, void 0));
448
- }, [R]), /* @__PURE__ */ ct(
449
- i,
447
+ A.length !== 0 && ((O = v.current) == null || O.call(v, { formData: $ }, void 0));
448
+ }, [$]), /* @__PURE__ */ ct(
449
+ a,
450
450
  {
451
- ...y,
452
- schema: e,
453
- validator: s,
454
- formData: R,
455
- uiSchema: _,
456
- onChange: (O, v) => {
457
- M(O.formData), p == null || p(O, v);
451
+ ref: e,
452
+ ...g,
453
+ schema: r,
454
+ validator: n,
455
+ formData: $,
456
+ uiSchema: T,
457
+ onChange: (O, I) => {
458
+ S(O.formData), p == null || p(O, I);
458
459
  }
459
460
  }
460
461
  );
461
462
  }
463
+ const Ft = ut(vt);
462
464
  export {
463
- kt as FormulaForm,
464
- at as analyzeSchema
465
+ Ft as FormulaForm,
466
+ lt as analyzeSchema
465
467
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a-ludi/rjsf-formulas",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "RJSF extension for computed fields driven by formulas",
5
5
  "author": "Arne Ludwig <arne.ludwig@posteo.de>",
6
6
  "license": "MIT",