@a-ludi/rjsf-formulas 0.2.0 → 0.3.1

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 A(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 A(e,t,r,s,n,o,u,a){const f=e;if(typeof f.$ref=="string"){const c=se.findSchemaDefinition(f.$ref,a);A(c,t,r,s,n,o,u,a);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}const y=e.type==="array"||e.type==null&&(Array.isArray(f.prefixItems)||e.items!=null&&!Array.isArray(e.items));if(e.properties&&!y)for(const[c,l]of Object.entries(e.properties))A(l,[...t,c],r,s,n,o,u,a);if(y&&(Array.isArray(f.prefixItems)&&f.prefixItems.forEach((l,m)=>{A(l,[...t,m],r,s,n,o,u,a)}),e.items&&!Array.isArray(e.items)&&A(e.items,[...t,U],r,s,n,o,u,a)),Array.isArray(f.allOf)){const c=f.allOf;for(const l of c){const m=[];A(l,t,m,s,n,o,u,a),ie(r,m,o)}}for(const c of["oneOf","anyOf"])if(Array.isArray(f[c])){const l=f[c];for(const m of l){const g=J(u,m);A(m,t,r,s,n,o,g,a)}}if("if"in f){const c=f.if,l=f.then,m=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.`),l!==void 0){const g=J(u,c);A(l,t,r,s,n,o,g,a)}if(m!==void 0){const g=J(u,{not:c});A(m,t,r,s,n,o,g,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,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((a,f)=>{if(!(a==null||typeof a!="object"))return a[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 a=0;a<o.length;a++)u.push(...k(n,t,[...r,a]));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 y of k(f.path,n)){const c=pe(f,y,n,s);o.push({field:f,concretePath:y,context:c})}const u=await Promise.allSettled(o.map(({field:f,context:y})=>new Promise(c=>c(r(f.formula,y))))),a=[];for(let f=0;f<o.length;f++){const{concretePath:y}=o[f],c=u[f];if(c.status==="fulfilled")V(n,y,c.value);else{const l=c.reason instanceof Error?c.reason:new Error(String(c.reason));a.push({path:y,error:l}),V(n,y,void 0)}}return{data:n,errors:a}}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,a,f){const y=t.filter(p=>p.condition===!0||a(p.condition,e));if(y.length===0)return e;const c=new Map,l=[];for(const p of y){const d=JSON.stringify(p.path);if(c.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 h=l.indexOf(c.get(d));l[h]=p,c.set(d,p)}else c.set(d,p),l.push(p)}const m={formulaDataKey:o,formulaPathKey:u};let g=e;for(let p=0;p<s;p++){const{data:d,errors:h}=await L(g,l,r,m);if(me(g,d,l)){for(const{path:w,error:$}of h)n==null||n(w,$);return d}g=d}const{data:O}=await L(g,l,r,m),j=structuredClone(O);for(const p of l)for(const d of k(p.path,O))H(I(g,d),I(O,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,a,f,y){const[c,l]=i.useState(e),m=i.useRef(e),g=i.useRef(e),O=i.useRef(new Map),j=i.useRef(null),p=i.useRef(!1),d=i.useRef(o),h=i.useRef(u),w=i.useRef(r),$=i.useRef(t),S=i.useRef(n),F=i.useRef(a),x=i.useRef(f),T=i.useRef(y);i.useEffect(()=>{d.current=o},[o]),i.useEffect(()=>{h.current=u},[u]),i.useEffect(()=>{w.current=r},[r]),i.useEffect(()=>{$.current=t},[t]),i.useEffect(()=>{S.current=n},[n]),i.useEffect(()=>{F.current=a},[a]),i.useEffect(()=>{x.current=f},[f]),i.useEffect(()=>{T.current=y},[y]);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=O.current;N.clear();for(const b of q)N.set(he(b),"running");q.length>0&&((_=h.current)==null||_.call(h,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){l(te),q.length>0&&((K=h.current)==null||K.call(h,[]));break}}}finally{p.current=!1}})()},[]),M=i.useCallback(Q=>{m.current=Q;const _=O.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,g.current)||(g.current=e,M(e)):(R.current=!0,g.current=e,M(e))}),i.useEffect(()=>(v.current=!1,()=>{v.current=!0,R.current=!1,j.current!==null&&clearTimeout(j.current)}),[]),{enrichedFormData:c,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)),a=typeof o=="number";e.some((y,c)=>{if(c===r||y.path.length<=n||G(y.path.slice(0,n))!==u)return!1;const l=y.path[n];return a?l===U:typeof l=="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 Ae(e,t){const{schema:r,formData:s,validator:n,uiSchema:o,evaluator:u,Form:a=fe,formulaKey:f="x-formula",formulaContextKey:y="x-formula-context",formulaDataKey:c="__formData__",formulaPathKey:l="__path__",maxConvergencePasses:m=10,debounceMs:g=300,onFormulaError:O,onLoadingChange:j,formulaConflictBehavior:p="warn",onChange:d,...h}=e,w=i.useMemo(()=>C(r,{formulaKey:f,formulaContextKey:y}),[r,f,y]),$=i.useMemo(()=>({formulaDataKey:c,formulaPathKey:l}),[c,l]),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,g,m,O,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(a,{ref:t,...h,schema:r,validator:n,formData:x,uiSchema:S,onChange:M})}const Oe=i.forwardRef(Ae);exports.FormulaForm=Oe;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,162 +1,163 @@
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 g, useEffect as j, 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", s = (e == null ? void 0 : e.formulaContextKey) ?? "x-formula-context", n = (e == null ? void 0 : e.formulaConflictBehavior) ?? "warn", o = [];
8
+ return x(t, [], o, r, s, n, !0, t), o;
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 yt(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) {
26
+ function pt(t, e, r) {
27
27
  const s = H(t.path);
28
- if (n === "error")
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(
32
+ r === "warn" && console.warn(
33
33
  `[rjsf-formulas] Formula conflict at path [${s}]: "${t.formula}" (earlier allOf branch) is overridden by "${e.formula}" (later allOf branch).`
34
34
  );
35
35
  }
36
- function yt(t, e, n) {
36
+ function dt(t, e, r) {
37
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);
38
+ const n = H(s.path), o = t.findIndex((i) => H(i.path) === n);
39
+ o !== -1 ? (pt(t[o], s, r), t[o] = s) : t.push(s);
40
40
  }
41
41
  }
42
- function A(t, e, n, s, r, o, i, u) {
42
+ function x(t, e, r, s, n, o, i, u) {
43
43
  const f = t;
44
44
  if (typeof f.$ref == "string") {
45
45
  const c = ft(f.$ref, u);
46
- A(c, e, n, s, r, o, i, u);
46
+ x(c, e, r, s, n, o, i, u);
47
47
  return;
48
48
  }
49
49
  if (s in t) {
50
50
  const c = f[s];
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: yt(f[n], e),
61
61
  condition: i
62
62
  });
63
63
  return;
64
64
  }
65
- if (t.properties && t.type !== "array")
65
+ const l = t.type === "array" || t.type == null && (Array.isArray(f.prefixItems) || t.items != null && !Array.isArray(t.items));
66
+ if (t.properties && !l)
66
67
  for (const [c, a] of Object.entries(t.properties))
67
- A(
68
+ x(
68
69
  a,
69
70
  [...e, c],
70
- n,
71
- s,
72
71
  r,
72
+ s,
73
+ n,
73
74
  o,
74
75
  i,
75
76
  u
76
77
  );
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(
78
+ if (l && (Array.isArray(f.prefixItems) && f.prefixItems.forEach((a, d) => {
79
+ x(a, [...e, d], r, s, n, o, i, u);
80
+ }), t.items && !Array.isArray(t.items) && x(
80
81
  t.items,
81
- [...e, N],
82
- n,
83
- s,
82
+ [...e, J],
84
83
  r,
84
+ s,
85
+ n,
85
86
  o,
86
87
  i,
87
88
  u
88
89
  )), Array.isArray(f.allOf)) {
89
90
  const c = f.allOf;
90
91
  for (const a of c) {
91
- const l = [];
92
- A(a, e, l, s, r, o, i, u), yt(n, l, o);
92
+ const d = [];
93
+ x(a, e, d, s, n, o, i, u), dt(r, d, o);
93
94
  }
94
95
  }
95
96
  for (const c of ["oneOf", "anyOf"])
96
97
  if (Array.isArray(f[c])) {
97
98
  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);
99
+ for (const d of a) {
100
+ const m = Y(i, d);
101
+ x(d, e, r, s, n, o, m, u);
101
102
  }
102
103
  }
103
104
  if ("if" in f) {
104
- const c = f.if, a = f.then, l = f.else;
105
+ const c = f.if, a = f.then, d = f.else;
105
106
  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
+ `[rjsf-formulas] Formula key "${s}" 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
108
  ), a !== void 0) {
108
- const g = Y(i, c);
109
- A(a, e, n, s, r, o, g, u);
109
+ const m = Y(i, c);
110
+ x(a, e, r, s, n, o, m, u);
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
+ if (d !== void 0) {
113
+ const m = Y(i, { not: c });
114
+ x(d, e, r, s, n, o, m, u);
114
115
  }
115
116
  }
116
117
  }
117
- function dt(t) {
118
+ function mt(t) {
118
119
  return t && t.__esModule && Object.prototype.hasOwnProperty.call(t, "default") ? t.default : t;
119
120
  }
120
121
  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;
122
+ function gt() {
123
+ return C || (C = 1, V = function t(e, r) {
124
+ if (e === r) return !0;
125
+ if (e && r && typeof e == "object" && typeof r == "object") {
126
+ if (e.constructor !== r.constructor) return !1;
127
+ var s, n, o;
127
128
  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;
129
+ if (s = e.length, s != r.length) return !1;
130
+ for (n = s; n-- !== 0; )
131
+ if (!t(e[n], r[n])) return !1;
131
132
  return !0;
132
133
  }
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;
134
+ if (e.constructor === RegExp) return e.source === r.source && e.flags === r.flags;
135
+ if (e.valueOf !== Object.prototype.valueOf) return e.valueOf() === r.valueOf();
136
+ if (e.toString !== Object.prototype.toString) return e.toString() === r.toString();
137
+ if (o = Object.keys(e), s = o.length, s !== Object.keys(r).length) return !1;
138
+ for (n = s; n-- !== 0; )
139
+ if (!Object.prototype.hasOwnProperty.call(r, o[n])) return !1;
140
+ for (n = s; n-- !== 0; ) {
141
+ var i = o[n];
142
+ if (!t(e[i], r[i])) return !1;
142
143
  }
143
144
  return !0;
144
145
  }
145
- return e !== e && n !== n;
146
+ return e !== e && r !== r;
146
147
  }), V;
147
148
  }
148
- var gt = mt();
149
- const ht = /* @__PURE__ */ dt(gt);
150
- function jt(t, e, n, s) {
149
+ var ht = gt();
150
+ const jt = /* @__PURE__ */ mt(ht);
151
+ function wt(t, e, r, s) {
151
152
  const o = e.slice(0, -1).reduce((u, f) => {
152
153
  if (!(u == null || typeof u != "object"))
153
154
  return u[f];
154
- }, n), i = o != null && typeof o == "object" && !Array.isArray(o) ? { ...o } : {};
155
+ }, r), i = o != null && typeof o == "object" && !Array.isArray(o) ? { ...o } : {};
155
156
  return t.contextMode === "extended" ? ((s.formulaDataKey in i || s.formulaPathKey in i) && console.warn(
156
157
  `[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.`
157
158
  ), {
158
159
  ...i,
159
- [s.formulaDataKey]: n,
160
+ [s.formulaDataKey]: r,
160
161
  [s.formulaPathKey]: [...e]
161
162
  }) : i;
162
163
  }
@@ -164,69 +165,69 @@ function tt(t) {
164
165
  return t.map((e) => typeof e == "symbol" ? "[*]" : String(e)).join(", ");
165
166
  }
166
167
  function k(t, e) {
167
- return e.reduce((n, s) => {
168
- if (!(n == null || typeof n != "object"))
169
- return n[s];
168
+ return e.reduce((r, s) => {
169
+ if (!(r == null || typeof r != "object"))
170
+ return r[s];
170
171
  }, t);
171
172
  }
172
- function Q(t, e, n) {
173
+ function Q(t, e, r) {
173
174
  if (e.length === 0) return;
174
175
  const s = k(t, e.slice(0, -1));
175
- s != null && typeof s == "object" && (s[e[e.length - 1]] = n);
176
+ s != null && typeof s == "object" && (s[e[e.length - 1]] = r);
176
177
  }
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);
178
+ function D(t, e, r = []) {
179
+ if (t.length === 0) return [r];
180
+ const [s, ...n] = t;
181
+ if (s === J) {
182
+ const o = k(e, r);
182
183
  if (!Array.isArray(o)) return [];
183
184
  const i = [];
184
185
  for (let u = 0; u < o.length; u++)
185
- i.push(...D(r, e, [...n, u]));
186
+ i.push(...D(n, e, [...r, u]));
186
187
  return i;
187
188
  }
188
- return D(r, e, [...n, s]);
189
+ return D(n, e, [...r, s]);
189
190
  }
190
191
  function Z(t, e) {
191
- return ht(t, e);
192
+ return jt(t, e);
192
193
  }
193
- async function et(t, e, n, s) {
194
- const r = structuredClone(t), o = [];
194
+ async function et(t, e, r, s) {
195
+ const n = structuredClone(t), o = [];
195
196
  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 });
197
+ for (const l of D(f.path, n)) {
198
+ const c = wt(f, l, n, s);
199
+ o.push({ field: f, concretePath: l, context: c });
199
200
  }
200
201
  const i = await Promise.allSettled(
201
- o.map(({ field: f, context: c }) => new Promise((a) => a(n(f.formula, c))))
202
+ o.map(({ field: f, context: l }) => new Promise((c) => c(r(f.formula, l))))
202
203
  ), u = [];
203
204
  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);
205
+ const { concretePath: l } = o[f], c = i[f];
206
+ if (c.status === "fulfilled")
207
+ Q(n, l, c.value);
207
208
  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);
209
+ const a = c.reason instanceof Error ? c.reason : new Error(String(c.reason));
210
+ u.push({ path: l, error: a }), Q(n, l, void 0);
210
211
  }
211
212
  }
212
- return { data: r, errors: u };
213
+ return { data: n, errors: u };
213
214
  }
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)))
215
+ function At(t, e, r) {
216
+ for (const s of r)
217
+ for (const n of D(s.path, e))
218
+ if (!Z(k(t, n), k(e, n)))
218
219
  return !1;
219
220
  return !0;
220
221
  }
221
- async function Ot(t, e, n, s, r, o, i, u, f) {
222
- const c = e.filter(
222
+ async function Ot(t, e, r, s, n, o, i, u, f) {
223
+ const l = e.filter(
223
224
  (p) => p.condition === !0 || u(p.condition, t)
224
225
  );
225
- if (c.length === 0) return t;
226
- const a = /* @__PURE__ */ new Map(), l = [];
227
- for (const p of c) {
226
+ if (l.length === 0) return t;
227
+ const c = /* @__PURE__ */ new Map(), a = [];
228
+ for (const p of l) {
228
229
  const y = JSON.stringify(p.path);
229
- if (a.has(y)) {
230
+ if (c.has(y)) {
230
231
  if (f === "error")
231
232
  throw new TypeError(
232
233
  `[rjsf-formulas] Formula conflict: two active fields share path [${tt(p.path)}]`
@@ -234,26 +235,26 @@ async function Ot(t, e, n, s, r, o, i, u, f) {
234
235
  f === "warn" && console.warn(
235
236
  `[rjsf-formulas] Formula conflict: two active fields share path [${tt(p.path)}]; taking last`
236
237
  );
237
- const d = l.indexOf(a.get(y));
238
- l[d] = p, a.set(y, p);
238
+ const h = a.indexOf(c.get(y));
239
+ a[h] = p, c.set(y, p);
239
240
  } else
240
- a.set(y, p), l.push(p);
241
+ c.set(y, p), a.push(p);
241
242
  }
242
- const g = { formulaDataKey: o, formulaPathKey: i };
243
- let j = t;
243
+ const d = { formulaDataKey: o, formulaPathKey: i };
244
+ let m = t;
244
245
  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, _);
246
+ const { data: y, errors: h } = await et(m, a, r, d);
247
+ if (At(m, y, a)) {
248
+ for (const { path: O, error: _ } of h)
249
+ n == null || n(O, _);
249
250
  return y;
250
251
  }
251
- j = y;
252
+ m = y;
252
253
  }
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(
254
+ const { data: b } = await et(m, a, r, d), w = structuredClone(b);
255
+ for (const p of a)
256
+ for (const y of D(p.path, b))
257
+ Z(k(m, y), k(b, y)) || (n == null || n(
257
258
  y,
258
259
  new Error(
259
260
  `[rjsf-formulas] Formula at [${y.join(", ")}] did not converge after ${s} passes`
@@ -261,60 +262,60 @@ async function Ot(t, e, n, s, r, o, i, u, f) {
261
262
  ), Q(w, y, void 0));
262
263
  return w;
263
264
  }
264
- function At(t) {
265
+ function xt(t) {
265
266
  return JSON.stringify(t);
266
267
  }
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(() => {
268
+ function bt(t, e, r, s, n, o, i, u, f, l) {
269
+ const [c, a] = it(t), d = g(t), m = g(t), b = g(/* @__PURE__ */ new Map()), w = g(null), p = g(!1), y = g(o), h = g(i), O = g(r), _ = g(e), T = g(n), F = g(u), $ = g(f), S = g(l);
270
+ j(() => {
270
271
  y.current = o;
271
- }, [o]), h(() => {
272
- d.current = i;
273
- }, [i]), h(() => {
274
- I.current = n;
275
- }, [n]), h(() => {
272
+ }, [o]), j(() => {
273
+ h.current = i;
274
+ }, [i]), j(() => {
275
+ O.current = r;
276
+ }, [r]), j(() => {
276
277
  _.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(() => {
278
+ }, [e]), j(() => {
279
+ T.current = n;
280
+ }, [n]), j(() => {
281
+ F.current = u;
282
+ }, [u]), j(() => {
283
+ $.current = f;
284
+ }, [f]), j(() => {
285
+ S.current = l;
286
+ }, [l]);
287
+ const v = G(() => {
287
288
  if (p.current) return;
288
289
  (async () => {
289
- var F, S;
290
+ var M, N;
290
291
  p.current = !0;
291
292
  try {
292
293
  for (; ; ) {
293
- const E = g.current, L = _.current, rt = L.filter(
294
- (b) => b.condition === !0 || M.current(b.condition, E)
294
+ const E = d.current, L = _.current, rt = L.filter(
295
+ (R) => R.condition === !0 || $.current(R.condition, E)
295
296
  ), q = [];
296
- for (const b of rt)
297
- for (const ot of D(b.path, E))
297
+ for (const R of rt)
298
+ for (const ot of D(R.path, E))
298
299
  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));
300
+ const U = b.current;
301
+ U.clear();
302
+ for (const R of q) U.set(xt(R), "running");
303
+ q.length > 0 && ((M = h.current) == null || M.call(h, q));
303
304
  const nt = await Ot(
304
305
  E,
305
306
  L,
306
- I.current,
307
+ O.current,
307
308
  T.current,
308
309
  y.current,
309
- R.current.formulaDataKey,
310
- R.current.formulaPathKey,
311
- M.current,
312
- $.current
310
+ F.current.formulaDataKey,
311
+ F.current.formulaPathKey,
312
+ $.current,
313
+ S.current
313
314
  );
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, []));
315
+ if (I.current) break;
316
+ const st = [...U.values()].some((R) => R === "dirty");
317
+ if (U.clear(), !st) {
318
+ a(nt), q.length > 0 && ((N = h.current) == null || N.call(h, []));
318
319
  break;
319
320
  }
320
321
  }
@@ -322,42 +323,42 @@ function xt(t, e, n, s, r, o, i, u, f, c) {
322
323
  p.current = !1;
323
324
  }
324
325
  })();
325
- }, []), O = G(
326
+ }, []), K = G(
326
327
  (B) => {
327
- g.current = B;
328
- const F = x.current;
329
- for (const [S, E] of F)
330
- E === "running" && F.set(S, "dirty");
328
+ d.current = B;
329
+ const M = b.current;
330
+ for (const [N, E] of M)
331
+ E === "running" && M.set(N, "dirty");
331
332
  p.current || (w.current !== null && clearTimeout(w.current), w.current = setTimeout(() => {
332
- w.current = null, J();
333
+ w.current = null, v();
333
334
  }, s));
334
335
  },
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 };
336
+ [s, v]
337
+ ), A = g(!1), I = g(!1);
338
+ return j(() => {
339
+ A.current ? Z(t, m.current) || (m.current = t, K(t)) : (A.current = !0, m.current = t, K(t));
340
+ }), j(() => (I.current = !1, () => {
341
+ I.current = !0, A.current = !1, w.current !== null && clearTimeout(w.current);
342
+ }), []), { enrichedFormData: c, handleInput: K };
342
343
  }
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;
344
+ function Rt(t, e) {
345
+ const r = structuredClone(t ?? {}), s = $t(e);
346
+ for (const n of e)
347
+ P(r, n.path, s);
348
+ return r;
348
349
  }
349
- function Rt(t) {
350
+ function $t(t) {
350
351
  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";
352
+ for (let r = 0; r < t.length; r++) {
353
+ const s = t[r].path;
354
+ for (let n = 0; n < s.length; n++) {
355
+ const o = s[n];
356
+ if (typeof o != "number" && o !== J) continue;
357
+ const i = W(s.slice(0, n)), u = typeof o == "number";
358
+ t.some((l, c) => {
359
+ if (c === r || l.path.length <= n || W(l.path.slice(0, n)) !== i) return !1;
360
+ const a = l.path[n];
361
+ return u ? a === J : typeof a == "number";
361
362
  }) && e.add(i);
362
363
  }
363
364
  }
@@ -366,100 +367,102 @@ function Rt(t) {
366
367
  function W(t) {
367
368
  return JSON.stringify(t.map((e) => typeof e == "symbol" ? "__ARRAY_INDEX__" : e));
368
369
  }
369
- function P(t, e, n, s = []) {
370
+ function P(t, e, r, s = []) {
370
371
  if (e.length === 0) {
371
372
  t["ui:readonly"] = !0;
372
373
  return;
373
374
  }
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]);
375
+ const [n, ...o] = e;
376
+ if (typeof n == "string") {
377
+ (!t[n] || typeof t[n] != "object" || Array.isArray(t[n])) && (t[n] = {}), P(t[n], o, r, [...s, n]);
377
378
  return;
378
379
  }
379
- if (typeof r == "number") {
380
+ if (typeof n == "number") {
380
381
  if (t.items !== void 0 && !Array.isArray(t.items)) {
381
382
  console.warn("[rjsf-formulas] mergeReadOnly: cannot inject ui:readonly at tuple index because existing uiSchema has items as an object. Skipping.");
382
383
  return;
383
384
  }
384
385
  Array.isArray(t.items) || (t.items = []);
385
386
  const f = t.items;
386
- (!f[r] || typeof f[r] != "object") && (f[r] = {}), P(f[r], o, n, [...s, r]);
387
+ (!f[n] || typeof f[n] != "object") && (f[n] = {}), P(f[n], o, r, [...s, n]);
387
388
  return;
388
389
  }
389
390
  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]);
391
+ if (r.has(i))
392
+ (!t.additionalItems || typeof t.additionalItems != "object") && (t.additionalItems = {}), P(t.additionalItems, o, r, [...s, n]);
392
393
  else {
393
394
  if (Array.isArray(t.items)) {
394
395
  console.warn("[rjsf-formulas] mergeReadOnly: cannot inject ui:readonly for uniform array because existing uiSchema has items as an array. Skipping.");
395
396
  return;
396
397
  }
397
- (!t.items || typeof t.items != "object") && (t.items = {}), P(t.items, o, n, [...s, r]);
398
+ (!t.items || typeof t.items != "object") && (t.items = {}), P(t.items, o, r, [...s, n]);
398
399
  }
399
400
  }
400
- function kt(t) {
401
+ function vt(t, e) {
401
402
  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",
403
+ schema: r,
404
+ formData: s,
405
+ validator: n,
406
+ uiSchema: o,
407
+ evaluator: i,
408
+ Form: u = at,
409
+ formulaKey: f = "x-formula",
410
+ formulaContextKey: l = "x-formula-context",
410
411
  formulaDataKey: c = "__formData__",
411
412
  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(
413
+ maxConvergencePasses: d = 10,
414
+ debounceMs: m = 300,
415
+ onFormulaError: b,
416
+ onLoadingChange: w,
417
+ formulaConflictBehavior: p = "warn",
418
+ onChange: y,
419
+ ...h
420
+ } = t, O = X(
421
+ () => lt(r, { formulaKey: f, formulaContextKey: l }),
422
+ [r, f, l]
423
+ ), _ = X(
423
424
  () => ({ formulaDataKey: c, formulaPathKey: a }),
424
425
  [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(
432
- n,
433
- d,
434
- o,
435
- g,
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(
426
+ ), T = X(
427
+ () => Rt(o, O),
428
+ [o, O]
429
+ ), F = G(
430
+ (A, I) => n.isValid(A, I, r),
431
+ [n, r]
432
+ ), { enrichedFormData: $, handleInput: S } = bt(
433
+ s,
434
+ O,
449
435
  i,
436
+ m,
437
+ d,
438
+ b,
439
+ w,
440
+ _,
441
+ F,
442
+ p
443
+ ), v = g(y);
444
+ return j(() => {
445
+ v.current = y;
446
+ }, [y]), j(() => {
447
+ var A;
448
+ O.length !== 0 && ((A = v.current) == null || A.call(v, { formData: $ }, void 0));
449
+ }, [$]), /* @__PURE__ */ ct(
450
+ u,
450
451
  {
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);
452
+ ref: e,
453
+ ...h,
454
+ schema: r,
455
+ validator: n,
456
+ formData: $,
457
+ uiSchema: T,
458
+ onChange: (A, I) => {
459
+ S(A.formData), y == null || y(A, I);
458
460
  }
459
461
  }
460
462
  );
461
463
  }
464
+ const Ft = ut(vt);
462
465
  export {
463
- kt as FormulaForm,
464
- at as analyzeSchema
466
+ Ft as FormulaForm,
467
+ lt as analyzeSchema
465
468
  };
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.1",
4
4
  "description": "RJSF extension for computed fields driven by formulas",
5
5
  "author": "Arne Ludwig <arne.ludwig@posteo.de>",
6
6
  "license": "MIT",