@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 +1 -1
- package/dist/index.d.cts +1 -37
- package/dist/index.d.mts +1 -37
- package/dist/index.mjs +250 -247
- package/package.json +1 -1
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
|
|
4
|
-
import
|
|
5
|
-
const
|
|
6
|
-
function
|
|
7
|
-
const
|
|
8
|
-
return
|
|
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
|
|
13
|
-
return Array.isArray(
|
|
12
|
+
const r = t;
|
|
13
|
+
return Array.isArray(r.allOf) ? { allOf: [...r.allOf, e] } : { allOf: [t, e] };
|
|
14
14
|
}
|
|
15
|
-
function
|
|
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 [${
|
|
17
|
+
`[rjsf-formulas] Unknown x-formula-context value "${t}" at path [${z(e)}], treating as "siblings"`
|
|
18
18
|
), "siblings");
|
|
19
19
|
}
|
|
20
|
-
function
|
|
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,
|
|
26
|
+
function pt(t, e, r) {
|
|
27
27
|
const s = H(t.path);
|
|
28
|
-
if (
|
|
28
|
+
if (r === "error")
|
|
29
29
|
throw new TypeError(
|
|
30
|
-
`[rjsf-formulas] Formula conflict at path [${
|
|
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
|
-
|
|
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
|
|
36
|
+
function dt(t, e, r) {
|
|
37
37
|
for (const s of e) {
|
|
38
|
-
const
|
|
39
|
-
o !== -1 ? (pt(t[o], 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
|
|
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
|
-
|
|
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 [${
|
|
53
|
+
`[rjsf-formulas] Formula at path [${z(e)}] is not a string (got ${typeof c}), skipping.`
|
|
54
54
|
);
|
|
55
55
|
return;
|
|
56
56
|
}
|
|
57
|
-
|
|
57
|
+
r.push({
|
|
58
58
|
path: e,
|
|
59
59
|
formula: c,
|
|
60
|
-
contextMode:
|
|
60
|
+
contextMode: yt(f[n], e),
|
|
61
61
|
condition: i
|
|
62
62
|
});
|
|
63
63
|
return;
|
|
64
64
|
}
|
|
65
|
-
|
|
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
|
-
|
|
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 (
|
|
78
|
-
|
|
79
|
-
}), t.items && !Array.isArray(t.items) &&
|
|
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,
|
|
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
|
|
92
|
-
|
|
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
|
|
99
|
-
const
|
|
100
|
-
|
|
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,
|
|
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 [${
|
|
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
|
|
109
|
-
|
|
109
|
+
const m = Y(i, c);
|
|
110
|
+
x(a, e, r, s, n, o, m, u);
|
|
110
111
|
}
|
|
111
|
-
if (
|
|
112
|
-
const
|
|
113
|
-
|
|
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
|
|
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
|
|
122
|
-
return C || (C = 1, V = function t(e,
|
|
123
|
-
if (e ===
|
|
124
|
-
if (e &&
|
|
125
|
-
if (e.constructor !==
|
|
126
|
-
var s,
|
|
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 !=
|
|
129
|
-
for (
|
|
130
|
-
if (!t(e[
|
|
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 ===
|
|
134
|
-
if (e.valueOf !== Object.prototype.valueOf) return e.valueOf() ===
|
|
135
|
-
if (e.toString !== Object.prototype.toString) return e.toString() ===
|
|
136
|
-
if (o = Object.keys(e), s = o.length, s !== Object.keys(
|
|
137
|
-
for (
|
|
138
|
-
if (!Object.prototype.hasOwnProperty.call(
|
|
139
|
-
for (
|
|
140
|
-
var i = o[
|
|
141
|
-
if (!t(e[i],
|
|
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 &&
|
|
146
|
+
return e !== e && r !== r;
|
|
146
147
|
}), V;
|
|
147
148
|
}
|
|
148
|
-
var
|
|
149
|
-
const
|
|
150
|
-
function
|
|
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
|
-
},
|
|
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]:
|
|
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((
|
|
168
|
-
if (!(
|
|
169
|
-
return
|
|
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,
|
|
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]] =
|
|
176
|
+
s != null && typeof s == "object" && (s[e[e.length - 1]] = r);
|
|
176
177
|
}
|
|
177
|
-
function D(t, e,
|
|
178
|
-
if (t.length === 0) return [
|
|
179
|
-
const [s, ...
|
|
180
|
-
if (s ===
|
|
181
|
-
const o = k(e,
|
|
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(
|
|
186
|
+
i.push(...D(n, e, [...r, u]));
|
|
186
187
|
return i;
|
|
187
188
|
}
|
|
188
|
-
return D(
|
|
189
|
+
return D(n, e, [...r, s]);
|
|
189
190
|
}
|
|
190
191
|
function Z(t, e) {
|
|
191
|
-
return
|
|
192
|
+
return jt(t, e);
|
|
192
193
|
}
|
|
193
|
-
async function et(t, e,
|
|
194
|
-
const
|
|
194
|
+
async function et(t, e, r, s) {
|
|
195
|
+
const n = structuredClone(t), o = [];
|
|
195
196
|
for (const f of e)
|
|
196
|
-
for (const
|
|
197
|
-
const
|
|
198
|
-
o.push({ field: f, concretePath:
|
|
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:
|
|
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:
|
|
205
|
-
if (
|
|
206
|
-
Q(
|
|
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
|
|
209
|
-
u.push({ path:
|
|
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:
|
|
213
|
+
return { data: n, errors: u };
|
|
213
214
|
}
|
|
214
|
-
function
|
|
215
|
-
for (const s of
|
|
216
|
-
for (const
|
|
217
|
-
if (!Z(k(t,
|
|
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,
|
|
222
|
-
const
|
|
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 (
|
|
226
|
-
const
|
|
227
|
-
for (const p of
|
|
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 (
|
|
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
|
|
238
|
-
|
|
238
|
+
const h = a.indexOf(c.get(y));
|
|
239
|
+
a[h] = p, c.set(y, p);
|
|
239
240
|
} else
|
|
240
|
-
|
|
241
|
+
c.set(y, p), a.push(p);
|
|
241
242
|
}
|
|
242
|
-
const
|
|
243
|
-
let
|
|
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:
|
|
246
|
-
if (
|
|
247
|
-
for (const { path:
|
|
248
|
-
|
|
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
|
-
|
|
252
|
+
m = y;
|
|
252
253
|
}
|
|
253
|
-
const { data:
|
|
254
|
-
for (const p of
|
|
255
|
-
for (const y of D(p.path,
|
|
256
|
-
Z(k(
|
|
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
|
|
265
|
+
function xt(t) {
|
|
265
266
|
return JSON.stringify(t);
|
|
266
267
|
}
|
|
267
|
-
function
|
|
268
|
-
const [
|
|
269
|
-
|
|
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]),
|
|
272
|
-
|
|
273
|
-
}, [i]),
|
|
274
|
-
|
|
275
|
-
}, [
|
|
272
|
+
}, [o]), j(() => {
|
|
273
|
+
h.current = i;
|
|
274
|
+
}, [i]), j(() => {
|
|
275
|
+
O.current = r;
|
|
276
|
+
}, [r]), j(() => {
|
|
276
277
|
_.current = e;
|
|
277
|
-
}, [e]),
|
|
278
|
-
T.current =
|
|
279
|
-
}, [
|
|
280
|
-
|
|
281
|
-
}, [u]),
|
|
282
|
-
|
|
283
|
-
}, [f]),
|
|
284
|
-
|
|
285
|
-
}, [
|
|
286
|
-
const
|
|
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
|
|
290
|
+
var M, N;
|
|
290
291
|
p.current = !0;
|
|
291
292
|
try {
|
|
292
293
|
for (; ; ) {
|
|
293
|
-
const E =
|
|
294
|
-
(
|
|
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
|
|
297
|
-
for (const ot of D(
|
|
297
|
+
for (const R of rt)
|
|
298
|
+
for (const ot of D(R.path, E))
|
|
298
299
|
q.push(ot);
|
|
299
|
-
const
|
|
300
|
-
|
|
301
|
-
for (const
|
|
302
|
-
q.length > 0 && ((
|
|
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
|
-
|
|
307
|
+
O.current,
|
|
307
308
|
T.current,
|
|
308
309
|
y.current,
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
310
|
+
F.current.formulaDataKey,
|
|
311
|
+
F.current.formulaPathKey,
|
|
312
|
+
$.current,
|
|
313
|
+
S.current
|
|
313
314
|
);
|
|
314
|
-
if (
|
|
315
|
-
const st = [...
|
|
316
|
-
if (
|
|
317
|
-
|
|
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
|
-
}, []),
|
|
326
|
+
}, []), K = G(
|
|
326
327
|
(B) => {
|
|
327
|
-
|
|
328
|
-
const
|
|
329
|
-
for (const [
|
|
330
|
-
E === "running" &&
|
|
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,
|
|
333
|
+
w.current = null, v();
|
|
333
334
|
}, s));
|
|
334
335
|
},
|
|
335
|
-
[s,
|
|
336
|
-
),
|
|
337
|
-
return
|
|
338
|
-
|
|
339
|
-
}),
|
|
340
|
-
|
|
341
|
-
}), []), { enrichedFormData:
|
|
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
|
|
344
|
-
const
|
|
345
|
-
for (const
|
|
346
|
-
P(
|
|
347
|
-
return
|
|
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
|
|
350
|
+
function $t(t) {
|
|
350
351
|
const e = /* @__PURE__ */ new Set();
|
|
351
|
-
for (let
|
|
352
|
-
const s = t[
|
|
353
|
-
for (let
|
|
354
|
-
const o = s[
|
|
355
|
-
if (typeof o != "number" && o !==
|
|
356
|
-
const i = W(s.slice(0,
|
|
357
|
-
t.some((
|
|
358
|
-
if (
|
|
359
|
-
const
|
|
360
|
-
return u ?
|
|
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,
|
|
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 [
|
|
375
|
-
if (typeof
|
|
376
|
-
(!t[
|
|
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
|
|
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[
|
|
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 (
|
|
391
|
-
(!t.additionalItems || typeof t.additionalItems != "object") && (t.additionalItems = {}), P(t.additionalItems, o,
|
|
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,
|
|
398
|
+
(!t.items || typeof t.items != "object") && (t.items = {}), P(t.items, o, r, [...s, n]);
|
|
398
399
|
}
|
|
399
400
|
}
|
|
400
|
-
function
|
|
401
|
+
function vt(t, e) {
|
|
401
402
|
const {
|
|
402
|
-
schema:
|
|
403
|
-
formData:
|
|
404
|
-
validator:
|
|
405
|
-
uiSchema:
|
|
406
|
-
evaluator:
|
|
407
|
-
Form:
|
|
408
|
-
formulaKey:
|
|
409
|
-
formulaContextKey:
|
|
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:
|
|
413
|
-
debounceMs:
|
|
414
|
-
onFormulaError:
|
|
415
|
-
onLoadingChange:
|
|
416
|
-
formulaConflictBehavior:
|
|
417
|
-
onChange:
|
|
418
|
-
...
|
|
419
|
-
} = t,
|
|
420
|
-
() =>
|
|
421
|
-
[
|
|
422
|
-
),
|
|
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
|
-
),
|
|
426
|
-
() =>
|
|
427
|
-
[
|
|
428
|
-
),
|
|
429
|
-
(
|
|
430
|
-
[
|
|
431
|
-
), { enrichedFormData:
|
|
432
|
-
|
|
433
|
-
|
|
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
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
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
|
-
|
|
464
|
-
|
|
466
|
+
Ft as FormulaForm,
|
|
467
|
+
lt as analyzeSchema
|
|
465
468
|
};
|