@a-ludi/rjsf-formulas 0.1.3 → 0.2.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 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;
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;
package/dist/index.d.cts CHANGED
@@ -8,8 +8,11 @@ import { StrictRJSFSchema } from '@rjsf/utils';
8
8
  * Scans a JSON Schema and returns descriptors for every field that carries a formula key.
9
9
  *
10
10
  * @remarks
11
- * Traversal is depth-first. Schema composition operators (`$ref`, `oneOf`, `anyOf`, `allOf`)
12
- * are not supported and emit a `console.warn` when encountered.
11
+ * Traversal is depth-first. `allOf`, `$ref`, `oneOf`, and `anyOf` are all supported.
12
+ * `allOf` branches are merged with conflict detection. `oneOf`/`anyOf` branches are each
13
+ * recursed and fields are collected with the branch schema as the `condition`. Nested
14
+ * `oneOf`/`anyOf` conditions are composed via `{ allOf: [...] }`. `$ref` is resolved
15
+ * synchronously using `findSchemaDefinition` (external refs will throw).
13
16
  *
14
17
  * @param schema - The root RJSF schema to scan.
15
18
  * @param options - Optional key overrides for locating formulas in the schema.
@@ -40,6 +43,14 @@ export declare type AnalyzeSchemaOptions = {
40
43
  formulaKey?: string;
41
44
  /** Schema key that selects the context mode for a computed field. Defaults to `'x-formula-context'`. */
42
45
  formulaContextKey?: string;
46
+ /**
47
+ * What to do when multiple `allOf` branches define a formula for the same path.
48
+ *
49
+ * - `'ignore'`: silently take the last definition.
50
+ * - `'warn'` (default): emit `console.warn` and take the last definition.
51
+ * - `'error'`: throw a `TypeError` synchronously.
52
+ */
53
+ formulaConflictBehavior?: 'ignore' | 'warn' | 'error';
43
54
  };
44
55
 
45
56
  /* Excluded from this release type: ARRAY_INDEX */
@@ -67,6 +78,14 @@ export declare type FormulaField = {
67
78
  formula: string;
68
79
  /** Which values are available when evaluating this field's formula. */
69
80
  contextMode: ContextMode;
81
+ /**
82
+ * The schema condition under which this formula is active.
83
+ *
84
+ * - `true` — always active (fields from `properties`, `items`, `prefixItems`, or `allOf` branches).
85
+ * - `RJSFSchema` — a conditional schema (a `oneOf`/`anyOf` branch condition, possibly composed
86
+ * via `{ allOf: [...] }` for nested branches).
87
+ */
88
+ condition: RJSFSchema | true;
70
89
  };
71
90
 
72
91
  /**
@@ -170,6 +189,15 @@ export declare type FormulaFormProps<T = any, S extends StrictRJSFSchema = RJSFS
170
189
  * @param loadingPaths - Paths of all fields whose evaluations are in progress.
171
190
  */
172
191
  onLoadingChange?: (loadingPaths: (string | number)[][]) => void;
192
+ /**
193
+ * What to do when multiple branches of a composition operator (`allOf` at schema-analysis
194
+ * time, or simultaneously-active `oneOf`/`anyOf` branches at evaluation time) define a
195
+ * formula for the same field path.
196
+ * - `'ignore'` — silently take the last definition.
197
+ * - `'warn'` (default) — emit `console.warn` and take the last definition.
198
+ * - `'error'` — throw a `TypeError` synchronously (useful during development).
199
+ */
200
+ formulaConflictBehavior?: 'ignore' | 'warn' | 'error';
173
201
  };
174
202
 
175
203
  export { }
package/dist/index.d.mts CHANGED
@@ -8,8 +8,11 @@ import { StrictRJSFSchema } from '@rjsf/utils';
8
8
  * Scans a JSON Schema and returns descriptors for every field that carries a formula key.
9
9
  *
10
10
  * @remarks
11
- * Traversal is depth-first. Schema composition operators (`$ref`, `oneOf`, `anyOf`, `allOf`)
12
- * are not supported and emit a `console.warn` when encountered.
11
+ * Traversal is depth-first. `allOf`, `$ref`, `oneOf`, and `anyOf` are all supported.
12
+ * `allOf` branches are merged with conflict detection. `oneOf`/`anyOf` branches are each
13
+ * recursed and fields are collected with the branch schema as the `condition`. Nested
14
+ * `oneOf`/`anyOf` conditions are composed via `{ allOf: [...] }`. `$ref` is resolved
15
+ * synchronously using `findSchemaDefinition` (external refs will throw).
13
16
  *
14
17
  * @param schema - The root RJSF schema to scan.
15
18
  * @param options - Optional key overrides for locating formulas in the schema.
@@ -40,6 +43,14 @@ export declare type AnalyzeSchemaOptions = {
40
43
  formulaKey?: string;
41
44
  /** Schema key that selects the context mode for a computed field. Defaults to `'x-formula-context'`. */
42
45
  formulaContextKey?: string;
46
+ /**
47
+ * What to do when multiple `allOf` branches define a formula for the same path.
48
+ *
49
+ * - `'ignore'`: silently take the last definition.
50
+ * - `'warn'` (default): emit `console.warn` and take the last definition.
51
+ * - `'error'`: throw a `TypeError` synchronously.
52
+ */
53
+ formulaConflictBehavior?: 'ignore' | 'warn' | 'error';
43
54
  };
44
55
 
45
56
  /* Excluded from this release type: ARRAY_INDEX */
@@ -67,6 +78,14 @@ export declare type FormulaField = {
67
78
  formula: string;
68
79
  /** Which values are available when evaluating this field's formula. */
69
80
  contextMode: ContextMode;
81
+ /**
82
+ * The schema condition under which this formula is active.
83
+ *
84
+ * - `true` — always active (fields from `properties`, `items`, `prefixItems`, or `allOf` branches).
85
+ * - `RJSFSchema` — a conditional schema (a `oneOf`/`anyOf` branch condition, possibly composed
86
+ * via `{ allOf: [...] }` for nested branches).
87
+ */
88
+ condition: RJSFSchema | true;
70
89
  };
71
90
 
72
91
  /**
@@ -170,6 +189,15 @@ export declare type FormulaFormProps<T = any, S extends StrictRJSFSchema = RJSFS
170
189
  * @param loadingPaths - Paths of all fields whose evaluations are in progress.
171
190
  */
172
191
  onLoadingChange?: (loadingPaths: (string | number)[][]) => void;
192
+ /**
193
+ * What to do when multiple branches of a composition operator (`allOf` at schema-analysis
194
+ * time, or simultaneously-active `oneOf`/`anyOf` branches at evaluation time) define a
195
+ * formula for the same field path.
196
+ * - `'ignore'` — silently take the last definition.
197
+ * - `'warn'` (default) — emit `console.warn` and take the last definition.
198
+ * - `'error'` — throw a `TypeError` synchronously (useful during development).
199
+ */
200
+ formulaConflictBehavior?: 'ignore' | 'warn' | 'error';
173
201
  };
174
202
 
175
203
  export { }
package/dist/index.mjs CHANGED
@@ -1,13 +1,18 @@
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";
1
+ import { findSchemaDefinition as ft } from "@rjsf/utils";
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";
4
5
  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;
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;
8
9
  }
9
- const rt = ["$ref", "oneOf", "anyOf", "allOf"];
10
- function nt(t, e) {
10
+ function Y(t, e) {
11
+ if (t === !0) return e;
12
+ const n = t;
13
+ return Array.isArray(n.allOf) ? { allOf: [...n.allOf, e] } : { allOf: [t, e] };
14
+ }
15
+ function lt(t, e) {
11
16
  return t === void 0 || t === "siblings" ? "siblings" : t === "extended" ? "extended" : (console.warn(
12
17
  `[rjsf-formulas] Unknown x-formula-context value "${t}" at path [${U(e)}], treating as "siblings"`
13
18
  ), "siblings");
@@ -15,284 +20,360 @@ function nt(t, e) {
15
20
  function U(t) {
16
21
  return t.map((e) => typeof e == "symbol" ? "[*]" : String(e)).join(", ");
17
22
  }
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.`
23
+ function H(t) {
24
+ return t.map((e) => typeof e == "symbol" ? "[*]" : String(e)).join(".");
25
+ }
26
+ function pt(t, e, n) {
27
+ const s = H(t.path);
28
+ if (n === "error")
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'.`
22
31
  );
23
- if (o in t) {
24
- const s = t[o];
25
- if (typeof s != "string") {
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).`
34
+ );
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);
40
+ }
41
+ }
42
+ function A(t, e, n, s, r, o, i, u) {
43
+ const f = t;
44
+ if (typeof f.$ref == "string") {
45
+ const c = ft(f.$ref, u);
46
+ A(c, e, n, s, r, o, i, u);
47
+ return;
48
+ }
49
+ if (s in t) {
50
+ const c = f[s];
51
+ if (typeof c != "string") {
26
52
  console.warn(
27
- `[rjsf-formulas] Formula at path [${U(e)}] is not a string (got ${typeof s}), skipping.`
53
+ `[rjsf-formulas] Formula at path [${U(e)}] is not a string (got ${typeof c}), skipping.`
28
54
  );
29
55
  return;
30
56
  }
31
57
  n.push({
32
58
  path: e,
33
- formula: s,
34
- contextMode: nt(
35
- t[r],
36
- e
37
- )
59
+ formula: c,
60
+ contextMode: lt(f[r], e),
61
+ condition: i
38
62
  });
39
63
  return;
40
64
  }
41
- if (t.type === "object" && t.properties)
42
- for (const [s, c] of Object.entries(t.properties))
43
- P(
44
- c,
45
- [...e, s],
65
+ if (t.properties && t.type !== "array")
66
+ for (const [c, a] of Object.entries(t.properties))
67
+ A(
68
+ a,
69
+ [...e, c],
46
70
  n,
71
+ s,
72
+ r,
47
73
  o,
48
- r
74
+ i,
75
+ u
49
76
  );
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,
58
- o,
59
- r
60
- );
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(
80
+ t.items,
81
+ [...e, N],
82
+ n,
83
+ s,
84
+ r,
85
+ o,
86
+ i,
87
+ u
88
+ )), Array.isArray(f.allOf)) {
89
+ const c = f.allOf;
90
+ for (const a of c) {
91
+ const l = [];
92
+ A(a, e, l, s, r, o, i, u), yt(n, l, o);
93
+ }
94
+ }
95
+ for (const c of ["oneOf", "anyOf"])
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);
101
+ }
102
+ }
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);
110
+ }
111
+ if (l !== void 0) {
112
+ const g = Y(i, { not: c });
113
+ A(l, e, n, s, r, o, g, u);
114
+ }
61
115
  }
62
116
  }
63
- function ot(t) {
117
+ function dt(t) {
64
118
  return t && t.__esModule && Object.prototype.hasOwnProperty.call(t, "default") ? t.default : t;
65
119
  }
66
- var C, H;
67
- function st() {
68
- return H || (H = 1, C = function t(e, n) {
120
+ var V, C;
121
+ function mt() {
122
+ return C || (C = 1, V = function t(e, n) {
69
123
  if (e === n) return !0;
70
124
  if (e && n && typeof e == "object" && typeof n == "object") {
71
125
  if (e.constructor !== n.constructor) return !1;
72
- var o, r, s;
126
+ var s, r, o;
73
127
  if (Array.isArray(e)) {
74
- if (o = e.length, o != n.length) return !1;
75
- for (r = o; r-- !== 0; )
128
+ if (s = e.length, s != n.length) return !1;
129
+ for (r = s; r-- !== 0; )
76
130
  if (!t(e[r], n[r])) return !1;
77
131
  return !0;
78
132
  }
79
133
  if (e.constructor === RegExp) return e.source === n.source && e.flags === n.flags;
80
134
  if (e.valueOf !== Object.prototype.valueOf) return e.valueOf() === n.valueOf();
81
135
  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;
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;
88
142
  }
89
143
  return !0;
90
144
  }
91
145
  return e !== e && n !== n;
92
- }), C;
146
+ }), V;
93
147
  }
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.`
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.`
103
157
  ), {
104
- ...c,
105
- [o.formulaDataKey]: n,
106
- [o.formulaPathKey]: [...e]
107
- }) : c;
158
+ ...i,
159
+ [s.formulaDataKey]: n,
160
+ [s.formulaPathKey]: [...e]
161
+ }) : i;
108
162
  }
109
- function A(t, e) {
110
- return e.reduce((n, o) => {
163
+ function tt(t) {
164
+ return t.map((e) => typeof e == "symbol" ? "[*]" : String(e)).join(", ");
165
+ }
166
+ function k(t, e) {
167
+ return e.reduce((n, s) => {
111
168
  if (!(n == null || typeof n != "object"))
112
- return n[o];
169
+ return n[s];
113
170
  }, t);
114
171
  }
115
- function Y(t, e, n) {
172
+ function Q(t, e, n) {
116
173
  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);
174
+ const s = k(t, e.slice(0, -1));
175
+ s != null && typeof s == "object" && (s[e[e.length - 1]] = n);
119
176
  }
120
- function _(t, e, n = []) {
177
+ function D(t, e, n = []) {
121
178
  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 [];
126
- const c = [];
127
- for (let i = 0; i < s.length; i++)
128
- c.push(..._(r, e, [...n, i]));
129
- return c;
179
+ const [s, ...r] = t;
180
+ if (s === N) {
181
+ const o = k(e, n);
182
+ if (!Array.isArray(o)) return [];
183
+ const i = [];
184
+ for (let u = 0; u < o.length; u++)
185
+ i.push(...D(r, e, [...n, u]));
186
+ return i;
130
187
  }
131
- return _(r, e, [...n, o]);
188
+ return D(r, e, [...n, s]);
132
189
  }
133
- function J(t, e) {
134
- return ct(t, e);
190
+ function Z(t, e) {
191
+ return ht(t, e);
135
192
  }
136
- async function Q(t, e, n, o) {
137
- const r = structuredClone(t), s = [];
193
+ async function et(t, e, n, s) {
194
+ const r = structuredClone(t), o = [];
138
195
  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 });
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 });
142
199
  }
143
- const c = await Promise.allSettled(
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];
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];
148
205
  if (a.status === "fulfilled")
149
- Y(r, u, a.value);
206
+ Q(r, c, a.value);
150
207
  else {
151
208
  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);
209
+ u.push({ path: c, error: l }), Q(r, c, void 0);
153
210
  }
154
211
  }
155
- return { data: r, errors: i };
212
+ return { data: r, errors: u };
156
213
  }
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)))
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)))
161
218
  return !1;
162
219
  return !0;
163
220
  }
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);
221
+ async function Ot(t, e, n, s, r, o, i, u, f) {
222
+ const c = e.filter(
223
+ (p) => p.condition === !0 || u(p.condition, t)
224
+ );
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)) {
230
+ if (f === "error")
231
+ throw new TypeError(
232
+ `[rjsf-formulas] Formula conflict: two active fields share path [${tt(p.path)}]`
233
+ );
234
+ f === "warn" && console.warn(
235
+ `[rjsf-formulas] Formula conflict: two active fields share path [${tt(p.path)}]; taking last`
236
+ );
237
+ const d = l.indexOf(a.get(y));
238
+ l[d] = p, a.set(y, p);
239
+ } else
240
+ a.set(y, p), l.push(p);
241
+ }
242
+ const g = { formulaDataKey: o, formulaPathKey: i };
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, _);
173
249
  return y;
174
250
  }
175
- f = y;
251
+ j = y;
176
252
  }
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(
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(
181
257
  y,
182
258
  new Error(
183
- `[rjsf-formulas] Formula at [${y.join(", ")}] did not converge after ${o} passes`
259
+ `[rjsf-formulas] Formula at [${y.join(", ")}] did not converge after ${s} passes`
184
260
  )
185
- ), Y(a, y, void 0));
186
- return a;
261
+ ), Q(w, y, void 0));
262
+ return w;
187
263
  }
188
- function lt(t) {
264
+ function At(t) {
189
265
  return JSON.stringify(t);
190
266
  }
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);
193
- j(() => {
194
- x.current = s;
195
- }, [s]), j(() => {
196
- m.current = c;
197
- }, [c]), j(() => {
198
- v.current = n;
199
- }, [n]), j(() => {
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;
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);
269
+ h(() => {
270
+ y.current = o;
271
+ }, [o]), h(() => {
272
+ d.current = i;
273
+ }, [i]), h(() => {
274
+ I.current = n;
275
+ }, [n]), h(() => {
276
+ _.current = e;
277
+ }, [e]), h(() => {
278
+ T.current = r;
279
+ }, [r]), h(() => {
280
+ R.current = u;
281
+ }, [u]), h(() => {
282
+ M.current = f;
283
+ }, [f]), h(() => {
284
+ $.current = c;
285
+ }, [c]);
286
+ const J = G(() => {
287
+ if (p.current) return;
208
288
  (async () => {
209
- var R, E;
210
- d.current = !0;
289
+ var F, S;
290
+ p.current = !0;
211
291
  try {
212
292
  for (; ; ) {
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,
226
- x.current,
227
- w.current.formulaDataKey,
228
- w.current.formulaPathKey
293
+ const E = g.current, L = _.current, rt = L.filter(
294
+ (b) => b.condition === !0 || M.current(b.condition, E)
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(
304
+ E,
305
+ L,
306
+ I.current,
307
+ T.current,
308
+ y.current,
309
+ R.current.formulaDataKey,
310
+ R.current.formulaPathKey,
311
+ M.current,
312
+ $.current
229
313
  );
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, []));
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, []));
234
318
  break;
235
319
  }
236
320
  }
237
321
  } finally {
238
- d.current = !1;
322
+ p.current = !1;
239
323
  }
240
324
  })();
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));
250
- },
251
- [o, h]
252
- ), b = p(!1), q = p(!1);
253
- return j(() => {
254
- b.current ? J(t, l.current) || (l.current = t, k(t)) : (b.current = !0, l.current = t, k(t));
255
- }), j(
256
- () => () => {
257
- q.current = !0, g.current !== null && clearTimeout(g.current);
325
+ }, []), O = G(
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));
258
334
  },
259
- []
260
- ), { enrichedFormData: f, handleInput: k };
335
+ [s, J]
336
+ ), v = m(!1), z = m(!1);
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 };
261
342
  }
262
- function pt(t, e) {
263
- const n = structuredClone(t ?? {}), o = dt(e);
343
+ function bt(t, e) {
344
+ const n = structuredClone(t ?? {}), s = Rt(e);
264
345
  for (const r of e)
265
- K(n, r.path, o);
346
+ P(n, r.path, s);
266
347
  return n;
267
348
  }
268
- function dt(t) {
349
+ function Rt(t) {
269
350
  const e = /* @__PURE__ */ new Set();
270
351
  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);
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";
361
+ }) && e.add(i);
281
362
  }
282
363
  }
283
364
  return e;
284
365
  }
285
- function z(t) {
366
+ function W(t) {
286
367
  return JSON.stringify(t.map((e) => typeof e == "symbol" ? "__ARRAY_INDEX__" : e));
287
368
  }
288
- function K(t, e, n, o = []) {
369
+ function P(t, e, n, s = []) {
289
370
  if (e.length === 0) {
290
371
  t["ui:readonly"] = !0;
291
372
  return;
292
373
  }
293
- const [r, ...s] = e;
374
+ const [r, ...o] = e;
294
375
  if (typeof r == "string") {
295
- (!t[r] || typeof t[r] != "object" || Array.isArray(t[r])) && (t[r] = {}), K(t[r], s, n, [...o, r]);
376
+ (!t[r] || typeof t[r] != "object" || Array.isArray(t[r])) && (t[r] = {}), P(t[r], o, n, [...s, r]);
296
377
  return;
297
378
  }
298
379
  if (typeof r == "number") {
@@ -302,75 +383,83 @@ function K(t, e, n, o = []) {
302
383
  }
303
384
  Array.isArray(t.items) || (t.items = []);
304
385
  const f = t.items;
305
- (!f[r] || typeof f[r] != "object") && (f[r] = {}), K(f[r], s, n, [...o, r]);
386
+ (!f[r] || typeof f[r] != "object") && (f[r] = {}), P(f[r], o, n, [...s, r]);
306
387
  return;
307
388
  }
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]);
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]);
311
392
  else {
312
393
  if (Array.isArray(t.items)) {
313
394
  console.warn("[rjsf-formulas] mergeReadOnly: cannot inject ui:readonly for uniform array because existing uiSchema has items as an array. Skipping.");
314
395
  return;
315
396
  }
316
- (!t.items || typeof t.items != "object") && (t.items = {}), K(t.items, s, n, [...o, r]);
397
+ (!t.items || typeof t.items != "object") && (t.items = {}), P(t.items, o, n, [...s, r]);
317
398
  }
318
399
  }
319
- function bt(t) {
400
+ function kt(t) {
320
401
  const {
321
402
  schema: e,
322
403
  formData: n,
323
- uiSchema: o,
324
- evaluator: r,
325
- Form: s = tt,
326
- formulaKey: c = "x-formula",
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,
333
- onLoadingChange: g,
334
- onChange: d,
335
- ...x
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(
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",
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]
425
+ ), _ = 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(
346
432
  n,
347
- m,
348
- r,
349
- l,
350
- a,
351
- y,
433
+ d,
434
+ o,
352
435
  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,
436
+ l,
437
+ j,
438
+ x,
439
+ I,
440
+ T,
441
+ w
442
+ ), $ = m(p);
443
+ return h(() => {
444
+ $.current = p;
445
+ }, [p]), h(() => {
446
+ var O;
447
+ d.length !== 0 && ((O = $.current) == null || O.call($, { formData: R }, void 0));
448
+ }, [R]), /* @__PURE__ */ ct(
449
+ i,
362
450
  {
363
- ...x,
451
+ ...y,
364
452
  schema: e,
365
- formData: O,
366
- uiSchema: M,
367
- onChange: (b, q) => {
368
- w(b.formData), d == null || d(b, q);
453
+ validator: s,
454
+ formData: R,
455
+ uiSchema: _,
456
+ onChange: (O, v) => {
457
+ M(O.formData), p == null || p(O, v);
369
458
  }
370
459
  }
371
460
  );
372
461
  }
373
462
  export {
374
- bt as FormulaForm,
375
- et as analyzeSchema
463
+ kt as FormulaForm,
464
+ at as analyzeSchema
376
465
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a-ludi/rjsf-formulas",
3
- "version": "0.1.3",
3
+ "version": "0.2.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",