@harbor-design/proform 1.0.11 → 1.0.13

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";var I=Object.defineProperty;var C=(l,e,t)=>e in l?I(l,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):l[e]=t;var c=(l,e,t)=>(C(l,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("vue");class u{static typeChecker(e){return{}.toString.call(e)}static isArray(e){return this.typeChecker(e)==="[object Array]"}static isFunction(e){return this.typeChecker(e)==="[object Function]"}static isPromise(e){return e instanceof Promise}static isObject(e){return this.typeChecker(e)==="[object Object]"}static isAsyncFunction(e){return this.typeChecker(e)==="[object AsyncFunction]"}static isUndefined(e){return e===void 0}static isArrayEmpty(e){return(e==null?void 0:e.length)<1}static isObjectEmpty(e){return this.isArrayEmpty(Object.keys(e))}static isListSchema(e){return e.type==="list"}static isGroupSchema(e){return e.type==="group"}static isItemSchema(e){return this.isUndefined(e.type)||e.type==="item"}static isProcessInprogress(e){if(e===void 0)return!0;if(this.isObject(e)){if(e.setup&&this.isFunction(e.setup)&&e.props)return!1;if(this.isObjectEmpty(e))return!0;for(const t in e)if(e.hasOwnProperty(t)&&this.isProcessInprogress(e[t]))return!0}else if(this.isArray(e)){if(this.isArrayEmpty(e))return!0;for(const t of e)if(this.isProcessInprogress(t))return!0}return!1}}function E(l,...e){return e.forEach(t=>{for(let s in t)t.hasOwnProperty(s)&&(typeof t[s]=="object"&&t[s]!==null?(l[s]=l[s]||{},E(l[s],t[s])):l[s]=t[s])}),l}function S(l){const e=new WeakMap;function t(s){if(s===null||typeof s!="object")return s;if(s instanceof Date)return new Date(s);if(s instanceof RegExp)return new RegExp(s);if(s instanceof Map){const i=new Map;for(let[n,f]of s)i.set(t(n),t(f));return i}if(s instanceof Set){const i=new Set;for(let n of s)i.add(t(n));return i}if(e.has(s))return e.get(s);if(Array.isArray(s)){const i=[];e.set(s,i);for(let n=0;n<s.length;n++)i[n]=t(s[n]);return i}const r=Object.create(Object.getPrototypeOf(s));e.set(s,r);for(let i in s)s.hasOwnProperty(i)&&(r[i]=t(s[i]));return r}return t(l)}class O{constructor(e){c(this,"runtimeCore");this.formCustomization=e}cleanFallbackFields(e){return e!==null&&typeof e=="object"&&(delete e.__yiwwhl_async_field_fallback,Object.values(e).forEach(t=>{this.cleanFallbackFields(t)})),e}setup(e){return this.runtimeCore=e,this.formCustomization}submit(){return new Promise((e,t)=>{this.runtimeCore.formRef.value.validate(s=>s?t(s):e(this.cleanFallbackFields(o.toRaw(this.runtimeCore.processor.processedModel.value))))})}hydrate(e){this.runtimeCore.hydrateEffect.trackEffect(()=>{o.isRef(e)?o.watch(()=>e.value,()=>{E(this.runtimeCore.model.value,e.value)},{deep:!0,immediate:!0}):o.isReactive(e)?o.watch(()=>e,()=>{E(this.runtimeCore.model.value,e)},{deep:!0,immediate:!0}):E(this.runtimeCore.model.value,e)},{lazy:!0})}}class j{constructor(){c(this,"effects",new Set)}clearEffects(){this.effects.clear()}triggerEffects(){Array.from(this.effects).forEach(e=>e())}trackEffect(e,t={lazy:!1}){return!t.lazy&&e(),this.effects.add(e),()=>this.effects.delete(e)}}class R{constructor(e){c(this,"runtimeCore");c(this,"processedSchemas");c(this,"processedModel");c(this,"getRuntimeMeta");c(this,"stableSchemas",[]);c(this,"stableModel",{});c(this,"schemaPreset",V.schemaPreset);c(this,"componentPropsPreset",V.componentPropsPreset);c(this,"stableUpdaterProcessProgress");c(this,"stableUpdaterTimes",0);c(this,"schemaEffect",new j);c(this,"defaultValueEffect",new j);c(this,"defaultValueInprogressMap",new Map);c(this,"baseDefaultValueFunctionsLength");this.runtimeCore=e,this.processedSchemas=e.schemas,this.processedModel=e.model,this.getRuntimeMeta=e.getRuntimeMeta.bind(e),o.watch(()=>this.processedModel.value,()=>{this.schemaEffect.triggerEffects()},{deep:!0})}parse(e,t){e.forEach((s,r)=>{this.parseItem(s,r,t)})}initSchemas(e){return e.map(t=>{const s={};return t.children&&(s.children=this.initSchemas(t.children)),s})}countFunctionDefaultValues(e){let t=0,s=new Set;function r(i){if(!s.has(i)&&(Array.isArray(i)||i!==null&&typeof i=="object")){s.add(i);for(let n in i)i.hasOwnProperty(n)&&(n==="defaultValue"&&typeof i[n]=="function"&&!i[n].toString().includes("[native code]")&&t++,r(i[n]))}}return r(e),t}parseSchemas(e,t){u.isArrayEmpty(this.processedSchemas.value)&&(this.baseDefaultValueFunctionsLength=this.countFunctionDefaultValues(S(e)),this.processedSchemas.value=this.initSchemas(e)),this.parse(e,t)}parseStable(e){const t={};if(!u.isUndefined(e.stable))t[e.key]=this.parseStable(e.stable);else return e;return t}stableUpdater(e=[]){if(e.every(Boolean)){const t=o.toRaw(this.processedSchemas.value);!u.isProcessInprogress(t)&&u.isObjectEmpty(this.stableModel)&&(this.stableUpdaterProcessProgress||(this.stableUpdaterProcessProgress=Array.from({length:t.length}).fill(!1)),this.stableUpdaterProcessProgress[this.stableUpdaterTimes]=!0,this.stableUpdaterTimes++,this.modelProcessor(t))}}parseItem(e,t,s){const r=this,i=Array.from({length:Object.keys(e).filter(f=>f!=="children").length}).fill(!1);this.objectParser({data:e,index:t,updater:n});function n(f){const a=f.index,h=f.key,P=f.keyIndex;if(!f.stable)return;const v=r.parseStable(f.stable),y=s==null?void 0:s.index,g=s==null?void 0:s.key;let d=v;if(u.isProcessInprogress(d)||(i[P]=!0),s){let p=r.processedSchemas.value[y][g][a][h];p&&u.isObject(p)&&h!=="component"&&(d=Object.assign(p,d)),r.processedSchemas.value[y][g][a][h]=d,r.stableUpdater(i)}else{let p=r.processedSchemas.value[a][h];p&&u.isObject(p)&&(d=Object.assign(p,d)),r.processedSchemas.value[a][h]=d,r.stableUpdater(i)}}}objectParser(e){const t=e.data;Object.keys(t).forEach((r,i)=>{if(r==="children")this.parseSchemas(t[r],{...e,key:r,keyIndex:i});else{const n=f=>{e.updater({...e,key:r,keyIndex:i,stable:f})};if(u.isFunction(t[r]))r!=="defaultValue"?this.schemaEffect.trackEffect(()=>{if(r==="component"){const f=t[r](this.getRuntimeMeta());this.promiseFieldParser(f,n,!1)}else this.fieldParser(t[r],n)}):this.defaultValueEffect.trackEffect(()=>{const f=this.schemaEffect.trackEffect(()=>{/\{\s*model\s*\}/.test(t[r].toString())?this.fieldParser(t[r],a=>{if(!a)return n(a);this.defaultValueInprogressMap.set(t[r],a),!u.isProcessInprogress(a)&&this.defaultValueInprogressMap.size===this.baseDefaultValueFunctionsLength&&Array.from(this.defaultValueInprogressMap.values()).every(h=>!h.includes("undefined"))?(n(a),this.defaultValueEffect.clearEffects(),f()):n(a)}):this.fieldParser(t[r],a=>{this.defaultValueInprogressMap.set(t[r],a),!u.isProcessInprogress(a)&&this.defaultValueInprogressMap.size===this.baseDefaultValueFunctionsLength&&Array.from(this.defaultValueInprogressMap.values()).every(h=>!h.includes("undefined"))?(n(a),this.defaultValueEffect.clearEffects(),f()):n(a)})})});else if(r==="component"){const f=t[r];this.promiseFieldParser(f,n,!1)}else this.fieldParser(t[r],n)}})}promiseFieldParser(e,t,s){u.isPromise(e)?e.then(r=>{s&&u.isObject(r)?this.objectParser({data:r,updater:t}):t(r)}):s&&u.isObject(e)?this.objectParser({data:e,updater:t}):t(e)}fieldParser(e,t,s=!0){if(u.isFunction(e)){const r=e(this.getRuntimeMeta());this.promiseFieldParser(r,t,s)}else o.isRef(e)?o.watch(()=>e.value,()=>{u.isUndefined(e.value)||(s&&u.isObject(e.value)?this.objectParser({data:e.value,updater:t}):t(e.value))},{immediate:!0,deep:!0}):o.isReactive(e)?o.watch(()=>e,()=>{u.isUndefined(e)||(s&&u.isObject(e)?this.objectParser({data:e,updater:t}):t(e))},{immediate:!0,deep:!0}):s&&u.isObject(e)?this.objectParser({data:e,updater:t}):t(e)}modelProcessor(e){e.map(t=>this.createModel(t,this.processedModel.value)),u.isObjectEmpty(this.stableModel)&&this.stableUpdaterProcessProgress.every(Boolean)&&this.defaultValueEffect.effects.size===0&&(this.stableModel=S(this.processedModel.value),this.runtimeCore.hydrateEffect.triggerEffects(),this.runtimeCore.hydrateEffect.clearEffects())}createModel(e,t){u.isListSchema(e)&&(t[e.field]||(t[e.field]=[{}]),e.children.forEach(s=>{this.createModel(s,t[e.field][0])})),u.isGroupSchema(e)&&e.children.forEach(s=>{this.createModel(s,t)}),u.isItemSchema(e)&&(t[e.field]=e.defaultValue)}}function w(l){return typeof l=="function"||Object.prototype.toString.call(l)==="[object Object]"&&!o.isVNode(l)}class A{constructor(e){c(this,"schemas",o.ref([]));c(this,"model",o.ref({}));c(this,"processorBySchemaType",{item:this.runtimeItemProcessor.bind(this),group:this.runtimeGroupProcessor.bind(this),list:this.runtimeListProcessor.bind(this)});c(this,"formRef",o.ref(null));c(this,"hydrateEffect",new j);this.setup=e,this.processor=new R(this);const t=this.setup(this);this.processor.parseSchemas(t.schemas)}getRuntimeMeta(){return{model:o.toRaw(S(this.model.value))}}runtimeItemProcessor(e,t,s=this.model.value,r){var g;const i=r?`${r.field}.${t}.${e.field}`:e.field,n=o.toRaw(e.component);if(!n)return;const f=n.name,a=e.componentProps??{},h=V.placeholderPresetByComponentName;let P=e.placeholder;if(P||(P=`${h[f]??"请输入"}${e.label}`),e.required){e.rules||(e.rules=[]);const d=e.rules.findIndex(p=>!!p.required);d===-1?(g=e.rules)==null||g.push({required:!0,message:`${e.label}是必填项`}):e.rules.splice(d,1,{required:!0,message:`${e.label}是必填项`})}let y=e.show;return y===void 0&&(y=!0),y||delete s[e.field],o.createVNode(m.runtimeDoms.Item,null,{default(){return o.withDirectives(o.createVNode(m.runtimeDoms.FormItem,{label:`${e.label}:`,rules:e.rules,field:i},{default:()=>[o.createVNode(n,o.mergeProps({modelValue:s[e.field],"onUpdate:modelValue":d=>s[e.field]=d,placeholder:P},a),null)]}),[[o.vShow,y]])}})}runtimeGroupProcessor(e){let t;return o.createVNode(m.runtimeDoms.Group,{schema:e},w(t=e.children.map(s=>this.runtimeItemProcessor(s)))?t:{default:()=>[t]})}addListItem(e){var t,s;if(!((t=this.processor.stableModel[e.field])!=null&&t[0]))return Promise.reject({code:"0001",message:"异步默认值数据正在处理中,请您耐心等待... "});(s=this.processor.stableModel[e.field])!=null&&s[0]&&this.model.value[e.field].push(S(this.processor.stableModel[e.field][0]))}deleteListItem(e,t){this.model.value[e.field].splice(t,1);const s=e.children.map(({field:r})=>`${e.field}.${t}.${r}`);this.formRef.value.clearValidate(s)}runtimeListProcessor(e){const t=this;return t.model.value[e.field]||(t.model.value[e.field]=[{}]),o.createVNode(m.runtimeDoms.List,{schema:e},{default(){return t.model.value[e.field].map((s,r)=>o.createVNode(m.runtimeDoms.ListItem,null,{default(){return e.children.map(i=>t.runtimeItemProcessor(i,r,s,e))},delete({container:i}={}){var f;let n=i??o.createVNode("button",null,null);return o.withDirectives(o.createVNode(n,{onClick:()=>t.deleteListItem(e,r)},null),[[o.vShow,((f=t.model.value[e.field])==null?void 0:f.length)>1]])}}))},add({container:s}={}){let r=s??o.createVNode("button",null,[o.createTextVNode("添加")]);return o.createVNode(r,{onClick:()=>t.addListItem(e)},null)}})}runtimeProcessor(e){return e.map(t=>(t.type||(t.type="item"),this.processorBySchemaType[t.type](t)))}exec(){let e;return o.createVNode(m.runtimeDoms.Form,{ref:this.formRef,model:this.model.value},w(e=this.runtimeProcessor(this.schemas.value))?e:{default:()=>[e]})}}class m{}c(m,"runtimeDoms");const b=class b{static getPlaceholderPrefixPresetByComponentName(){const e={请选择:["Select","Tree","TreeSelect"],请输入:["Input"]},t={};for(let s in e)e[s].forEach(r=>{t[r]=s});return t}};c(b,"schemaPreset",{type:{defaultValue:"item"},component:{defaultValue:void 0},componentProps:{defaultValue:void 0},defaultValue:{defaultValue:void 0},label:{defaultValue:""},field:{defaultValue:"__yiwwhl_async_field_fallback"},rules:{defaultValue:[]},show:{defaultValue:!0},required:{defaultValue:!1},placeholder:{defaultValue:void 0},children:{defaultValue:[]}}),c(b,"componentPropsPreset",{options:{defaultValue:[]}}),c(b,"placeholderPresetByComponentName",b.getPlaceholderPrefixPresetByComponentName());let V=b;const D=o.defineComponent({props:{setup:{type:Function,required:!0}},setup(l){const e=new A(l.setup);return()=>e.exec()}});function M(l){const e=new O(l);return[e.setup.bind(e),{submit:e.submit.bind(e),hydrate:e.hydrate.bind(e)}]}function N(l){return{install(){m.runtimeDoms=l}}}exports.ProForm=D;exports.useForm=M;exports.useFormRenderer=N;
1
+ "use strict";var I=Object.defineProperty;var O=(n,e,t)=>e in n?I(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var c=(n,e,t)=>(O(n,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("vue");class a{static typeChecker(e){return{}.toString.call(e)}static isArray(e){return this.typeChecker(e)==="[object Array]"}static isFunction(e){return this.typeChecker(e)==="[object Function]"}static isPromise(e){return e instanceof Promise}static isObject(e){return this.typeChecker(e)==="[object Object]"}static isAsyncFunction(e){return this.typeChecker(e)==="[object AsyncFunction]"}static isUndefined(e){return e===void 0}static isArrayEmpty(e){return(e==null?void 0:e.length)<1}static isObjectEmpty(e){return this.isArrayEmpty(Object.keys(e))}static isListSchema(e){return e.type==="list"}static isGroupSchema(e){return e.type==="group"}static isItemSchema(e){return this.isUndefined(e.type)||e.type==="item"}static isProcessInprogress(e){if(e===void 0)return!0;if(this.isObject(e)){if(e.setup&&this.isFunction(e.setup)&&e.props)return!1;if(this.isObjectEmpty(e))return!0;for(const t in e)if(e.hasOwnProperty(t)&&this.isProcessInprogress(e[t]))return!0}else if(this.isArray(e)){if(this.isArrayEmpty(e))return!0;for(const t of e)if(this.isProcessInprogress(t))return!0}return!1}}function E(n,...e){return e.forEach(t=>{for(let s in t)t.hasOwnProperty(s)&&(typeof t[s]=="object"&&t[s]!==null?(n[s]=n[s]||{},E(n[s],t[s])):n[s]=t[s])}),n}function S(n){const e=new WeakMap;function t(s){if(s===null||typeof s!="object")return s;if(s instanceof Date)return new Date(s);if(s instanceof RegExp)return new RegExp(s);if(s instanceof Map){const i=new Map;for(let[l,f]of s)i.set(t(l),t(f));return i}if(s instanceof Set){const i=new Set;for(let l of s)i.add(t(l));return i}if(e.has(s))return e.get(s);if(Array.isArray(s)){const i=[];e.set(s,i);for(let l=0;l<s.length;l++)i[l]=t(s[l]);return i}const r=Object.create(Object.getPrototypeOf(s));e.set(s,r);for(let i in s)s.hasOwnProperty(i)&&(r[i]=t(s[i]));return r}return t(n)}class C{constructor(e){c(this,"runtimeCore");this.formCustomization=e}cleanFallbackFields(e){return e!==null&&typeof e=="object"&&(delete e.__yiwwhl_async_field_fallback,Object.values(e).forEach(t=>{this.cleanFallbackFields(t)})),e}setup(e){return this.runtimeCore=e,this.formCustomization}submit(){return new Promise((e,t)=>{this.runtimeCore.formRef.value.validate(s=>s?t(s):e(this.cleanFallbackFields(o.toRaw(this.runtimeCore.processor.processedModel.value))))})}hydrate(e){this.runtimeCore.hydrateEffect.trackEffect(()=>{o.isRef(e)?o.watch(()=>e.value,()=>{E(this.runtimeCore.model.value,e.value)},{deep:!0,immediate:!0}):o.isReactive(e)?o.watch(()=>e,()=>{E(this.runtimeCore.model.value,e)},{deep:!0,immediate:!0}):E(this.runtimeCore.model.value,e)},{lazy:!0})}}class j{constructor(){c(this,"effects",new Set)}clearEffects(){this.effects.clear()}triggerEffects(){Array.from(this.effects).forEach(e=>e())}trackEffect(e,t={lazy:!1}){return!t.lazy&&e(),this.effects.add(e),()=>this.effects.delete(e)}}class M{constructor(e){c(this,"runtimeCore");c(this,"processedSchemas");c(this,"processedModel");c(this,"getRuntimeMeta");c(this,"stableSchemas",[]);c(this,"stableModel",{});c(this,"schemaPreset",V.schemaPreset);c(this,"componentPropsPreset",V.componentPropsPreset);c(this,"stableUpdaterProcessProgress");c(this,"stableUpdaterTimes",0);c(this,"schemaEffect",new j);c(this,"defaultValueEffect",new j);c(this,"defaultValueInprogressMap",new Map);c(this,"baseDefaultValueFunctionsLength");this.runtimeCore=e,this.processedSchemas=e.schemas,this.processedModel=e.model,this.getRuntimeMeta=e.getRuntimeMeta.bind(e),o.watch(()=>this.processedModel.value,()=>{this.schemaEffect.triggerEffects()},{deep:!0})}parse(e,t){e.forEach((s,r)=>{this.parseItem(s,r,t)})}initSchemas(e){return e.map(t=>{const s={};return t.children&&(s.children=this.initSchemas(t.children)),s})}countFunctionDefaultValues(e){let t=0,s=new Set;function r(i){if(!s.has(i)&&(Array.isArray(i)||i!==null&&typeof i=="object")){s.add(i);for(let l in i)i.hasOwnProperty(l)&&(l==="defaultValue"&&typeof i[l]=="function"&&!i[l].toString().includes("[native code]")&&t++,r(i[l]))}}return r(e),t}parseSchemas(e,t){a.isArrayEmpty(this.processedSchemas.value)&&(this.baseDefaultValueFunctionsLength=this.countFunctionDefaultValues(S(e)),this.processedSchemas.value=this.initSchemas(e)),this.parse(e,t)}parseStable(e){const t={};if(!a.isUndefined(e.stable))t[e.key]=this.parseStable(e.stable);else return e;return t}stableUpdater(e=[]){if(e.every(Boolean)){const t=o.toRaw(this.processedSchemas.value);!a.isProcessInprogress(t)&&a.isObjectEmpty(this.stableModel)&&(this.stableUpdaterProcessProgress||(this.stableUpdaterProcessProgress=Array.from({length:t.length}).fill(!1)),this.stableUpdaterProcessProgress[this.stableUpdaterTimes]=!0,this.stableUpdaterTimes++,this.modelProcessor(t))}}parseItem(e,t,s){const r=this,i=Array.from({length:Object.keys(e).filter(f=>f!=="children").length}).fill(!1);this.objectParser({data:e,index:t,updater:l});function l(f){const u=f.index,h=f.key,P=f.keyIndex;if(!f.stable)return;const w=r.parseStable(f.stable),b=s==null?void 0:s.index,g=s==null?void 0:s.key;let d=w;if(a.isProcessInprogress(d)||(i[P]=!0),s){let p=r.processedSchemas.value[b][g][u][h];p&&a.isObject(p)&&h!=="component"&&(d=Object.assign(p,d)),r.processedSchemas.value[b][g][u][h]=d,r.stableUpdater(i)}else{let p=r.processedSchemas.value[u][h];p&&a.isObject(p)&&(d=Object.assign(p,d)),r.processedSchemas.value[u][h]=d,r.stableUpdater(i)}}}objectParser(e){const t=e.data;Object.keys(t).forEach((r,i)=>{if(r==="children")this.parseSchemas(t[r],{...e,key:r,keyIndex:i});else{const l=f=>{e.updater({...e,key:r,keyIndex:i,stable:f})};if(a.isFunction(t[r]))r!=="defaultValue"?this.schemaEffect.trackEffect(()=>{if(r==="component"){const f=t[r](this.getRuntimeMeta());this.promiseFieldParser(f,l,!1)}else this.fieldParser(t[r],l)}):this.defaultValueEffect.trackEffect(()=>{const f=this.schemaEffect.trackEffect(()=>{/\{\s*model\s*\}/.test(t[r].toString())?this.fieldParser(t[r],u=>{if(!u)return l(u);this.defaultValueInprogressMap.set(t[r],u),!a.isProcessInprogress(u)&&this.defaultValueInprogressMap.size===this.baseDefaultValueFunctionsLength&&Array.from(this.defaultValueInprogressMap.values()).every(h=>!h.includes("undefined"))?(l(u),this.defaultValueEffect.clearEffects(),f()):l(u)}):this.fieldParser(t[r],u=>{this.defaultValueInprogressMap.set(t[r],u),!a.isProcessInprogress(u)&&this.defaultValueInprogressMap.size===this.baseDefaultValueFunctionsLength&&Array.from(this.defaultValueInprogressMap.values()).every(h=>!h.includes("undefined"))?(l(u),this.defaultValueEffect.clearEffects(),f()):l(u)})})});else if(r==="component"){const f=t[r];this.promiseFieldParser(f,l,!1)}else this.fieldParser(t[r],l)}})}promiseFieldParser(e,t,s){a.isPromise(e)?e.then(r=>{s&&a.isObject(r)?this.objectParser({data:r,updater:t}):t(r)}):s&&a.isObject(e)?this.objectParser({data:e,updater:t}):t(e)}fieldParser(e,t,s=!0){if(a.isFunction(e))if(e.name.startsWith("__proform_raw_"))t(e);else{const r=e(this.getRuntimeMeta());this.promiseFieldParser(r,t,s)}else o.isRef(e)?o.watch(()=>e.value,()=>{a.isUndefined(e.value)||(s&&a.isObject(e.value)?this.objectParser({data:e.value,updater:t}):t(e.value))},{immediate:!0,deep:!0}):o.isReactive(e)?o.watch(()=>e,()=>{a.isUndefined(e)||(s&&a.isObject(e)?this.objectParser({data:e,updater:t}):t(e))},{immediate:!0,deep:!0}):s&&a.isObject(e)?this.objectParser({data:e,updater:t}):t(e)}modelProcessor(e){e.map(t=>this.createModel(t,this.processedModel.value)),a.isObjectEmpty(this.stableModel)&&this.stableUpdaterProcessProgress.every(Boolean)&&this.defaultValueEffect.effects.size===0&&(this.stableModel=S(this.processedModel.value),this.runtimeCore.hydrateEffect.triggerEffects(),this.runtimeCore.hydrateEffect.clearEffects())}createModel(e,t){a.isListSchema(e)&&(t[e.field]||(t[e.field]=[{}]),e.children.forEach(s=>{this.createModel(s,t[e.field][0])})),a.isGroupSchema(e)&&e.children.forEach(s=>{this.createModel(s,t)}),a.isItemSchema(e)&&(t[e.field]=e.defaultValue)}}function v(n){return typeof n=="function"||Object.prototype.toString.call(n)==="[object Object]"&&!o.isVNode(n)}class R{constructor(e){c(this,"schemas",o.ref([]));c(this,"model",o.ref({}));c(this,"processorBySchemaType",{item:this.runtimeItemProcessor.bind(this),group:this.runtimeGroupProcessor.bind(this),list:this.runtimeListProcessor.bind(this)});c(this,"formRef",o.ref(null));c(this,"hydrateEffect",new j);this.setup=e,this.processor=new M(this);const t=this.setup(this);this.processor.parseSchemas(t.schemas)}getRuntimeMeta(){return{model:o.toRaw(S(this.model.value))}}runtimeItemProcessor(e,t,s=this.model.value,r){var g;const i=r?`${r.field}.${t}.${e.field}`:e.field,l=o.toRaw(e.component);if(!l)return;const f=l.name,u=e.componentProps??{},h=V.placeholderPresetByComponentName;let P=e.placeholder;if(P||(P=`${h[f]??"请输入"}${e.label}`),e.required)if(!e.rules)e.rules=[],(g=e.rules)==null||g.push({required:!0,message:`${e.label}是必填项`});else{const d=e.rules.findIndex(p=>!!p.required);e.rules[d].message=`${e.label}是必填项`}let b=e.show;return b===void 0&&(b=!0),b||delete s[e.field],o.createVNode(m.runtimeDoms.Item,null,{default(){return o.withDirectives(o.createVNode(m.runtimeDoms.FormItem,{label:`${e.label}:`,rules:e.rules,field:i},{default:()=>[o.createVNode(l,o.mergeProps({modelValue:s[e.field],"onUpdate:modelValue":d=>s[e.field]=d,placeholder:P},u),null)]}),[[o.vShow,b]])}})}runtimeGroupProcessor(e){let t;return o.createVNode(m.runtimeDoms.Group,{schema:e},v(t=e.children.map(s=>this.runtimeItemProcessor(s)))?t:{default:()=>[t]})}addListItem(e){var t,s;if(!((t=this.processor.stableModel[e.field])!=null&&t[0]))return Promise.reject({code:"0001",message:"异步默认值数据正在处理中,请您耐心等待... "});(s=this.processor.stableModel[e.field])!=null&&s[0]&&this.model.value[e.field].push(S(this.processor.stableModel[e.field][0]))}deleteListItem(e,t){this.model.value[e.field].splice(t,1);const s=e.children.map(({field:r})=>`${e.field}.${t}.${r}`);this.formRef.value.clearValidate(s)}runtimeListProcessor(e){const t=this;return t.model.value[e.field]||(t.model.value[e.field]=[{}]),o.createVNode(m.runtimeDoms.List,{schema:e},{default(){return t.model.value[e.field].map((s,r)=>o.createVNode(m.runtimeDoms.ListItem,null,{default(){return e.children.map(i=>t.runtimeItemProcessor(i,r,s,e))},delete({container:i}={}){var f;let l=i??o.createVNode("button",null,null);return o.withDirectives(o.createVNode(l,{onClick:()=>t.deleteListItem(e,r)},null),[[o.vShow,((f=t.model.value[e.field])==null?void 0:f.length)>1]])}}))},add({container:s}={}){let r=s??o.createVNode("button",null,[o.createTextVNode("添加")]);return o.createVNode(r,{onClick:()=>t.addListItem(e)},null)}})}runtimeProcessor(e){return e.map(t=>(t.type||(t.type="item"),this.processorBySchemaType[t.type](t)))}exec(){let e;return o.createVNode(m.runtimeDoms.Form,{ref:this.formRef,model:this.model.value},v(e=this.runtimeProcessor(this.schemas.value))?e:{default:()=>[e]})}}class m{}c(m,"runtimeDoms");const y=class y{static getPlaceholderPrefixPresetByComponentName(){const e={请选择:["Select","Tree","TreeSelect"],请输入:["Input"]},t={};for(let s in e)e[s].forEach(r=>{t[r]=s});return t}};c(y,"schemaPreset",{type:{defaultValue:"item"},component:{defaultValue:void 0},componentProps:{defaultValue:void 0},defaultValue:{defaultValue:void 0},label:{defaultValue:""},field:{defaultValue:"__yiwwhl_async_field_fallback"},rules:{defaultValue:[]},show:{defaultValue:!0},required:{defaultValue:!1},placeholder:{defaultValue:void 0},children:{defaultValue:[]}}),c(y,"componentPropsPreset",{options:{defaultValue:[]}}),c(y,"placeholderPresetByComponentName",y.getPlaceholderPrefixPresetByComponentName());let V=y;const A=o.defineComponent({props:{setup:{type:Function,required:!0}},setup(n){const e=new R(n.setup);return()=>e.exec()}});function D(n){const e=new C(n);return[e.setup.bind(e),{submit:e.submit.bind(e),hydrate:e.hydrate.bind(e)}]}function N(n){return{install(){m.runtimeDoms=n}}}function x(n,e){return e==="raw"&&Object.defineProperty(n,"name",{value:`__proform_raw_${n.name}`,writable:!0}),n}exports.ProForm=A;exports.useForm=D;exports.useFormRenderer=N;exports.useModifiers=x;
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- var R = Object.defineProperty;
2
- var U = (l, e, t) => e in l ? R(l, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[e] = t;
3
- var o = (l, e, t) => (U(l, typeof e != "symbol" ? e + "" : e, t), t);
4
- import { toRaw as V, isRef as D, watch as E, isReactive as M, ref as I, createVNode as h, withDirectives as O, mergeProps as k, vShow as A, createTextVNode as L, isVNode as F, defineComponent as z } from "vue";
1
+ var U = Object.defineProperty;
2
+ var _ = (n, e, t) => e in n ? U(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
+ var o = (n, e, t) => (_(n, typeof e != "symbol" ? e + "" : e, t), t);
4
+ import { toRaw as V, isRef as x, watch as E, isReactive as D, ref as I, createVNode as h, withDirectives as O, mergeProps as k, vShow as A, createTextVNode as R, isVNode as L, defineComponent as $ } from "vue";
5
5
  class f {
6
6
  static typeChecker(e) {
7
7
  return {}.toString.call(e);
@@ -60,13 +60,13 @@ class f {
60
60
  return !1;
61
61
  }
62
62
  }
63
- function S(l, ...e) {
63
+ function S(n, ...e) {
64
64
  return e.forEach((t) => {
65
65
  for (let s in t)
66
- t.hasOwnProperty(s) && (typeof t[s] == "object" && t[s] !== null ? (l[s] = l[s] || {}, S(l[s], t[s])) : l[s] = t[s]);
67
- }), l;
66
+ t.hasOwnProperty(s) && (typeof t[s] == "object" && t[s] !== null ? (n[s] = n[s] || {}, S(n[s], t[s])) : n[s] = t[s]);
67
+ }), n;
68
68
  }
69
- function v(l) {
69
+ function v(n) {
70
70
  const e = /* @__PURE__ */ new WeakMap();
71
71
  function t(s) {
72
72
  if (s === null || typeof s != "object")
@@ -77,14 +77,14 @@ function v(l) {
77
77
  return new RegExp(s);
78
78
  if (s instanceof Map) {
79
79
  const i = /* @__PURE__ */ new Map();
80
- for (let [n, c] of s)
81
- i.set(t(n), t(c));
80
+ for (let [l, c] of s)
81
+ i.set(t(l), t(c));
82
82
  return i;
83
83
  }
84
84
  if (s instanceof Set) {
85
85
  const i = /* @__PURE__ */ new Set();
86
- for (let n of s)
87
- i.add(t(n));
86
+ for (let l of s)
87
+ i.add(t(l));
88
88
  return i;
89
89
  }
90
90
  if (e.has(s))
@@ -92,8 +92,8 @@ function v(l) {
92
92
  if (Array.isArray(s)) {
93
93
  const i = [];
94
94
  e.set(s, i);
95
- for (let n = 0; n < s.length; n++)
96
- i[n] = t(s[n]);
95
+ for (let l = 0; l < s.length; l++)
96
+ i[l] = t(s[l]);
97
97
  return i;
98
98
  }
99
99
  const r = Object.create(Object.getPrototypeOf(s));
@@ -102,9 +102,9 @@ function v(l) {
102
102
  s.hasOwnProperty(i) && (r[i] = t(s[i]));
103
103
  return r;
104
104
  }
105
- return t(l);
105
+ return t(n);
106
106
  }
107
- class $ {
107
+ class z {
108
108
  constructor(e) {
109
109
  o(this, "runtimeCore");
110
110
  this.formCustomization = e;
@@ -130,7 +130,7 @@ class $ {
130
130
  hydrate(e) {
131
131
  this.runtimeCore.hydrateEffect.trackEffect(
132
132
  () => {
133
- D(e) ? E(
133
+ x(e) ? E(
134
134
  () => e.value,
135
135
  () => {
136
136
  S(this.runtimeCore.model.value, e.value);
@@ -139,7 +139,7 @@ class $ {
139
139
  deep: !0,
140
140
  immediate: !0
141
141
  }
142
- ) : M(e) ? E(
142
+ ) : D(e) ? E(
143
143
  () => e,
144
144
  () => {
145
145
  S(this.runtimeCore.model.value, e);
@@ -216,8 +216,8 @@ class B {
216
216
  function r(i) {
217
217
  if (!s.has(i) && (Array.isArray(i) || i !== null && typeof i == "object")) {
218
218
  s.add(i);
219
- for (let n in i)
220
- i.hasOwnProperty(n) && (n === "defaultValue" && typeof i[n] == "function" && !i[n].toString().includes("[native code]") && t++, r(i[n]));
219
+ for (let l in i)
220
+ i.hasOwnProperty(l) && (l === "defaultValue" && typeof i[l] == "function" && !i[l].toString().includes("[native code]") && t++, r(i[l]));
221
221
  }
222
222
  }
223
223
  return r(e), t;
@@ -250,16 +250,16 @@ class B {
250
250
  const r = this, i = Array.from({
251
251
  length: Object.keys(e).filter((c) => c !== "children").length
252
252
  }).fill(!1);
253
- this.objectParser({ data: e, index: t, updater: n });
254
- function n(c) {
253
+ this.objectParser({ data: e, index: t, updater: l });
254
+ function l(c) {
255
255
  const u = c.index, d = c.key, P = c.keyIndex;
256
256
  if (!c.stable)
257
257
  return;
258
- const C = r.parseStable(c.stable), y = s == null ? void 0 : s.index, g = s == null ? void 0 : s.key;
258
+ const C = r.parseStable(c.stable), b = s == null ? void 0 : s.index, g = s == null ? void 0 : s.key;
259
259
  let a = C;
260
260
  if (f.isProcessInprogress(a) || (i[P] = !0), s) {
261
- let p = r.processedSchemas.value[y][g][u][d];
262
- p && f.isObject(p) && d !== "component" && (a = Object.assign(p, a)), r.processedSchemas.value[y][g][u][d] = a, r.stableUpdater(i);
261
+ let p = r.processedSchemas.value[b][g][u][d];
262
+ p && f.isObject(p) && d !== "component" && (a = Object.assign(p, a)), r.processedSchemas.value[b][g][u][d] = a, r.stableUpdater(i);
263
263
  } else {
264
264
  let p = r.processedSchemas.value[u][d];
265
265
  p && f.isObject(p) && (a = Object.assign(p, a)), r.processedSchemas.value[u][d] = a, r.stableUpdater(i);
@@ -277,7 +277,7 @@ class B {
277
277
  keyIndex: i
278
278
  });
279
279
  else {
280
- const n = (c) => {
280
+ const l = (c) => {
281
281
  e.updater({
282
282
  ...e,
283
283
  key: r,
@@ -289,29 +289,29 @@ class B {
289
289
  r !== "defaultValue" ? this.schemaEffect.trackEffect(() => {
290
290
  if (r === "component") {
291
291
  const c = t[r](this.getRuntimeMeta());
292
- this.promiseFieldParser(c, n, !1);
292
+ this.promiseFieldParser(c, l, !1);
293
293
  } else
294
- this.fieldParser(t[r], n);
294
+ this.fieldParser(t[r], l);
295
295
  }) : this.defaultValueEffect.trackEffect(() => {
296
296
  const c = this.schemaEffect.trackEffect(() => {
297
297
  /\{\s*model\s*\}/.test(t[r].toString()) ? this.fieldParser(t[r], (u) => {
298
298
  if (!u)
299
- return n(u);
299
+ return l(u);
300
300
  this.defaultValueInprogressMap.set(t[r], u), !f.isProcessInprogress(u) && this.defaultValueInprogressMap.size === this.baseDefaultValueFunctionsLength && Array.from(this.defaultValueInprogressMap.values()).every(
301
301
  (d) => !d.includes("undefined")
302
- ) ? (n(u), this.defaultValueEffect.clearEffects(), c()) : n(u);
302
+ ) ? (l(u), this.defaultValueEffect.clearEffects(), c()) : l(u);
303
303
  }) : this.fieldParser(t[r], (u) => {
304
304
  this.defaultValueInprogressMap.set(t[r], u), !f.isProcessInprogress(u) && this.defaultValueInprogressMap.size === this.baseDefaultValueFunctionsLength && Array.from(this.defaultValueInprogressMap.values()).every(
305
305
  (d) => !d.includes("undefined")
306
- ) ? (n(u), this.defaultValueEffect.clearEffects(), c()) : n(u);
306
+ ) ? (l(u), this.defaultValueEffect.clearEffects(), c()) : l(u);
307
307
  });
308
308
  });
309
309
  });
310
310
  else if (r === "component") {
311
311
  const c = t[r];
312
- this.promiseFieldParser(c, n, !1);
312
+ this.promiseFieldParser(c, l, !1);
313
313
  } else
314
- this.fieldParser(t[r], n);
314
+ this.fieldParser(t[r], l);
315
315
  }
316
316
  });
317
317
  }
@@ -328,11 +328,15 @@ class B {
328
328
  }
329
329
  // 对任意对象中单个字段的 parse: 做基本处理
330
330
  fieldParser(e, t, s = !0) {
331
- if (f.isFunction(e)) {
332
- const r = e(this.getRuntimeMeta());
333
- this.promiseFieldParser(r, t, s);
334
- } else
335
- D(e) ? E(
331
+ if (f.isFunction(e))
332
+ if (e.name.startsWith("__proform_raw_"))
333
+ t(e);
334
+ else {
335
+ const r = e(this.getRuntimeMeta());
336
+ this.promiseFieldParser(r, t, s);
337
+ }
338
+ else
339
+ x(e) ? E(
336
340
  () => e.value,
337
341
  () => {
338
342
  f.isUndefined(e.value) || (s && f.isObject(e.value) ? this.objectParser({
@@ -344,7 +348,7 @@ class B {
344
348
  immediate: !0,
345
349
  deep: !0
346
350
  }
347
- ) : M(e) ? E(
351
+ ) : D(e) ? E(
348
352
  () => e,
349
353
  () => {
350
354
  f.isUndefined(e) || (s && f.isObject(e) ? this.objectParser({
@@ -374,10 +378,10 @@ class B {
374
378
  }), f.isItemSchema(e) && (t[e.field] = e.defaultValue);
375
379
  }
376
380
  }
377
- function x(l) {
378
- return typeof l == "function" || Object.prototype.toString.call(l) === "[object Object]" && !F(l);
381
+ function M(n) {
382
+ return typeof n == "function" || Object.prototype.toString.call(n) === "[object Object]" && !L(n);
379
383
  }
380
- class q {
384
+ class F {
381
385
  constructor(e) {
382
386
  o(this, "schemas", I([]));
383
387
  o(this, "model", I({}));
@@ -399,37 +403,36 @@ class q {
399
403
  }
400
404
  runtimeItemProcessor(e, t, s = this.model.value, r) {
401
405
  var g;
402
- const i = r ? `${r.field}.${t}.${e.field}` : e.field, n = V(e.component);
403
- if (!n)
406
+ const i = r ? `${r.field}.${t}.${e.field}` : e.field, l = V(e.component);
407
+ if (!l)
404
408
  return;
405
- const c = n.name, u = e.componentProps ?? {}, d = j.placeholderPresetByComponentName;
409
+ const c = l.name, u = e.componentProps ?? {}, d = j.placeholderPresetByComponentName;
406
410
  let P = e.placeholder;
407
411
  if (P || (P = `${// @ts-expect-error
408
- d[c] ?? "请输入"}${e.label}`), e.required) {
409
- e.rules || (e.rules = []);
410
- const a = e.rules.findIndex((p) => !!p.required);
411
- a === -1 ? (g = e.rules) == null || g.push({
412
- required: !0,
413
- message: `${e.label}是必填项`
414
- }) : e.rules.splice(a, 1, {
415
- required: !0,
416
- message: `${e.label}是必填项`
417
- });
418
- }
419
- let y = e.show;
420
- return y === void 0 && (y = !0), y || delete s[e.field], h(m.runtimeDoms.Item, null, {
412
+ d[c] ?? "请输入"}${e.label}`), e.required)
413
+ if (!e.rules)
414
+ e.rules = [], (g = e.rules) == null || g.push({
415
+ required: !0,
416
+ message: `${e.label}是必填项`
417
+ });
418
+ else {
419
+ const a = e.rules.findIndex((p) => !!p.required);
420
+ e.rules[a].message = `${e.label}是必填项`;
421
+ }
422
+ let b = e.show;
423
+ return b === void 0 && (b = !0), b || delete s[e.field], h(m.runtimeDoms.Item, null, {
421
424
  default() {
422
425
  return O(h(m.runtimeDoms.FormItem, {
423
426
  label: `${e.label}:`,
424
427
  rules: e.rules,
425
428
  field: i
426
429
  }, {
427
- default: () => [h(n, k({
430
+ default: () => [h(l, k({
428
431
  modelValue: s[e.field],
429
432
  "onUpdate:modelValue": (a) => s[e.field] = a,
430
433
  placeholder: P
431
434
  }, u), null)]
432
- }), [[A, y]]);
435
+ }), [[A, b]]);
433
436
  }
434
437
  });
435
438
  }
@@ -437,7 +440,7 @@ class q {
437
440
  let t;
438
441
  return h(m.runtimeDoms.Group, {
439
442
  schema: e
440
- }, x(t = e.children.map((s) => this.runtimeItemProcessor(s))) ? t : {
443
+ }, M(t = e.children.map((s) => this.runtimeItemProcessor(s))) ? t : {
441
444
  default: () => [t]
442
445
  });
443
446
  }
@@ -471,8 +474,8 @@ class q {
471
474
  container: i
472
475
  } = {}) {
473
476
  var c;
474
- let n = i ?? h("button", null, null);
475
- return O(h(n, {
477
+ let l = i ?? h("button", null, null);
478
+ return O(h(l, {
476
479
  onClick: () => t.deleteListItem(e, r)
477
480
  }, null), [[A, ((c = t.model.value[e.field]) == null ? void 0 : c.length) > 1]]);
478
481
  }
@@ -481,7 +484,7 @@ class q {
481
484
  add({
482
485
  container: s
483
486
  } = {}) {
484
- let r = s ?? h("button", null, [L("添加")]);
487
+ let r = s ?? h("button", null, [R("添加")]);
485
488
  return h(r, {
486
489
  onClick: () => t.addListItem(e)
487
490
  }, null);
@@ -496,7 +499,7 @@ class q {
496
499
  return h(m.runtimeDoms.Form, {
497
500
  ref: this.formRef,
498
501
  model: this.model.value
499
- }, x(e = this.runtimeProcessor(this.schemas.value)) ? e : {
502
+ }, M(e = this.runtimeProcessor(this.schemas.value)) ? e : {
500
503
  default: () => [e]
501
504
  });
502
505
  }
@@ -504,7 +507,7 @@ class q {
504
507
  class m {
505
508
  }
506
509
  o(m, "runtimeDoms");
507
- const b = class b {
510
+ const y = class y {
508
511
  static getPlaceholderPrefixPresetByComponentName() {
509
512
  const e = {
510
513
  请选择: ["Select", "Tree", "TreeSelect"],
@@ -517,7 +520,7 @@ const b = class b {
517
520
  return t;
518
521
  }
519
522
  };
520
- o(b, "schemaPreset", {
523
+ o(y, "schemaPreset", {
521
524
  type: {
522
525
  defaultValue: "item"
523
526
  },
@@ -551,27 +554,27 @@ o(b, "schemaPreset", {
551
554
  children: {
552
555
  defaultValue: []
553
556
  }
554
- }), o(b, "componentPropsPreset", {
557
+ }), o(y, "componentPropsPreset", {
555
558
  options: {
556
559
  defaultValue: []
557
560
  }
558
561
  }), // 基于基本功能提出基本预设
559
- o(b, "placeholderPresetByComponentName", b.getPlaceholderPrefixPresetByComponentName());
560
- let j = b;
561
- const _ = /* @__PURE__ */ z({
562
+ o(y, "placeholderPresetByComponentName", y.getPlaceholderPrefixPresetByComponentName());
563
+ let j = y;
564
+ const q = /* @__PURE__ */ $({
562
565
  props: {
563
566
  setup: {
564
567
  type: Function,
565
568
  required: !0
566
569
  }
567
570
  },
568
- setup(l) {
569
- const e = new q(l.setup);
571
+ setup(n) {
572
+ const e = new F(n.setup);
570
573
  return () => e.exec();
571
574
  }
572
575
  });
573
- function G(l) {
574
- const e = new $(l);
576
+ function G(n) {
577
+ const e = new z(n);
575
578
  return [
576
579
  e.setup.bind(e),
577
580
  {
@@ -580,15 +583,22 @@ function G(l) {
580
583
  }
581
584
  ];
582
585
  }
583
- function K(l) {
586
+ function K(n) {
584
587
  return {
585
588
  install() {
586
- m.runtimeDoms = l;
589
+ m.runtimeDoms = n;
587
590
  }
588
591
  };
589
592
  }
593
+ function W(n, e) {
594
+ return e === "raw" && Object.defineProperty(n, "name", {
595
+ value: `__proform_raw_${n.name}`,
596
+ writable: !0
597
+ }), n;
598
+ }
590
599
  export {
591
- _ as ProForm,
600
+ q as ProForm,
592
601
  G as useForm,
593
- K as useFormRenderer
602
+ K as useFormRenderer,
603
+ W as useModifiers
594
604
  };
@@ -1,3 +1,4 @@
1
1
  export { default as ProForm } from "./src/form/index";
2
2
  export { useForm } from "./src/interactions/useForm";
3
3
  export { useFormRenderer } from "./src/interactions/useFormRenderer";
4
+ export { useModifiers } from "./src/interactions/useModifiers";
@@ -0,0 +1,4 @@
1
+ import { AnyFunction } from "../types";
2
+ type Modifier = "raw";
3
+ export declare function useModifiers(functionRaw: AnyFunction, modifier: Modifier): AnyFunction;
4
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@harbor-design/proform",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",