@harbor-design/proform 1.0.17 → 1.0.19

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 O=(o,e,t)=>e in o?I(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var c=(o,e,t)=>(O(o,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("vue");class f{static typeChecker(e){return{}.toString.call(e)}static isString(e){return typeof e=="string"}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 S(o,...e){return e.forEach(t=>{for(let s in t)t.hasOwnProperty(s)&&(typeof t[s]=="object"&&t[s]!==null?(o[s]=o[s]||{},S(o[s],t[s])):o[s]=t[s])}),o}function E(o){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,a]of s)i.set(t(l),t(a));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(o)}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(n.toRaw(this.runtimeCore.processor.processedModel.value))))})}hydrate(e){this.runtimeCore.hydrateEffect.trackEffect(()=>{n.isRef(e)?n.watch(()=>e.value,()=>{S(this.runtimeCore.model.value,e.value)},{deep:!0,immediate:!0}):n.isReactive(e)?n.watch(()=>e,()=>{S(this.runtimeCore.model.value,e)},{deep:!0,immediate:!0}):S(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),n.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){f.isArrayEmpty(this.processedSchemas.value)&&(this.baseDefaultValueFunctionsLength=this.countFunctionDefaultValues(E(e)),this.processedSchemas.value=this.initSchemas(e)),this.parse(e,t)}parseStable(e){const t={};if(!f.isUndefined(e.stable))t[e.key]=this.parseStable(e.stable);else return e;return t}stableUpdater(e=[]){if(e.every(Boolean)){const t=n.toRaw(this.processedSchemas.value);!f.isProcessInprogress(t)&&f.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(a=>a!=="children").length}).fill(!1);this.objectParser({data:e,index:t,updater:l});function l(a){const u=a.index,h=a.key,P=a.keyIndex;if(!a.stable)return;const v=r.parseStable(a.stable),y=s==null?void 0:s.index,g=s==null?void 0:s.key;let d=v;if(f.isProcessInprogress(d)||(i[P]=!0),s){let p=r.processedSchemas.value[y][g][u][h];p&&f.isObject(p)&&h!=="component"&&(d=Object.assign(p,d)),r.processedSchemas.value[y][g][u][h]=d,r.stableUpdater(i)}else{let p=r.processedSchemas.value[u][h];p&&f.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=a=>{e.updater({...e,key:r,keyIndex:i,stable:a})};if(f.isFunction(t[r]))r!=="defaultValue"?this.schemaEffect.trackEffect(()=>{if(r==="component"){const a=t[r](this.getRuntimeMeta());this.promiseFieldParser(a,l,!1)}else this.fieldParser(t[r],l)}):this.defaultValueEffect.trackEffect(()=>{const a=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),!f.isProcessInprogress(u)&&this.defaultValueInprogressMap.size===this.baseDefaultValueFunctionsLength&&Array.from(this.defaultValueInprogressMap.values()).every(h=>!h.includes("undefined"))?(l(u),this.defaultValueEffect.clearEffects(),n.nextTick(()=>{a()})):l(u)}):this.fieldParser(t[r],u=>{this.defaultValueInprogressMap.set(t[r],u),!f.isProcessInprogress(u)&&this.defaultValueInprogressMap.size===this.baseDefaultValueFunctionsLength&&Array.from(this.defaultValueInprogressMap.values()).every(h=>!h.includes("undefined"))?(l(u),this.defaultValueEffect.clearEffects(),n.nextTick(()=>{a()})):l(u)})})});else if(r==="component"){const a=t[r];this.promiseFieldParser(a,l,!1)}else this.fieldParser(t[r],l)}})}replaceUndefinedInString(e,t){return e.replace(/undefined/g,t)}promiseFieldParser(e,t,s){f.isPromise(e)?e.then(r=>{s&&f.isObject(r)?this.objectParser({data:r,updater:t}):t(r)}):(f.isString(e)&&(e=this.replaceUndefinedInString(e,"")),s&&f.isObject(e)?this.objectParser({data:e,updater:t}):t(e))}fieldParser(e,t,s=!0){if(f.isFunction(e))if(e.name.startsWith("__proform_raw_"))t(e);else{const r=e(this.getRuntimeMeta());this.promiseFieldParser(r,t,s)}else n.isRef(e)?n.watch(()=>e.value,()=>{f.isUndefined(e.value)||(s&&f.isObject(e.value)?this.objectParser({data:e.value,updater:t}):t(e.value))},{immediate:!0,deep:!0}):n.isReactive(e)?n.watch(()=>e,()=>{f.isUndefined(e)||(s&&f.isObject(e)?this.objectParser({data:e,updater:t}):t(e))},{immediate:!0,deep:!0}):s&&f.isObject(e)?this.objectParser({data:e,updater:t}):t(e)}modelProcessor(e){e.map(t=>this.createModel(t,this.processedModel.value)),f.isObjectEmpty(this.stableModel)&&this.stableUpdaterProcessProgress.every(Boolean)&&this.defaultValueEffect.effects.size===0&&(this.stableModel=E(this.processedModel.value),this.runtimeCore.hydrateEffect.triggerEffects(),this.runtimeCore.hydrateEffect.clearEffects())}createModel(e,t){f.isListSchema(e)&&(t[e.field]||(t[e.field]=[{}]),e.children.forEach(s=>{this.createModel(s,t[e.field][0])})),f.isGroupSchema(e)&&e.children.forEach(s=>{this.createModel(s,t)}),f.isItemSchema(e)&&(t[e.field]=e.defaultValue)}}function w(o){return typeof o=="function"||Object.prototype.toString.call(o)==="[object Object]"&&!n.isVNode(o)}class M{constructor(e){c(this,"schemas",n.ref([]));c(this,"model",n.ref({}));c(this,"processorBySchemaType",{item:this.runtimeItemProcessor.bind(this),group:this.runtimeGroupProcessor.bind(this),list:this.runtimeListProcessor.bind(this)});c(this,"formRef",n.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:n.toRaw(E(this.model.value))}}runtimeItemProcessor(e,t,s=this.model.value,r){var g;const i=r?`${r.field}.${t}.${e.field}`:e.field,l=n.toRaw(e.component);if(!l)return;const a=l.name,u=e.componentProps??{},h=V.placeholderPresetByComponentName;let P=e.placeholder;if(P||(P=`${h[a]??"请输入"}${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 y=e.show;return y===void 0&&(y=!0),y||delete s[e.field],n.createVNode(m.runtimeDoms.Item,null,{default(){return n.withDirectives(n.createVNode(m.runtimeDoms.FormItem,{label:`${e.label}:`,rules:e.rules,field:i},{default:()=>[n.createVNode(l,n.mergeProps({modelValue:s[e.field],"onUpdate:modelValue":d=>s[e.field]=d,placeholder:P},u),null)]}),[[n.vShow,y]])}})}runtimeGroupProcessor(e){let t;return n.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(E(this.processor.stableModel[e.field][0])),this.formRef.value.clearValidate()}deleteListItem(e,t){this.model.value[e.field].splice(t,1),this.formRef.value.clearValidate()}runtimeListProcessor(e){const t=this;return t.model.value[e.field]||(t.model.value[e.field]=[{}]),n.createVNode(m.runtimeDoms.List,{schema:e},{default(){return t.model.value[e.field].map((s,r)=>n.createVNode(m.runtimeDoms.ListItem,null,{default(){return e.children.map(i=>t.runtimeItemProcessor(i,r,s,e))},delete({container:i}={}){var a;let l=i??n.createVNode("button",null,null);return n.withDirectives(n.createVNode(l,{onClick:()=>t.deleteListItem(e,r)},null),[[n.vShow,((a=t.model.value[e.field])==null?void 0:a.length)>1]])}}))},add({container:s}={}){let r=s??n.createVNode("button",null,[n.createTextVNode("添加")]);return n.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 n.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 x=n.defineComponent({props:{setup:{type:Function,required:!0}},setup(o){const e=new M(o.setup);return()=>e.exec()}});function A(o){const e=new C(o);return[e.setup.bind(e),{submit:e.submit.bind(e),hydrate:e.hydrate.bind(e)}]}function D(o){return{install(){m.runtimeDoms=o}}}function N(o,e){return e==="raw"&&Object.defineProperty(o,"name",{value:`__proform_raw_${o.name}`,writable:!0}),o}exports.ProForm=x;exports.useForm=A;exports.useFormRenderer=D;exports.useModifiers=N;
1
+ "use strict";var L=Object.defineProperty;var T=(n,e,t)=>e in n?L(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var a=(n,e,t)=>(T(n,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("vue");class u{static typeChecker(e){return{}.toString.call(e)}static isString(e){return typeof e=="string"}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 p(n,...e){return e.forEach(t=>{if(Array.isArray(t))Array.isArray(n)||(n=[]),t.forEach((s,r)=>{typeof s=="object"&&s!==null?n[r]=p(Array.isArray(s)?[]:{},s):n[r]=s});else for(let s in t)t.hasOwnProperty(s)&&(typeof t[s]=="object"&&t[s]!==null?n[s]=p(n[s]||{},t[s]):n[s]=t[s])}),n}function y(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 o=new Map;for(let[l,f]of s)o.set(t(l),t(f));return o}if(s instanceof Set){const o=new Set;for(let l of s)o.add(t(l));return o}if(e.has(s))return e.get(s);if(Array.isArray(s)){const o=[];e.set(s,o);for(let l=0;l<s.length;l++)o[l]=t(s[l]);return o}const r=Object.create(Object.getPrototypeOf(s));e.set(s,r);for(let o in s)s.hasOwnProperty(o)&&(r[o]=t(s[o]));return r}return t(n)}class B{constructor(e){a(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(i.toRaw(this.runtimeCore.processor.processedModel.value))))})}hydrate(e){if(!this.runtimeCore)return Promise.reject({code:"0002",message:"hydrate 使用时机错误,建议将 hydrate 操作放到 onMounted 等页面节点挂载完成的钩子中,或者使用响应式的值来注入数据"});this.runtimeCore.hydrateEffect.trackEffect(()=>{i.isRef(e)?i.watch(()=>e.value,()=>{p(this.runtimeCore.model.value,e.value)},{deep:!0,immediate:!0}):i.isReactive(e)?i.watch(()=>e,()=>{p(this.runtimeCore.model.value,e)},{deep:!0,immediate:!0}):p(this.runtimeCore.model.value,e)},{lazy:!0})}customize(e){Object.assign(this.runtimeCore.customizedOptions,e),Object.assign(this.runtimeCore.gridProps,this.formCustomization.gridProps)}}class I{constructor(){a(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 q{constructor(e){a(this,"runtimeCore");a(this,"processedSchemas");a(this,"processedModel");a(this,"getRuntimeMeta");a(this,"stableSchemas",[]);a(this,"stableModel",{});a(this,"schemaPreset",v.schemaPreset);a(this,"componentPropsPreset",v.componentPropsPreset);a(this,"stableUpdaterProcessProgress");a(this,"stableUpdaterTimes",0);a(this,"schemaEffect",new I);a(this,"defaultValueEffect",new I);a(this,"defaultValueInprogressMap",new Map);a(this,"baseDefaultValueFunctionsLength");this.runtimeCore=e,this.processedSchemas=e.schemas,this.processedModel=e.model,this.getRuntimeMeta=e.getRuntimeMeta.bind(e),i.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(o){if(!s.has(o)&&(Array.isArray(o)||o!==null&&typeof o=="object")){s.add(o);for(let l in o)o.hasOwnProperty(l)&&(l==="defaultValue"&&typeof o[l]=="function"&&!o[l].toString().includes("[native code]")&&t++,r(o[l]))}}return r(e),t}parseSchemas(e,t){u.isArrayEmpty(this.processedSchemas.value)&&(this.baseDefaultValueFunctionsLength=this.countFunctionDefaultValues(y(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=i.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,o=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 c=f.index,d=f.key,j=f.keyIndex;if(!f.stable)return;const E=r.parseStable(f.stable),V=s==null?void 0:s.index,g=s==null?void 0:s.key;let h=E;if(u.isProcessInprogress(h)||(o[j]=!0),s){let m=r.processedSchemas.value[V][g][c][d];m&&u.isObject(m)&&d!=="component"&&(h=p(m,h)),r.processedSchemas.value[V][g][c][d]=h,r.stableUpdater(o)}else{let m=r.processedSchemas.value[c][d];m&&u.isObject(m)&&(h=p(m,h)),r.processedSchemas.value[c][d]=h,r.stableUpdater(o)}}}objectParser(e){const t=e.data;Object.keys(t).forEach((r,o)=>{if(r==="children")this.parseSchemas(t[r],{...e,key:r,keyIndex:o});else{const l=f=>{e.updater({...e,key:r,keyIndex:o,stable:f})};u.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],c=>{if(!c)return l(c);this.defaultValueInprogressMap.set(t[r],c),!u.isProcessInprogress(c)&&this.defaultValueInprogressMap.size===this.baseDefaultValueFunctionsLength&&Array.from(this.defaultValueInprogressMap.values()).every(d=>!d.includes("undefined"))?(l(c),this.defaultValueEffect.clearEffects(),i.nextTick(()=>{f()})):l(c)}):this.fieldParser(t[r],c=>{this.defaultValueInprogressMap.set(t[r],c),!u.isProcessInprogress(c)&&this.defaultValueInprogressMap.size===this.baseDefaultValueFunctionsLength&&Array.from(this.defaultValueInprogressMap.values()).every(d=>!d.includes("undefined"))?(l(c),this.defaultValueEffect.clearEffects(),i.nextTick(()=>{f()})):l(c)})})}):r==="component"||r==="slots"?this.promiseFieldParser(t[r],l,!1):this.fieldParser(t[r],l)}})}replaceUndefinedInString(e,t){return e.replace(/undefined/g,t)}promiseFieldParser(e,t,s){u.isPromise(e)?e.then(r=>{s&&u.isObject(r)?this.objectParser({data:r,updater:t}):t(r)}):(u.isString(e)&&(e=this.replaceUndefinedInString(e,"")),s&&u.isObject(e)?this.objectParser({data:e,updater:t}):t(e))}fieldParser(e,t,s=!0){if(u.isFunction(e))if(e.name.startsWith("__proform_raw_"))t(e);else{const r=e(this.getRuntimeMeta());this.promiseFieldParser(r,t,s)}else i.isRef(e)?i.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}):i.isReactive(e)?i.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=y(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 $(n){return typeof n=="function"||Object.prototype.toString.call(n)==="[object Object]"&&!i.isVNode(n)}class G{constructor(e){a(this,"schemas",i.ref([]));a(this,"model",i.ref({}));a(this,"processorBySchemaType",{item:this.runtimeItemProcessor.bind(this),group:this.runtimeGroupProcessor.bind(this),list:this.runtimeListProcessor.bind(this)});a(this,"formRef",i.ref(null));a(this,"hydrateEffect",new I);a(this,"customizedOptions",i.reactive({}));a(this,"gridProps",{});a(this,"globalNativeFormOverride",{props:{},slots:{}});this.setup=e,this.processor=new q(this);const t=this.setup(this);this.processor.parseSchemas(t.schemas)}getRuntimeMeta(){return{model:i.toRaw(y(this.model.value))}}runtimeItemProcessor(e,t,s=this.model.value,r){var w,O,C,N,A,F,M,R,z,x,D,U,k;p(this.globalNativeFormOverride.props,(O=(w=e.native)==null?void 0:w.props)==null?void 0:O.Form),p(this.globalNativeFormOverride.slots,(N=(C=e.native)==null?void 0:C.slots)==null?void 0:N.Form);const o=p(y((F=(A=this.customizedOptions.native)==null?void 0:A.props)==null?void 0:F.FormItem)??{},(R=(M=e.native)==null?void 0:M.props)==null?void 0:R.FormItem),l=p(y((x=(z=this.customizedOptions.native)==null?void 0:z.slots)==null?void 0:x.FormItem)??{},(U=(D=e.native)==null?void 0:D.slots)==null?void 0:U.FormItem),f={display:"grid",gridColumn:"1 / -1",...e.gridProps},c=r?`${r.field}.${t}.${e.field}`:e.field,d=i.toRaw(e.component);if(!d)return;const j=d.name,E=e.componentProps??{},V=v.placeholderPresetByComponentName;let g=e.placeholder;if(g||(g=`${V[j]??"请输入"}${e.label}`),e.required)if(!e.rules)e.rules=[],(k=e.rules)==null||k.push({required:!0,message:`${e.label}是必填项`});else{const S=e.rules.findIndex(_=>!!_.required);e.rules[S].message=`${e.label}是必填项`}let m=e.show;return m===void 0&&(m=!0),m||delete s[e.field],i.createVNode("div",{style:f},[i.createVNode(b.runtimeDoms.Item,null,{default(){return i.withDirectives(i.createVNode(b.runtimeDoms.FormItem,i.mergeProps(o,{label:`${e.label}:`,rules:e.rules,field:c}),{default(){return i.createVNode(d,i.mergeProps({modelValue:s[e.field],"onUpdate:modelValue":S=>s[e.field]=S,placeholder:g},E),null)},...l}),[[i.vShow,m]])}})])}runtimeGroupProcessor(e){let t;const s={display:"grid",gridColumn:"1 / -1",...e.gridProps};return i.createVNode("div",{style:s},[i.createVNode(b.runtimeDoms.Group,{schema:e},$(t=e.children.map(r=>this.runtimeItemProcessor(r)))?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(y(this.processor.stableModel[e.field][0])),this.formRef.value.clearValidate()}deleteListItem(e,t){this.model.value[e.field].splice(t,1),this.formRef.value.clearValidate()}runtimeListProcessor(e){const t={display:"grid",gridColumn:"1 / -1",...e.gridProps},s=this;return s.model.value[e.field]||(s.model.value[e.field]=[{}]),i.createVNode("div",{style:t},[i.createVNode(b.runtimeDoms.List,{schema:e},{default(){return s.model.value[e.field].map((r,o)=>i.createVNode(b.runtimeDoms.ListItem,null,{default(){return e.children.map(l=>s.runtimeItemProcessor(l,o,r,e))},delete({container:l}={}){var c;let f=l??i.createVNode("button",null,null);return i.withDirectives(i.createVNode(f,{onClick:()=>s.deleteListItem(e,o)},null),[[i.vShow,((c=s.model.value[e.field])==null?void 0:c.length)>1]])}}))},add({container:r}={}){let o=r??i.createVNode("button",null,[i.createTextVNode("添加")]);return i.createVNode(o,{onClick:()=>s.addListItem(e)},null)}})])}runtimeProcessor(e){return e.map(t=>(t.type||(t.type="item"),this.processorBySchemaType[t.type](t)))}exec(){var o,l,f,c;const e={display:"grid",gridColumn:"1 / -1",gridAutoColumns:"1fr",...this.gridProps},t=this,s=p(y((l=(o=this.customizedOptions.native)==null?void 0:o.props)==null?void 0:l.Form)??{},this.globalNativeFormOverride.props),r=p(y((c=(f=this.customizedOptions.native)==null?void 0:f.slots)==null?void 0:c.Form)??{},this.globalNativeFormOverride.slots);return i.createVNode(b.runtimeDoms.Form,i.mergeProps(s,{ref:this.formRef,model:this.model.value}),{default(){return i.createVNode("div",{style:e},[t.runtimeProcessor(t.schemas.value)])},...r})}}class b{}a(b,"runtimeDoms");const P=class P{static getPlaceholderPrefixPresetByComponentName(){const e={请选择:["Select","Tree","TreeSelect"],请输入:["Input"]},t={};for(let s in e)e[s].forEach(r=>{t[r]=s});return t}};a(P,"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:[]},native:void 0,gridProps:void 0}),a(P,"componentPropsPreset",{options:{defaultValue:[]}}),a(P,"placeholderPresetByComponentName",P.getPlaceholderPrefixPresetByComponentName());let v=P;const K=i.defineComponent({props:{setup:{type:Function,required:!0}},setup(n){const e=new G(n.setup);return()=>e.exec()}});function W(n){const e=new B(n);return[e.setup.bind(e),{submit:e.submit.bind(e),hydrate:e.hydrate.bind(e),customize:e.customize.bind(e)}]}function H(n){return{install(){b.runtimeDoms=n}}}function J(n,e){return e==="raw"&&Object.defineProperty(n,"name",{value:`__proform_raw_${n.name}`,writable:!0}),n}exports.ProForm=K;exports.useForm=W;exports.useFormRenderer=H;exports.useModifiers=J;
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
- var k = Object.defineProperty;
2
- var _ = (n, e, t) => e in n ? k(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 U, watch as S, isReactive as D, nextTick as O, ref as I, createVNode as h, withDirectives as x, mergeProps as R, vShow as A, createTextVNode as L, isVNode as z, defineComponent as B } from "vue";
5
- class c {
1
+ var J = Object.defineProperty;
2
+ var Q = (o, e, t) => e in o ? J(o, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[e] = t;
3
+ var l = (o, e, t) => (Q(o, typeof e != "symbol" ? e + "" : e, t), t);
4
+ import { toRaw as I, isRef as K, watch as v, isReactive as W, nextTick as $, ref as C, reactive as X, createVNode as c, withDirectives as q, mergeProps as w, vShow as G, createTextVNode as Y, isVNode as Z, defineComponent as ee } from "vue";
5
+ class f {
6
6
  static typeChecker(e) {
7
7
  return {}.toString.call(e);
8
8
  }
@@ -63,13 +63,18 @@ class c {
63
63
  return !1;
64
64
  }
65
65
  }
66
- function j(n, ...e) {
66
+ function p(o, ...e) {
67
67
  return e.forEach((t) => {
68
- for (let s in t)
69
- t.hasOwnProperty(s) && (typeof t[s] == "object" && t[s] !== null ? (n[s] = n[s] || {}, j(n[s], t[s])) : n[s] = t[s]);
70
- }), n;
68
+ if (Array.isArray(t))
69
+ Array.isArray(o) || (o = []), t.forEach((s, r) => {
70
+ typeof s == "object" && s !== null ? o[r] = p(Array.isArray(s) ? [] : {}, s) : o[r] = s;
71
+ });
72
+ else
73
+ for (let s in t)
74
+ t.hasOwnProperty(s) && (typeof t[s] == "object" && t[s] !== null ? o[s] = p(o[s] || {}, t[s]) : o[s] = t[s]);
75
+ }), o;
71
76
  }
72
- function v(n) {
77
+ function y(o) {
73
78
  const e = /* @__PURE__ */ new WeakMap();
74
79
  function t(s) {
75
80
  if (s === null || typeof s != "object")
@@ -80,14 +85,14 @@ function v(n) {
80
85
  return new RegExp(s);
81
86
  if (s instanceof Map) {
82
87
  const i = /* @__PURE__ */ new Map();
83
- for (let [l, f] of s)
84
- i.set(t(l), t(f));
88
+ for (let [n, u] of s)
89
+ i.set(t(n), t(u));
85
90
  return i;
86
91
  }
87
92
  if (s instanceof Set) {
88
93
  const i = /* @__PURE__ */ new Set();
89
- for (let l of s)
90
- i.add(t(l));
94
+ for (let n of s)
95
+ i.add(t(n));
91
96
  return i;
92
97
  }
93
98
  if (e.has(s))
@@ -95,8 +100,8 @@ function v(n) {
95
100
  if (Array.isArray(s)) {
96
101
  const i = [];
97
102
  e.set(s, i);
98
- for (let l = 0; l < s.length; l++)
99
- i[l] = t(s[l]);
103
+ for (let n = 0; n < s.length; n++)
104
+ i[n] = t(s[n]);
100
105
  return i;
101
106
  }
102
107
  const r = Object.create(Object.getPrototypeOf(s));
@@ -105,11 +110,11 @@ function v(n) {
105
110
  s.hasOwnProperty(i) && (r[i] = t(s[i]));
106
111
  return r;
107
112
  }
108
- return t(n);
113
+ return t(o);
109
114
  }
110
- class T {
115
+ class te {
111
116
  constructor(e) {
112
- o(this, "runtimeCore");
117
+ l(this, "runtimeCore");
113
118
  this.formCustomization = e;
114
119
  }
115
120
  // happy path, 后续可以完善更多的 fallback 处理,fallback 处理是为了不卡住异步时的首次渲染做的优化
@@ -125,43 +130,52 @@ class T {
125
130
  return new Promise((e, t) => {
126
131
  this.runtimeCore.formRef.value.validate((s) => s ? t(s) : e(
127
132
  this.cleanFallbackFields(
128
- V(this.runtimeCore.processor.processedModel.value)
133
+ I(this.runtimeCore.processor.processedModel.value)
129
134
  )
130
135
  ));
131
136
  });
132
137
  }
133
138
  hydrate(e) {
139
+ if (!this.runtimeCore)
140
+ return Promise.reject({
141
+ code: "0002",
142
+ message: "hydrate 使用时机错误,建议将 hydrate 操作放到 onMounted 等页面节点挂载完成的钩子中,或者使用响应式的值来注入数据"
143
+ });
134
144
  this.runtimeCore.hydrateEffect.trackEffect(
135
145
  () => {
136
- U(e) ? S(
146
+ K(e) ? v(
137
147
  () => e.value,
138
148
  () => {
139
- j(this.runtimeCore.model.value, e.value);
149
+ p(this.runtimeCore.model.value, e.value);
140
150
  },
141
151
  {
142
152
  deep: !0,
143
153
  immediate: !0
144
154
  }
145
- ) : D(e) ? S(
155
+ ) : W(e) ? v(
146
156
  () => e,
147
157
  () => {
148
- j(this.runtimeCore.model.value, e);
158
+ p(this.runtimeCore.model.value, e);
149
159
  },
150
160
  {
151
161
  deep: !0,
152
162
  immediate: !0
153
163
  }
154
- ) : j(this.runtimeCore.model.value, e);
164
+ ) : p(this.runtimeCore.model.value, e);
155
165
  },
156
166
  {
157
167
  lazy: !0
158
168
  }
159
169
  );
160
170
  }
171
+ // TODO:目前仅用于配制一些基本的如 Form,FormItem 等 UI 库组件的默认属性,但后续会扩展其价值,包括设置统一布局等,都会考虑往内部封装
172
+ customize(e) {
173
+ Object.assign(this.runtimeCore.customizedOptions, e), Object.assign(this.runtimeCore.gridProps, this.formCustomization.gridProps);
174
+ }
161
175
  }
162
- class w {
176
+ class A {
163
177
  constructor() {
164
- o(this, "effects", /* @__PURE__ */ new Set());
178
+ l(this, "effects", /* @__PURE__ */ new Set());
165
179
  }
166
180
  clearEffects() {
167
181
  this.effects.clear();
@@ -175,23 +189,23 @@ class w {
175
189
  return !t.lazy && e(), this.effects.add(e), () => this.effects.delete(e);
176
190
  }
177
191
  }
178
- class N {
192
+ class se {
179
193
  constructor(e) {
180
- o(this, "runtimeCore");
181
- o(this, "processedSchemas");
182
- o(this, "processedModel");
183
- o(this, "getRuntimeMeta");
184
- o(this, "stableSchemas", []);
185
- o(this, "stableModel", {});
186
- o(this, "schemaPreset", E.schemaPreset);
187
- o(this, "componentPropsPreset", E.componentPropsPreset);
188
- o(this, "stableUpdaterProcessProgress");
189
- o(this, "stableUpdaterTimes", 0);
190
- o(this, "schemaEffect", new w());
191
- o(this, "defaultValueEffect", new w());
192
- o(this, "defaultValueInprogressMap", /* @__PURE__ */ new Map());
193
- o(this, "baseDefaultValueFunctionsLength");
194
- this.runtimeCore = e, this.processedSchemas = e.schemas, this.processedModel = e.model, this.getRuntimeMeta = e.getRuntimeMeta.bind(e), S(
194
+ l(this, "runtimeCore");
195
+ l(this, "processedSchemas");
196
+ l(this, "processedModel");
197
+ l(this, "getRuntimeMeta");
198
+ l(this, "stableSchemas", []);
199
+ l(this, "stableModel", {});
200
+ l(this, "schemaPreset", S.schemaPreset);
201
+ l(this, "componentPropsPreset", S.componentPropsPreset);
202
+ l(this, "stableUpdaterProcessProgress");
203
+ l(this, "stableUpdaterTimes", 0);
204
+ l(this, "schemaEffect", new A());
205
+ l(this, "defaultValueEffect", new A());
206
+ l(this, "defaultValueInprogressMap", /* @__PURE__ */ new Map());
207
+ l(this, "baseDefaultValueFunctionsLength");
208
+ this.runtimeCore = e, this.processedSchemas = e.schemas, this.processedModel = e.model, this.getRuntimeMeta = e.getRuntimeMeta.bind(e), v(
195
209
  () => this.processedModel.value,
196
210
  () => {
197
211
  this.schemaEffect.triggerEffects();
@@ -219,21 +233,21 @@ class N {
219
233
  function r(i) {
220
234
  if (!s.has(i) && (Array.isArray(i) || i !== null && typeof i == "object")) {
221
235
  s.add(i);
222
- for (let l in i)
223
- i.hasOwnProperty(l) && (l === "defaultValue" && typeof i[l] == "function" && !i[l].toString().includes("[native code]") && t++, r(i[l]));
236
+ for (let n in i)
237
+ i.hasOwnProperty(n) && (n === "defaultValue" && typeof i[n] == "function" && !i[n].toString().includes("[native code]") && t++, r(i[n]));
224
238
  }
225
239
  }
226
240
  return r(e), t;
227
241
  }
228
242
  // 派生过程,用于外部应用
229
243
  parseSchemas(e, t) {
230
- c.isArrayEmpty(this.processedSchemas.value) && (this.baseDefaultValueFunctionsLength = this.countFunctionDefaultValues(
231
- v(e)
244
+ f.isArrayEmpty(this.processedSchemas.value) && (this.baseDefaultValueFunctionsLength = this.countFunctionDefaultValues(
245
+ y(e)
232
246
  ), this.processedSchemas.value = this.initSchemas(e)), this.parse(e, t);
233
247
  }
234
248
  parseStable(e) {
235
249
  const t = {};
236
- if (!c.isUndefined(e.stable))
250
+ if (!f.isUndefined(e.stable))
237
251
  t[e.key] = this.parseStable(e.stable);
238
252
  else
239
253
  return e;
@@ -242,8 +256,8 @@ class N {
242
256
  // 对于稳定初始化更新的抽象
243
257
  stableUpdater(e = []) {
244
258
  if (e.every(Boolean)) {
245
- const t = V(this.processedSchemas.value);
246
- !c.isProcessInprogress(t) && c.isObjectEmpty(this.stableModel) && (this.stableUpdaterProcessProgress || (this.stableUpdaterProcessProgress = Array.from({
259
+ const t = I(this.processedSchemas.value);
260
+ !f.isProcessInprogress(t) && f.isObjectEmpty(this.stableModel) && (this.stableUpdaterProcessProgress || (this.stableUpdaterProcessProgress = Array.from({
247
261
  length: t.length
248
262
  }).fill(!1)), this.stableUpdaterProcessProgress[this.stableUpdaterTimes] = !0, this.stableUpdaterTimes++, this.modelProcessor(t));
249
263
  }
@@ -251,21 +265,21 @@ class N {
251
265
  // 核心单 schema 处理过程,接收一个对象键值对的数据结构
252
266
  parseItem(e, t, s) {
253
267
  const r = this, i = Array.from({
254
- length: Object.keys(e).filter((f) => f !== "children").length
268
+ length: Object.keys(e).filter((u) => u !== "children").length
255
269
  }).fill(!1);
256
- this.objectParser({ data: e, index: t, updater: l });
257
- function l(f) {
258
- const u = f.index, d = f.key, P = f.keyIndex;
259
- if (!f.stable)
270
+ this.objectParser({ data: e, index: t, updater: n });
271
+ function n(u) {
272
+ const a = u.index, d = u.key, O = u.keyIndex;
273
+ if (!u.stable)
260
274
  return;
261
- const C = r.parseStable(f.stable), y = s == null ? void 0 : s.index, g = s == null ? void 0 : s.key;
262
- let a = C;
263
- if (c.isProcessInprogress(a) || (i[P] = !0), s) {
264
- let p = r.processedSchemas.value[y][g][u][d];
265
- p && c.isObject(p) && d !== "component" && (a = Object.assign(p, a)), r.processedSchemas.value[y][g][u][d] = a, r.stableUpdater(i);
275
+ const V = r.parseStable(u.stable), E = s == null ? void 0 : s.index, g = s == null ? void 0 : s.key;
276
+ let h = V;
277
+ if (f.isProcessInprogress(h) || (i[O] = !0), s) {
278
+ let m = r.processedSchemas.value[E][g][a][d];
279
+ m && f.isObject(m) && d !== "component" && (h = p(m, h)), r.processedSchemas.value[E][g][a][d] = h, r.stableUpdater(i);
266
280
  } else {
267
- let p = r.processedSchemas.value[u][d];
268
- p && c.isObject(p) && (a = Object.assign(p, a)), r.processedSchemas.value[u][d] = a, r.stableUpdater(i);
281
+ let m = r.processedSchemas.value[a][d];
282
+ m && f.isObject(m) && (h = p(m, h)), r.processedSchemas.value[a][d] = h, r.stableUpdater(i);
269
283
  }
270
284
  }
271
285
  }
@@ -280,45 +294,39 @@ class N {
280
294
  keyIndex: i
281
295
  });
282
296
  else {
283
- const l = (f) => {
297
+ const n = (u) => {
284
298
  e.updater({
285
299
  ...e,
286
300
  key: r,
287
301
  keyIndex: i,
288
- stable: f
302
+ stable: u
289
303
  });
290
304
  };
291
- if (c.isFunction(t[r]))
292
- r !== "defaultValue" ? this.schemaEffect.trackEffect(() => {
293
- if (r === "component") {
294
- const f = t[r](this.getRuntimeMeta());
295
- this.promiseFieldParser(f, l, !1);
296
- } else
297
- this.fieldParser(t[r], l);
298
- }) : this.defaultValueEffect.trackEffect(() => {
299
- const f = this.schemaEffect.trackEffect(() => {
300
- /\{\s*model\s*\}/.test(t[r].toString()) ? this.fieldParser(t[r], (u) => {
301
- if (!u)
302
- return l(u);
303
- this.defaultValueInprogressMap.set(t[r], u), !c.isProcessInprogress(u) && this.defaultValueInprogressMap.size === this.baseDefaultValueFunctionsLength && Array.from(this.defaultValueInprogressMap.values()).every(
304
- (d) => !d.includes("undefined")
305
- ) ? (l(u), this.defaultValueEffect.clearEffects(), O(() => {
306
- f();
307
- })) : l(u);
308
- }) : this.fieldParser(t[r], (u) => {
309
- this.defaultValueInprogressMap.set(t[r], u), !c.isProcessInprogress(u) && this.defaultValueInprogressMap.size === this.baseDefaultValueFunctionsLength && Array.from(this.defaultValueInprogressMap.values()).every(
310
- (d) => !d.includes("undefined")
311
- ) ? (l(u), this.defaultValueEffect.clearEffects(), O(() => {
312
- f();
313
- })) : l(u);
314
- });
305
+ f.isFunction(t[r]) ? r !== "defaultValue" ? this.schemaEffect.trackEffect(() => {
306
+ if (r === "component") {
307
+ const u = t[r](this.getRuntimeMeta());
308
+ this.promiseFieldParser(u, n, !1);
309
+ } else
310
+ this.fieldParser(t[r], n);
311
+ }) : this.defaultValueEffect.trackEffect(() => {
312
+ const u = this.schemaEffect.trackEffect(() => {
313
+ /\{\s*model\s*\}/.test(t[r].toString()) ? this.fieldParser(t[r], (a) => {
314
+ if (!a)
315
+ return n(a);
316
+ this.defaultValueInprogressMap.set(t[r], a), !f.isProcessInprogress(a) && this.defaultValueInprogressMap.size === this.baseDefaultValueFunctionsLength && Array.from(this.defaultValueInprogressMap.values()).every(
317
+ (d) => !d.includes("undefined")
318
+ ) ? (n(a), this.defaultValueEffect.clearEffects(), $(() => {
319
+ u();
320
+ })) : n(a);
321
+ }) : this.fieldParser(t[r], (a) => {
322
+ this.defaultValueInprogressMap.set(t[r], a), !f.isProcessInprogress(a) && this.defaultValueInprogressMap.size === this.baseDefaultValueFunctionsLength && Array.from(this.defaultValueInprogressMap.values()).every(
323
+ (d) => !d.includes("undefined")
324
+ ) ? (n(a), this.defaultValueEffect.clearEffects(), $(() => {
325
+ u();
326
+ })) : n(a);
315
327
  });
316
328
  });
317
- else if (r === "component") {
318
- const f = t[r];
319
- this.promiseFieldParser(f, l, !1);
320
- } else
321
- this.fieldParser(t[r], l);
329
+ }) : r === "component" || r === "slots" ? this.promiseFieldParser(t[r], n, !1) : this.fieldParser(t[r], n);
322
330
  }
323
331
  });
324
332
  }
@@ -326,19 +334,19 @@ class N {
326
334
  return e.replace(/undefined/g, t);
327
335
  }
328
336
  promiseFieldParser(e, t, s) {
329
- c.isPromise(e) ? e.then((r) => {
330
- s && c.isObject(r) ? this.objectParser({
337
+ f.isPromise(e) ? e.then((r) => {
338
+ s && f.isObject(r) ? this.objectParser({
331
339
  data: r,
332
340
  updater: t
333
341
  }) : t(r);
334
- }) : (c.isString(e) && (e = this.replaceUndefinedInString(e, "")), s && c.isObject(e) ? this.objectParser({
342
+ }) : (f.isString(e) && (e = this.replaceUndefinedInString(e, "")), s && f.isObject(e) ? this.objectParser({
335
343
  data: e,
336
344
  updater: t
337
345
  }) : t(e));
338
346
  }
339
347
  // 对任意对象中单个字段的 parse: 做基本处理
340
348
  fieldParser(e, t, s = !0) {
341
- if (c.isFunction(e))
349
+ if (f.isFunction(e))
342
350
  if (e.name.startsWith("__proform_raw_"))
343
351
  t(e);
344
352
  else {
@@ -346,10 +354,10 @@ class N {
346
354
  this.promiseFieldParser(r, t, s);
347
355
  }
348
356
  else
349
- U(e) ? S(
357
+ K(e) ? v(
350
358
  () => e.value,
351
359
  () => {
352
- c.isUndefined(e.value) || (s && c.isObject(e.value) ? this.objectParser({
360
+ f.isUndefined(e.value) || (s && f.isObject(e.value) ? this.objectParser({
353
361
  data: e.value,
354
362
  updater: t
355
363
  }) : t(e.value));
@@ -358,10 +366,10 @@ class N {
358
366
  immediate: !0,
359
367
  deep: !0
360
368
  }
361
- ) : D(e) ? S(
369
+ ) : W(e) ? v(
362
370
  () => e,
363
371
  () => {
364
- c.isUndefined(e) || (s && c.isObject(e) ? this.objectParser({
372
+ f.isUndefined(e) || (s && f.isObject(e) ? this.objectParser({
365
373
  data: e,
366
374
  updater: t
367
375
  }) : t(e));
@@ -370,7 +378,7 @@ class N {
370
378
  immediate: !0,
371
379
  deep: !0
372
380
  }
373
- ) : s && c.isObject(e) ? this.objectParser({
381
+ ) : s && f.isObject(e) ? this.objectParser({
374
382
  data: e,
375
383
  updater: t
376
384
  }) : t(e);
@@ -378,81 +386,104 @@ class N {
378
386
  modelProcessor(e) {
379
387
  e.map(
380
388
  (t) => this.createModel(t, this.processedModel.value)
381
- ), c.isObjectEmpty(this.stableModel) && this.stableUpdaterProcessProgress.every(Boolean) && this.defaultValueEffect.effects.size === 0 && (this.stableModel = v(this.processedModel.value), this.runtimeCore.hydrateEffect.triggerEffects(), this.runtimeCore.hydrateEffect.clearEffects());
389
+ ), f.isObjectEmpty(this.stableModel) && this.stableUpdaterProcessProgress.every(Boolean) && this.defaultValueEffect.effects.size === 0 && (this.stableModel = y(this.processedModel.value), this.runtimeCore.hydrateEffect.triggerEffects(), this.runtimeCore.hydrateEffect.clearEffects());
382
390
  }
383
391
  createModel(e, t) {
384
- c.isListSchema(e) && (t[e.field] || (t[e.field] = [{}]), e.children.forEach((s) => {
392
+ f.isListSchema(e) && (t[e.field] || (t[e.field] = [{}]), e.children.forEach((s) => {
385
393
  this.createModel(s, t[e.field][0]);
386
- })), c.isGroupSchema(e) && e.children.forEach((s) => {
394
+ })), f.isGroupSchema(e) && e.children.forEach((s) => {
387
395
  this.createModel(s, t);
388
- }), c.isItemSchema(e) && (t[e.field] = e.defaultValue);
396
+ }), f.isItemSchema(e) && (t[e.field] = e.defaultValue);
389
397
  }
390
398
  }
391
- function M(n) {
392
- return typeof n == "function" || Object.prototype.toString.call(n) === "[object Object]" && !z(n);
399
+ function re(o) {
400
+ return typeof o == "function" || Object.prototype.toString.call(o) === "[object Object]" && !Z(o);
393
401
  }
394
- class $ {
402
+ class ie {
395
403
  constructor(e) {
396
- o(this, "schemas", I([]));
397
- o(this, "model", I({}));
398
- o(this, "processorBySchemaType", {
404
+ l(this, "schemas", C([]));
405
+ l(this, "model", C({}));
406
+ l(this, "processorBySchemaType", {
399
407
  item: this.runtimeItemProcessor.bind(this),
400
408
  group: this.runtimeGroupProcessor.bind(this),
401
409
  list: this.runtimeListProcessor.bind(this)
402
410
  });
403
- o(this, "formRef", I(null));
404
- o(this, "hydrateEffect", new w());
405
- this.setup = e, this.processor = new N(this);
411
+ l(this, "formRef", C(null));
412
+ l(this, "hydrateEffect", new A());
413
+ l(this, "customizedOptions", X({}));
414
+ l(this, "gridProps", {});
415
+ l(this, "globalNativeFormOverride", {
416
+ props: {},
417
+ slots: {}
418
+ });
419
+ this.setup = e, this.processor = new se(this);
406
420
  const t = this.setup(this);
407
421
  this.processor.parseSchemas(t.schemas);
408
422
  }
409
423
  getRuntimeMeta() {
410
424
  return {
411
- model: V(v(this.model.value))
425
+ model: I(y(this.model.value))
412
426
  };
413
427
  }
414
428
  runtimeItemProcessor(e, t, s = this.model.value, r) {
415
- var g;
416
- const i = r ? `${r.field}.${t}.${e.field}` : e.field, l = V(e.component);
417
- if (!l)
429
+ var z, F, M, x, U, D, N, k, R, _, L, B, T;
430
+ p(this.globalNativeFormOverride.props, (F = (z = e.native) == null ? void 0 : z.props) == null ? void 0 : F.Form), p(this.globalNativeFormOverride.slots, (x = (M = e.native) == null ? void 0 : M.slots) == null ? void 0 : x.Form);
431
+ const i = p(y((D = (U = this.customizedOptions.native) == null ? void 0 : U.props) == null ? void 0 : D.FormItem) ?? {}, (k = (N = e.native) == null ? void 0 : N.props) == null ? void 0 : k.FormItem), n = p(y((_ = (R = this.customizedOptions.native) == null ? void 0 : R.slots) == null ? void 0 : _.FormItem) ?? {}, (B = (L = e.native) == null ? void 0 : L.slots) == null ? void 0 : B.FormItem), u = {
432
+ display: "grid",
433
+ gridColumn: "1 / -1",
434
+ ...e.gridProps
435
+ }, a = r ? `${r.field}.${t}.${e.field}` : e.field, d = I(e.component);
436
+ if (!d)
418
437
  return;
419
- const f = l.name, u = e.componentProps ?? {}, d = E.placeholderPresetByComponentName;
420
- let P = e.placeholder;
421
- if (P || (P = `${// @ts-expect-error
422
- d[f] ?? "请输入"}${e.label}`), e.required)
438
+ const O = d.name, V = e.componentProps ?? {}, E = S.placeholderPresetByComponentName;
439
+ let g = e.placeholder;
440
+ if (g || (g = `${// @ts-expect-error
441
+ E[O] ?? "请输入"}${e.label}`), e.required)
423
442
  if (!e.rules)
424
- e.rules = [], (g = e.rules) == null || g.push({
443
+ e.rules = [], (T = e.rules) == null || T.push({
425
444
  required: !0,
426
445
  message: `${e.label}是必填项`
427
446
  });
428
447
  else {
429
- const a = e.rules.findIndex((p) => !!p.required);
430
- e.rules[a].message = `${e.label}是必填项`;
448
+ const j = e.rules.findIndex((H) => !!H.required);
449
+ e.rules[j].message = `${e.label}是必填项`;
431
450
  }
432
- let y = e.show;
433
- return y === void 0 && (y = !0), y || delete s[e.field], h(m.runtimeDoms.Item, null, {
451
+ let m = e.show;
452
+ return m === void 0 && (m = !0), m || delete s[e.field], c("div", {
453
+ style: u
454
+ }, [c(b.runtimeDoms.Item, null, {
434
455
  default() {
435
- return x(h(m.runtimeDoms.FormItem, {
456
+ return q(c(b.runtimeDoms.FormItem, w(i, {
436
457
  label: `${e.label}:`,
437
458
  rules: e.rules,
438
- field: i
439
- }, {
440
- default: () => [h(l, R({
441
- modelValue: s[e.field],
442
- "onUpdate:modelValue": (a) => s[e.field] = a,
443
- placeholder: P
444
- }, u), null)]
445
- }), [[A, y]]);
459
+ field: a
460
+ }), {
461
+ default() {
462
+ return c(d, w({
463
+ modelValue: s[e.field],
464
+ "onUpdate:modelValue": (j) => s[e.field] = j,
465
+ placeholder: g
466
+ }, V), null);
467
+ },
468
+ ...n
469
+ }), [[G, m]]);
446
470
  }
447
- });
471
+ })]);
448
472
  }
449
473
  runtimeGroupProcessor(e) {
450
474
  let t;
451
- return h(m.runtimeDoms.Group, {
475
+ const s = {
476
+ display: "grid",
477
+ gridColumn: "1 / -1",
478
+ ...e.gridProps
479
+ };
480
+ return c("div", {
481
+ style: s
482
+ }, [c(b.runtimeDoms.Group, {
452
483
  schema: e
453
- }, M(t = e.children.map((s) => this.runtimeItemProcessor(s))) ? t : {
484
+ }, re(t = e.children.map((r) => this.runtimeItemProcessor(r))) ? t : {
454
485
  default: () => [t]
455
- });
486
+ })]);
456
487
  }
457
488
  addListItem(e) {
458
489
  var t, s;
@@ -461,59 +492,76 @@ class $ {
461
492
  code: "0001",
462
493
  message: "异步默认值数据正在处理中,请您耐心等待... "
463
494
  });
464
- (s = this.processor.stableModel[e.field]) != null && s[0] && this.model.value[e.field].push(v(this.processor.stableModel[e.field][0])), this.formRef.value.clearValidate();
495
+ (s = this.processor.stableModel[e.field]) != null && s[0] && this.model.value[e.field].push(y(this.processor.stableModel[e.field][0])), this.formRef.value.clearValidate();
465
496
  }
466
497
  deleteListItem(e, t) {
467
498
  this.model.value[e.field].splice(t, 1), this.formRef.value.clearValidate();
468
499
  }
469
500
  runtimeListProcessor(e) {
470
- const t = this;
471
- return t.model.value[e.field] || (t.model.value[e.field] = [{}]), h(m.runtimeDoms.List, {
501
+ const t = {
502
+ display: "grid",
503
+ gridColumn: "1 / -1",
504
+ ...e.gridProps
505
+ }, s = this;
506
+ return s.model.value[e.field] || (s.model.value[e.field] = [{}]), c("div", {
507
+ style: t
508
+ }, [c(b.runtimeDoms.List, {
472
509
  schema: e
473
510
  }, {
474
511
  default() {
475
- return t.model.value[e.field].map((s, r) => h(m.runtimeDoms.ListItem, null, {
512
+ return s.model.value[e.field].map((r, i) => c(b.runtimeDoms.ListItem, null, {
476
513
  default() {
477
- return e.children.map((i) => t.runtimeItemProcessor(i, r, s, e));
514
+ return e.children.map((n) => s.runtimeItemProcessor(n, i, r, e));
478
515
  },
479
516
  delete({
480
- container: i
517
+ container: n
481
518
  } = {}) {
482
- var f;
483
- let l = i ?? h("button", null, null);
484
- return x(h(l, {
485
- onClick: () => t.deleteListItem(e, r)
486
- }, null), [[A, ((f = t.model.value[e.field]) == null ? void 0 : f.length) > 1]]);
519
+ var a;
520
+ let u = n ?? c("button", null, null);
521
+ return q(c(u, {
522
+ onClick: () => s.deleteListItem(e, i)
523
+ }, null), [[G, ((a = s.model.value[e.field]) == null ? void 0 : a.length) > 1]]);
487
524
  }
488
525
  }));
489
526
  },
490
527
  add({
491
- container: s
528
+ container: r
492
529
  } = {}) {
493
- let r = s ?? h("button", null, [L("添加")]);
494
- return h(r, {
495
- onClick: () => t.addListItem(e)
530
+ let i = r ?? c("button", null, [Y("添加")]);
531
+ return c(i, {
532
+ onClick: () => s.addListItem(e)
496
533
  }, null);
497
534
  }
498
- });
535
+ })]);
499
536
  }
500
537
  runtimeProcessor(e) {
501
538
  return e.map((t) => (t.type || (t.type = "item"), this.processorBySchemaType[t.type](t)));
502
539
  }
503
540
  exec() {
504
- let e;
505
- return h(m.runtimeDoms.Form, {
541
+ var i, n, u, a;
542
+ const e = {
543
+ display: "grid",
544
+ gridColumn: "1 / -1",
545
+ gridAutoColumns: "1fr",
546
+ ...this.gridProps
547
+ }, t = this, s = p(y((n = (i = this.customizedOptions.native) == null ? void 0 : i.props) == null ? void 0 : n.Form) ?? {}, this.globalNativeFormOverride.props), r = p(y((a = (u = this.customizedOptions.native) == null ? void 0 : u.slots) == null ? void 0 : a.Form) ?? {}, this.globalNativeFormOverride.slots);
548
+ return c(b.runtimeDoms.Form, w(s, {
506
549
  ref: this.formRef,
507
550
  model: this.model.value
508
- }, M(e = this.runtimeProcessor(this.schemas.value)) ? e : {
509
- default: () => [e]
551
+ }), {
552
+ default() {
553
+ return c("div", {
554
+ style: e
555
+ }, [t.runtimeProcessor(t.schemas.value)]);
556
+ },
557
+ ...r
510
558
  });
511
559
  }
512
560
  }
513
- class m {
561
+ class b {
514
562
  }
515
- o(m, "runtimeDoms");
516
- const b = class b {
563
+ l(b, "runtimeDoms");
564
+ const P = class P {
517
565
  static getPlaceholderPrefixPresetByComponentName() {
518
566
  const e = {
519
567
  请选择: ["Select", "Tree", "TreeSelect"],
@@ -526,7 +574,7 @@ const b = class b {
526
574
  return t;
527
575
  }
528
576
  };
529
- o(b, "schemaPreset", {
577
+ l(P, "schemaPreset", {
530
578
  type: {
531
579
  defaultValue: "item"
532
580
  },
@@ -559,52 +607,55 @@ o(b, "schemaPreset", {
559
607
  },
560
608
  children: {
561
609
  defaultValue: []
562
- }
563
- }), o(b, "componentPropsPreset", {
610
+ },
611
+ native: void 0,
612
+ gridProps: void 0
613
+ }), l(P, "componentPropsPreset", {
564
614
  options: {
565
615
  defaultValue: []
566
616
  }
567
617
  }), // 基于基本功能提出基本预设
568
- o(b, "placeholderPresetByComponentName", b.getPlaceholderPrefixPresetByComponentName());
569
- let E = b;
570
- const G = /* @__PURE__ */ B({
618
+ l(P, "placeholderPresetByComponentName", P.getPlaceholderPrefixPresetByComponentName());
619
+ let S = P;
620
+ const le = /* @__PURE__ */ ee({
571
621
  props: {
572
622
  setup: {
573
623
  type: Function,
574
624
  required: !0
575
625
  }
576
626
  },
577
- setup(n) {
578
- const e = new $(n.setup);
627
+ setup(o) {
628
+ const e = new ie(o.setup);
579
629
  return () => e.exec();
580
630
  }
581
631
  });
582
- function K(n) {
583
- const e = new T(n);
632
+ function ae(o) {
633
+ const e = new te(o);
584
634
  return [
585
635
  e.setup.bind(e),
586
636
  {
587
637
  submit: e.submit.bind(e),
588
- hydrate: e.hydrate.bind(e)
638
+ hydrate: e.hydrate.bind(e),
639
+ customize: e.customize.bind(e)
589
640
  }
590
641
  ];
591
642
  }
592
- function W(n) {
643
+ function fe(o) {
593
644
  return {
594
645
  install() {
595
- m.runtimeDoms = n;
646
+ b.runtimeDoms = o;
596
647
  }
597
648
  };
598
649
  }
599
- function H(n, e) {
600
- return e === "raw" && Object.defineProperty(n, "name", {
601
- value: `__proform_raw_${n.name}`,
650
+ function ue(o, e) {
651
+ return e === "raw" && Object.defineProperty(o, "name", {
652
+ value: `__proform_raw_${o.name}`,
602
653
  writable: !0
603
- }), n;
654
+ }), o;
604
655
  }
605
656
  export {
606
- G as ProForm,
607
- K as useForm,
608
- W as useFormRenderer,
609
- H as useModifiers
657
+ le as ProForm,
658
+ ae as useForm,
659
+ fe as useFormRenderer,
660
+ ue as useModifiers
610
661
  };
@@ -1,4 +1,4 @@
1
- import { AnyObject, FormCustomization } from "../types";
1
+ import { AnyObject, CustomizationOptions, FormCustomization } from "../types";
2
2
  import { RuntimeCore } from "./index";
3
3
  export default class FormCustomizer {
4
4
  formCustomization: FormCustomization;
@@ -7,5 +7,6 @@ export default class FormCustomizer {
7
7
  constructor(formCustomization: FormCustomization);
8
8
  setup(_runtimeCore: RuntimeCore): FormCustomization;
9
9
  submit(): Promise<AnyObject>;
10
- hydrate(data: AnyObject): void;
10
+ hydrate(data: AnyObject): Promise<never> | undefined;
11
+ customize(options: Partial<CustomizationOptions>): void;
11
12
  }
@@ -1,5 +1,5 @@
1
1
  import { Ref } from "vue";
2
- import { Setup, Schema, AnyObject, ItemSchema, GroupSchema, ListSchema, ProcessorBySchemaType } from "../types";
2
+ import { Setup, Schema, AnyObject, ItemSchema, GroupSchema, ListSchema, ProcessorBySchemaType, CustomizationOptions } from "../types";
3
3
  import Processor from "./Processor";
4
4
  import Effect from "./Effect";
5
5
  export default class RuntimeCore {
@@ -10,6 +10,12 @@ export default class RuntimeCore {
10
10
  processorBySchemaType: ProcessorBySchemaType;
11
11
  formRef: Ref<AnyObject>;
12
12
  hydrateEffect: Effect;
13
+ customizedOptions: CustomizationOptions;
14
+ gridProps: {};
15
+ globalNativeFormOverride: {
16
+ props: {};
17
+ slots: {};
18
+ };
13
19
  constructor(setup: Setup);
14
20
  getRuntimeMeta(): {
15
21
  model: AnyObject;
@@ -23,12 +23,43 @@ export type FieldRule<T = any> = {
23
23
  hasKeys?: string[];
24
24
  validator?: (value: T | undefined, callback: (error?: string) => void) => void;
25
25
  };
26
+ interface GridStyle {
27
+ gridTemplateColumns?: string;
28
+ gridTemplateRows?: string;
29
+ gridTemplateAreas?: string;
30
+ gridTemplate?: string;
31
+ gridColumnGap?: string;
32
+ gridRowGap?: string;
33
+ gridGap?: string;
34
+ justifyItems?: string;
35
+ alignItems?: string;
36
+ placeItems?: string;
37
+ justifyContent?: string;
38
+ alignContent?: string;
39
+ placeContent?: string;
40
+ gridAutoColumns?: string;
41
+ gridAutoRows?: string;
42
+ gridAutoFlow?: string;
43
+ gridColumnStart?: string;
44
+ gridColumnEnd?: string;
45
+ gridRowStart?: string;
46
+ gridRowEnd?: string;
47
+ gridColumn?: string;
48
+ gridRow?: string;
49
+ gridArea?: string;
50
+ justifySelf?: string;
51
+ alignSelf?: string;
52
+ placeSelf?: string;
53
+ [key: string]: any;
54
+ }
26
55
  export interface ItemSchema {
27
56
  type?: "item";
28
57
  rules?: FieldRule[];
29
58
  show?: boolean;
30
59
  required?: boolean;
31
60
  placeholder?: string;
61
+ native?: NativeCustomizationOptions;
62
+ gridProps?: GridStyle;
32
63
  label: string;
33
64
  field: string;
34
65
  component: DomType;
@@ -39,12 +70,14 @@ export interface GroupSchema {
39
70
  type: "group";
40
71
  label: string;
41
72
  children: ProxyedSchema[];
73
+ gridProps?: GridStyle;
42
74
  }
43
75
  export interface ListSchema {
44
76
  type: "list";
45
77
  field: string;
46
78
  label: string;
47
79
  children: ProxyedSchema[];
80
+ gridProps?: GridStyle;
48
81
  }
49
82
  export type Schema = ItemSchema | GroupSchema | ListSchema;
50
83
  export interface runtimeMeta {
@@ -56,5 +89,20 @@ export type ProFormProxy<T> = {
56
89
  };
57
90
  export type ProxyedSchema = ProFormProxy<ItemSchema | GroupSchema | ListSchema>;
58
91
  export interface FormCustomization {
92
+ gridProps?: GridStyle;
59
93
  schemas: ProxyedSchema[];
60
94
  }
95
+ export type NativeCustomizationOptions = {
96
+ props?: {
97
+ Form?: AnyObject;
98
+ FormItem?: AnyObject;
99
+ };
100
+ slots?: {
101
+ Form?: AnyObject;
102
+ FormItem?: AnyObject;
103
+ };
104
+ };
105
+ export type CustomizationOptions = {
106
+ native?: NativeCustomizationOptions;
107
+ };
108
+ export {};
@@ -1,4 +1,4 @@
1
- import { AnyObject } from "../types";
1
+ import { AnyObject, CustomizationOptions } from "../types";
2
2
  import type { RuntimeCore } from "../services";
3
3
  export type Setup = (runtimeCore: RuntimeCore) => any;
4
4
  export type UseForm = [
@@ -6,5 +6,6 @@ export type UseForm = [
6
6
  {
7
7
  submit: () => Promise<AnyObject>;
8
8
  hydrate: (data: AnyObject) => any;
9
+ customize: (options: CustomizationOptions) => any;
9
10
  }
10
11
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@harbor-design/proform",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",