@harbor-design/proform 1.0.2 → 1.0.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @harbor-design/proform
2
2
 
3
+ ## 1.0.3
4
+
5
+ ### Patch Changes
6
+
7
+ - fix error dist bug
8
+
3
9
  ## 1.0.2
4
10
 
5
11
  ### Patch Changes
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";var b=Object.defineProperty;var k=(u,e,t)=>e in u?b(u,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[e]=t;var m=(u,e,t)=>(k(u,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("vue");class R{constructor(e){m(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(i=>i?t(i):e(this.cleanFallbackFields(l.toRaw(this.runtimeCore.processors.processedModel.value))))})}}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).includes("Function")}static isAsyncFunction(e){let t=!1;return this.typeChecker(e)==="[object AsyncFunction]"&&(t=!0),t}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"}}function V(u){const e=new WeakMap;function t(i){if(i===null||typeof i!="object")return i;if(i instanceof Date)return new Date(i);if(i instanceof RegExp)return new RegExp(i);if(i instanceof Map){const o=new Map;for(let[s,f]of i)o.set(t(s),t(f));return o}if(i instanceof Set){const o=new Set;for(let s of i)o.add(t(s));return o}if(e.has(i))return e.get(i);if(Array.isArray(i)){const o=[];e.set(i,o);for(let s=0;s<i.length;s++)o[s]=t(i[s]);return o}const c=Object.create(Object.getPrototypeOf(i));e.set(i,c);for(let o in i)i.hasOwnProperty(o)&&(c[o]=t(i[o]));return c}return t(u)}class W{constructor(e,t){m(this,"rawSchemas",[]);m(this,"rawModel",{});m(this,"schemaPreset",{type:{defaultValueWhenAsync:"item"},component:{defaultValueWhenAsync:void 0},componentProps:{defaultValueWhenAsync:void 0},defaultValue:{defaultValueWhenAsync:void 0},label:{defaultValueWhenAsync:""},field:{defaultValueWhenAsync:"__yiwwhl_async_field_fallback"},rules:{defaultValueWhenAsync:[]},show:{defaultValueWhenAsync:!0}});m(this,"componentPropsPreset",{options:{defaultValueWhenAsync:[]}});m(this,"uniqueEffectMap",{});m(this,"schemaEffect",new F);m(this,"modelEffect",new F);m(this,"stopWatchEffect",new F);this.processedSchemas=e,this.processedModel=t,l.watch(()=>this.processedModel.value,()=>{this.schemaEffect.triggerEffects(),this.modelEffect.triggerEffects()},{deep:!0})}schemaAnalyzer(e,t=this.processedSchemas.value,i=this.rawSchemas,c,o){for(let s=0;s<e.length;s++){let f=e[s];this.schemaProcessor(f,s,(p,v,y)=>{y?i[s][y]=p[y]:(t[s]=p,this.modelProcessor(p,c&&this.processedModel.value[c][0]),(!i[s]||v)&&(i[s]=V(p)),this.schemaEffect.triggerEffects(),this.modelEffect.triggerEffects())},o,c)}}schemaProcessor(e,t,i,c,o){const s={},f=this;function p(v=!1){var y,w;if(s.componentProps){const h={};f.propsProcessor(s.componentProps,f.componentPropsPreset,h,E=>{s.componentProps=h,i({...s},E,"componentProps")},t,c);return}if(s.children){f.processedSchemas.value[t]=s,f.rawSchemas[t]=s,i({...s},v),f.schemaAnalyzer(s.children,(y=f.processedSchemas.value[t])==null?void 0:y.children,(w=f.rawSchemas[t])==null?void 0:w.children,s.field,t);return}i({...s},v),f.rawModel=V(f.processedModel.value)}this.propsProcessor(e,this.schemaPreset,s,p,t,c,o)}replaceFunctionsWithUndefined(e){if(typeof e!="object"||e===null)return e;for(let t in e)if(e.hasOwnProperty(t)){let i=e[t];typeof i=="function"||typeof i=="object"&&this.replaceFunctionsWithUndefined(i)}return e}runtimeMeta(){return{model:this.replaceFunctionsWithUndefined(l.toRaw(V(this.processedModel.value)))}}propsProcessor(e,t,i,c,o,s,f){const p=Object.keys(e),v=Array.from({length:p.length}).fill(!1);function y(){return v.every(w=>w)}for(let w=0;w<p.length;w++){const h=p[w],E=e[h];if(a.isFunction(E)){const P=E(this.runtimeMeta());h!=="defaultValue"?this.schemaEffect.trackEffect(()=>{let n=E(this.runtimeMeta());n instanceof Promise?n.then(r=>{s===void 0?a.isFunction(r)||(typeof r=="string"&&r.includes("undefined")&&(r=r.replace(/undefined/g,"")),this.processedSchemas.value[o][h]=r):a.isFunction(r)||(typeof r=="string"&&r.includes("undefined")&&(r=r.replace(/undefined/g,"")),this.processedSchemas.value[s].children[o][h]=r)}):s===void 0?a.isFunction(n)||(typeof n=="string"&&n.includes("undefined")&&(n=n.replace(/undefined/g,"")),this.processedSchemas.value[o][h]=n):a.isFunction(n)||(typeof n=="string"&&n.includes("undefined")&&(n=n.replace(/undefined/g,"")),this.processedSchemas.value[s].children[o][h]=n)}):this.modelEffect.trackEffect(()=>{let n=E(this.runtimeMeta());this.stopWatchEffect.trackEffect(l.watchEffect(()=>{if(n=E(this.runtimeMeta()),n instanceof Promise)n.then(r=>{if(typeof r=="string"&&!r.includes("undefined")?this.stopWatchEffect.triggerEffects():r=r.replace(/undefined/g,""),o===void 0||f===void 0){if(a.isFunction(e.field)){const d=e.field(this.runtimeMeta());d instanceof Promise?d.then(g=>{this.processedModel.value[g]=r}):this.processedModel.value[d]=r;return}this.processedModel.value[e.field]=r}else{if(a.isFunction(e.field)){const d=e.field(this.runtimeMeta());d instanceof Promise?d.then(g=>{this.processedModel.value[f][o][g]=r}):this.processedModel.value[f][o]&&(this.processedModel.value[f][o][d]=r);return}this.processedModel.value[f]&&this.processedModel.value[f].forEach(d=>{d[e.field]=r})}this.rawModel=V(this.processedModel.value),this.modelEffect.clearEffects()});else{if(typeof n=="string"&&!n.includes("undefined")?this.stopWatchEffect.triggerEffects():n=n.replace(/undefined/g,""),a.isFunction(e.field)){const r=e.field(this.runtimeMeta());r instanceof Promise?r.then(d=>{this.processedModel.value[d]=n}):this.processedModel.value[r]=n;return}if(this.processedModel.value[e.field]=n,o===void 0||f===void 0){if(a.isFunction(e.field)){const r=e.field(this.runtimeMeta());r instanceof Promise?r.then(d=>{this.processedModel.value[d]=n}):this.processedModel.value[r]=n;return}this.processedModel.value[e.field]=n}else for(let r=0;r<this.processedModel.value[f].length;r++){const d=this.processedModel.value[f][r];if(!a.isFunction(e.field))d[e.field]=n;else{const g=e.field(this.runtimeMeta());g instanceof Promise?g.then(S=>{d[S]=n}):d[g]=n;return}}this.rawModel=V(this.processedModel.value),this.modelEffect.clearEffects()}}))}),P instanceof Promise?(v[w]=!0,i[h]=t[h].defaultValueWhenAsync,y()&&c(),P.then(n=>{v[w]=!0,i[h]=n,y()&&c(!0)})):(v[w]=!0,i[h]=P,y()&&c())}else v[w]=!0,l.isRef(E)?l.watch(()=>E.value,P=>{i[h]=P,y()&&c()},{immediate:!0,deep:!0}):l.isReactive(E)?l.watch(()=>E,P=>{i[h]=P,y()&&c()},{immediate:!0,deep:!0}):(i[h]=E,y()&&c())}}modelProcessor(e,t=this.processedModel.value){if(a.isListSchema(e)){if(a.isFunction(e.field))return;t[e.field]||(t[e.field]=[{}]),e.children.forEach(i=>{this.modelProcessor(i,t[e.field][0])});return}if(a.isGroupSchema(e)){e.children.forEach(i=>{this.modelProcessor(i,t)});return}if(a.isItemSchema(e)){if(a.isFunction(e.field)||a.isUndefined(e.field)||!Number.isNaN(Number(e.field))||a.isFunction(e.defaultValue))return;t[e.field]=e.defaultValue}}}function A(u){return typeof u=="function"||Object.prototype.toString.call(u)==="[object Object]"&&!l.isVNode(u)}class N{constructor(e){m(this,"schemas",l.ref([]));m(this,"model",l.ref({}));m(this,"processorBySchemaType",{item:this.runtimeItemProcessor.bind(this),group:this.runtimeGroupProcessor.bind(this),list:this.runtimeListProcessor.bind(this)});m(this,"formRef",l.ref(null));this.setup=e,this.processors=new W(this.schemas,this.model),this.analyze(this.setup(this))}analyze(e){this.processors.schemaAnalyzer(e.schemas)}runtimeItemProcessor(e,t,i=this.model.value,c){const o=c?`${c.field}.${t}.${e.field}`:e.field,s=l.toRaw(e.component),f=e.componentProps??{};let p=e.show;return p===void 0&&(p=!0),p||delete i[e.field],l.createVNode(M.runtimeDoms.Item,null,{default(){return l.withDirectives(l.createVNode(M.runtimeDoms.FormItem,{label:`${e.label}:`,rules:e.rules,field:o},{default:()=>[l.createVNode(s,l.mergeProps({modelValue:i[e.field],"onUpdate:modelValue":v=>i[e.field]=v},f),null)]}),[[l.vShow,p]])}})}runtimeGroupProcessor(e){let t;return l.createVNode(M.runtimeDoms.Group,{schema:e},A(t=e.children.map(i=>this.runtimeItemProcessor(i)))?t:{default:()=>[t]})}addListItem(e){var t,i;if(console.log(this.processors.rawModel,e.field),!((t=this.processors.rawModel[e.field])!=null&&t[0]))return Promise.reject({code:"0001",message:"异步默认值数据正在处理中,请您耐心等待... "});(i=this.processors.rawModel[e.field])!=null&&i[0]&&this.model.value[e.field].push(V(this.processors.rawModel[e.field][0]))}deleteListItem(e,t){this.model.value[e.field].splice(t,1)}runtimeListProcessor(e){const t=this;return l.createVNode(M.runtimeDoms.List,{schema:e},{default(){return t.model.value[e.field].map((i,c)=>l.createVNode(M.runtimeDoms.ListItem,null,{default(){return e.children.map((o,s)=>t.runtimeItemProcessor(o,s,i,e))},delete({container:o}={}){var f;let s=o??l.createVNode("button",null,null);return l.withDirectives(l.createVNode(s,{onClick:()=>t.deleteListItem(e,c)},null),[[l.vShow,((f=t.model.value[e.field])==null?void 0:f.length)>1]])}}))},add({container:i}={}){let c=i??l.createVNode("button",null,[l.createTextVNode("添加")]);return l.createVNode(c,{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 l.createVNode(M.runtimeDoms.Form,{ref:this.formRef,model:this.model.value},A(e=this.runtimeProcessor(this.schemas.value))?e:{default:()=>[e]})}}class M{}m(M,"runtimeDoms");class F{constructor(){m(this,"effects",new Set)}clearEffects(){this.effects.clear()}triggerEffects(){Array.from(this.effects).forEach(e=>e())}trackEffect(e){this.effects.add(e)}}const D=l.defineComponent({props:{setup:{type:Function,required:!0}},setup(u){const e=new N(u.setup);return()=>e.exec()}});function C(u){const e=new R(u);return[e.setup.bind(e),{submit:e.submit.bind(e)}]}function _(u){return{install(){M.runtimeDoms=u}}}exports.ProForm=D;exports.useForm=C;exports.useFormRenderer=_;
package/dist/index.js ADDED
@@ -0,0 +1,509 @@
1
+ var D = Object.defineProperty;
2
+ var _ = (c, e, t) => e in c ? D(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
3
+ var h = (c, e, t) => (_(c, typeof e != "symbol" ? e + "" : e, t), t);
4
+ import { toRaw as S, watch as F, watchEffect as j, isRef as L, isReactive as z, ref as b, createVNode as v, withDirectives as V, mergeProps as N, vShow as W, createTextVNode as G, isVNode as U, defineComponent as $ } from "vue";
5
+ class B {
6
+ constructor(e) {
7
+ h(this, "runtimeCore");
8
+ this.formCustomization = e;
9
+ }
10
+ // happy path, 后续可以完善更多的 fallback 处理,fallback 处理是为了不卡住异步时的首次渲染做的优化
11
+ cleanFallbackFields(e) {
12
+ return e !== null && typeof e == "object" && (delete e.__yiwwhl_async_field_fallback, Object.values(e).forEach((t) => {
13
+ this.cleanFallbackFields(t);
14
+ })), e;
15
+ }
16
+ setup(e) {
17
+ return this.runtimeCore = e, this.formCustomization;
18
+ }
19
+ submit() {
20
+ return new Promise((e, t) => {
21
+ this.runtimeCore.formRef.value.validate((i) => i ? t(i) : e(
22
+ this.cleanFallbackFields(
23
+ S(this.runtimeCore.processors.processedModel.value)
24
+ )
25
+ ));
26
+ });
27
+ }
28
+ }
29
+ class d {
30
+ static typeChecker(e) {
31
+ return {}.toString.call(e);
32
+ }
33
+ static isArray(e) {
34
+ return this.typeChecker(e) === "[object Array]";
35
+ }
36
+ static isFunction(e) {
37
+ return this.typeChecker(e).includes("Function");
38
+ }
39
+ static isAsyncFunction(e) {
40
+ let t = !1;
41
+ return this.typeChecker(e) === "[object AsyncFunction]" && (t = !0), t;
42
+ }
43
+ static isUndefined(e) {
44
+ return e === void 0;
45
+ }
46
+ static isArrayEmpty(e) {
47
+ return (e == null ? void 0 : e.length) < 1;
48
+ }
49
+ static isObjectEmpty(e) {
50
+ return this.isArrayEmpty(Object.keys(e));
51
+ }
52
+ static isListSchema(e) {
53
+ return e.type === "list";
54
+ }
55
+ static isGroupSchema(e) {
56
+ return e.type === "group";
57
+ }
58
+ static isItemSchema(e) {
59
+ return this.isUndefined(e.type) || e.type === "item";
60
+ }
61
+ }
62
+ function A(c) {
63
+ const e = /* @__PURE__ */ new WeakMap();
64
+ function t(i) {
65
+ if (i === null || typeof i != "object")
66
+ return i;
67
+ if (i instanceof Date)
68
+ return new Date(i);
69
+ if (i instanceof RegExp)
70
+ return new RegExp(i);
71
+ if (i instanceof Map) {
72
+ const o = /* @__PURE__ */ new Map();
73
+ for (let [s, l] of i)
74
+ o.set(t(s), t(l));
75
+ return o;
76
+ }
77
+ if (i instanceof Set) {
78
+ const o = /* @__PURE__ */ new Set();
79
+ for (let s of i)
80
+ o.add(t(s));
81
+ return o;
82
+ }
83
+ if (e.has(i))
84
+ return e.get(i);
85
+ if (Array.isArray(i)) {
86
+ const o = [];
87
+ e.set(i, o);
88
+ for (let s = 0; s < i.length; s++)
89
+ o[s] = t(i[s]);
90
+ return o;
91
+ }
92
+ const f = Object.create(Object.getPrototypeOf(i));
93
+ e.set(i, f);
94
+ for (let o in i)
95
+ i.hasOwnProperty(o) && (f[o] = t(i[o]));
96
+ return f;
97
+ }
98
+ return t(c);
99
+ }
100
+ class O {
101
+ constructor(e, t) {
102
+ h(this, "rawSchemas", []);
103
+ h(this, "rawModel", {});
104
+ h(this, "schemaPreset", {
105
+ type: {
106
+ defaultValueWhenAsync: "item"
107
+ },
108
+ component: {
109
+ defaultValueWhenAsync: void 0
110
+ },
111
+ componentProps: {
112
+ defaultValueWhenAsync: void 0
113
+ },
114
+ defaultValue: {
115
+ defaultValueWhenAsync: void 0
116
+ },
117
+ label: {
118
+ defaultValueWhenAsync: ""
119
+ },
120
+ field: {
121
+ defaultValueWhenAsync: "__yiwwhl_async_field_fallback"
122
+ },
123
+ rules: {
124
+ defaultValueWhenAsync: []
125
+ },
126
+ show: {
127
+ defaultValueWhenAsync: !0
128
+ }
129
+ });
130
+ h(this, "componentPropsPreset", {
131
+ options: {
132
+ defaultValueWhenAsync: []
133
+ }
134
+ });
135
+ h(this, "uniqueEffectMap", {});
136
+ h(this, "schemaEffect", new k());
137
+ h(this, "modelEffect", new k());
138
+ h(this, "stopWatchEffect", new k());
139
+ this.processedSchemas = e, this.processedModel = t, F(
140
+ () => this.processedModel.value,
141
+ () => {
142
+ this.schemaEffect.triggerEffects(), this.modelEffect.triggerEffects();
143
+ },
144
+ {
145
+ deep: !0
146
+ }
147
+ );
148
+ }
149
+ schemaAnalyzer(e, t = this.processedSchemas.value, i = this.rawSchemas, f, o) {
150
+ for (let s = 0; s < e.length; s++) {
151
+ let l = e[s];
152
+ this.schemaProcessor(
153
+ l,
154
+ s,
155
+ (p, y, m) => {
156
+ m ? i[s][m] = p[m] : (t[s] = p, this.modelProcessor(
157
+ p,
158
+ f && this.processedModel.value[f][0]
159
+ ), (!i[s] || y) && (i[s] = A(p)), this.schemaEffect.triggerEffects(), this.modelEffect.triggerEffects());
160
+ },
161
+ o,
162
+ f
163
+ );
164
+ }
165
+ }
166
+ schemaProcessor(e, t, i, f, o) {
167
+ const s = {}, l = this;
168
+ function p(y = !1) {
169
+ var m, E;
170
+ if (s.componentProps) {
171
+ const a = {};
172
+ l.propsProcessor(
173
+ s.componentProps,
174
+ l.componentPropsPreset,
175
+ a,
176
+ (M) => {
177
+ s.componentProps = a, i({ ...s }, M, "componentProps");
178
+ },
179
+ t,
180
+ f
181
+ );
182
+ return;
183
+ }
184
+ if (s.children) {
185
+ l.processedSchemas.value[t] = s, l.rawSchemas[t] = s, i({ ...s }, y), l.schemaAnalyzer(
186
+ s.children,
187
+ // @ts-expect-error 此处已经守卫为非 ItemSchema
188
+ (m = l.processedSchemas.value[t]) == null ? void 0 : m.children,
189
+ // @ts-expect-error 此处已经守卫为非 ItemSchema
190
+ (E = l.rawSchemas[t]) == null ? void 0 : E.children,
191
+ s.field,
192
+ t
193
+ );
194
+ return;
195
+ }
196
+ i({ ...s }, y), l.rawModel = A(l.processedModel.value);
197
+ }
198
+ this.propsProcessor(
199
+ e,
200
+ this.schemaPreset,
201
+ s,
202
+ p,
203
+ t,
204
+ f,
205
+ o
206
+ );
207
+ }
208
+ replaceFunctionsWithUndefined(e) {
209
+ if (typeof e != "object" || e === null)
210
+ return e;
211
+ for (let t in e)
212
+ if (e.hasOwnProperty(t)) {
213
+ let i = e[t];
214
+ typeof i == "function" || typeof i == "object" && this.replaceFunctionsWithUndefined(i);
215
+ }
216
+ return e;
217
+ }
218
+ runtimeMeta() {
219
+ return {
220
+ model: this.replaceFunctionsWithUndefined(
221
+ S(A(this.processedModel.value))
222
+ )
223
+ };
224
+ }
225
+ propsProcessor(e, t, i, f, o, s, l) {
226
+ const p = Object.keys(e), y = Array.from({
227
+ length: p.length
228
+ }).fill(!1);
229
+ function m() {
230
+ return y.every((E) => E);
231
+ }
232
+ for (let E = 0; E < p.length; E++) {
233
+ const a = p[E], M = e[a];
234
+ if (d.isFunction(M)) {
235
+ const P = M(this.runtimeMeta());
236
+ a !== "defaultValue" ? this.schemaEffect.trackEffect(() => {
237
+ let n = M(this.runtimeMeta());
238
+ n instanceof Promise ? n.then((r) => {
239
+ s === void 0 ? d.isFunction(r) || (typeof r == "string" && r.includes("undefined") && (r = r.replace(/undefined/g, "")), this.processedSchemas.value[o][a] = r) : d.isFunction(r) || (typeof r == "string" && r.includes("undefined") && (r = r.replace(/undefined/g, "")), this.processedSchemas.value[s].children[o][a] = r);
240
+ }) : s === void 0 ? d.isFunction(n) || (typeof n == "string" && n.includes("undefined") && (n = n.replace(/undefined/g, "")), this.processedSchemas.value[o][a] = n) : d.isFunction(n) || (typeof n == "string" && n.includes("undefined") && (n = n.replace(/undefined/g, "")), this.processedSchemas.value[s].children[o][a] = n);
241
+ }) : this.modelEffect.trackEffect(() => {
242
+ let n = M(this.runtimeMeta());
243
+ this.stopWatchEffect.trackEffect(
244
+ j(() => {
245
+ if (n = M(this.runtimeMeta()), n instanceof Promise)
246
+ n.then((r) => {
247
+ if (typeof r == "string" && !r.includes("undefined") ? this.stopWatchEffect.triggerEffects() : r = r.replace(/undefined/g, ""), o === void 0 || l === void 0) {
248
+ if (d.isFunction(e.field)) {
249
+ const u = e.field(
250
+ this.runtimeMeta()
251
+ );
252
+ u instanceof Promise ? u.then((g) => {
253
+ this.processedModel.value[g] = r;
254
+ }) : this.processedModel.value[u] = r;
255
+ return;
256
+ }
257
+ this.processedModel.value[e.field] = r;
258
+ } else {
259
+ if (d.isFunction(e.field)) {
260
+ const u = e.field(
261
+ this.runtimeMeta()
262
+ );
263
+ u instanceof Promise ? u.then((g) => {
264
+ this.processedModel.value[l][o][g] = r;
265
+ }) : this.processedModel.value[l][o] && (this.processedModel.value[l][o][u] = r);
266
+ return;
267
+ }
268
+ this.processedModel.value[l] && this.processedModel.value[l].forEach(
269
+ // @ts-expect-error
270
+ (u) => {
271
+ u[e.field] = r;
272
+ }
273
+ );
274
+ }
275
+ this.rawModel = A(this.processedModel.value), this.modelEffect.clearEffects();
276
+ });
277
+ else {
278
+ if (typeof n == "string" && !n.includes("undefined") ? this.stopWatchEffect.triggerEffects() : n = n.replace(/undefined/g, ""), d.isFunction(e.field)) {
279
+ const r = e.field(this.runtimeMeta());
280
+ r instanceof Promise ? r.then((u) => {
281
+ this.processedModel.value[u] = n;
282
+ }) : this.processedModel.value[r] = n;
283
+ return;
284
+ }
285
+ if (this.processedModel.value[e.field] = n, o === void 0 || l === void 0) {
286
+ if (d.isFunction(e.field)) {
287
+ const r = e.field(this.runtimeMeta());
288
+ r instanceof Promise ? r.then((u) => {
289
+ this.processedModel.value[u] = n;
290
+ }) : this.processedModel.value[r] = n;
291
+ return;
292
+ }
293
+ this.processedModel.value[e.field] = n;
294
+ } else
295
+ for (let r = 0; r < this.processedModel.value[l].length; r++) {
296
+ const u = this.processedModel.value[l][r];
297
+ if (!d.isFunction(e.field))
298
+ u[e.field] = n;
299
+ else {
300
+ const g = e.field(
301
+ this.runtimeMeta()
302
+ );
303
+ g instanceof Promise ? g.then((C) => {
304
+ u[C] = n;
305
+ }) : u[g] = n;
306
+ return;
307
+ }
308
+ }
309
+ this.rawModel = A(this.processedModel.value), this.modelEffect.clearEffects();
310
+ }
311
+ })
312
+ );
313
+ }), P instanceof Promise ? (y[E] = !0, i[a] = t[a].defaultValueWhenAsync, m() && f(), P.then((n) => {
314
+ y[E] = !0, i[a] = n, m() && f(!0);
315
+ })) : (y[E] = !0, i[a] = P, m() && f());
316
+ } else
317
+ y[E] = !0, L(M) ? F(
318
+ () => M.value,
319
+ (P) => {
320
+ i[a] = P, m() && f();
321
+ },
322
+ {
323
+ immediate: !0,
324
+ deep: !0
325
+ }
326
+ ) : z(M) ? F(
327
+ () => M,
328
+ (P) => {
329
+ i[a] = P, m() && f();
330
+ },
331
+ {
332
+ immediate: !0,
333
+ deep: !0
334
+ }
335
+ ) : (i[a] = M, m() && f());
336
+ }
337
+ }
338
+ modelProcessor(e, t = this.processedModel.value) {
339
+ if (d.isListSchema(e)) {
340
+ if (d.isFunction(e.field))
341
+ return;
342
+ t[e.field] || (t[e.field] = [{}]), e.children.forEach((i) => {
343
+ this.modelProcessor(i, t[e.field][0]);
344
+ });
345
+ return;
346
+ }
347
+ if (d.isGroupSchema(e)) {
348
+ e.children.forEach((i) => {
349
+ this.modelProcessor(i, t);
350
+ });
351
+ return;
352
+ }
353
+ if (d.isItemSchema(e)) {
354
+ if (d.isFunction(e.field) || d.isUndefined(e.field) || !Number.isNaN(Number(e.field)) || d.isFunction(e.defaultValue))
355
+ return;
356
+ t[e.field] = e.defaultValue;
357
+ }
358
+ }
359
+ }
360
+ function R(c) {
361
+ return typeof c == "function" || Object.prototype.toString.call(c) === "[object Object]" && !U(c);
362
+ }
363
+ class T {
364
+ constructor(e) {
365
+ h(this, "schemas", b([]));
366
+ h(this, "model", b({}));
367
+ h(this, "processorBySchemaType", {
368
+ item: this.runtimeItemProcessor.bind(this),
369
+ group: this.runtimeGroupProcessor.bind(this),
370
+ list: this.runtimeListProcessor.bind(this)
371
+ });
372
+ h(this, "formRef", b(null));
373
+ this.setup = e, this.processors = new O(this.schemas, this.model), this.analyze(this.setup(this));
374
+ }
375
+ analyze(e) {
376
+ this.processors.schemaAnalyzer(e.schemas);
377
+ }
378
+ runtimeItemProcessor(e, t, i = this.model.value, f) {
379
+ const o = f ? `${f.field}.${t}.${e.field}` : e.field, s = S(e.component), l = e.componentProps ?? {};
380
+ let p = e.show;
381
+ return p === void 0 && (p = !0), p || delete i[e.field], v(w.runtimeDoms.Item, null, {
382
+ default() {
383
+ return V(v(w.runtimeDoms.FormItem, {
384
+ label: `${e.label}:`,
385
+ rules: e.rules,
386
+ field: o
387
+ }, {
388
+ default: () => [v(s, N({
389
+ modelValue: i[e.field],
390
+ "onUpdate:modelValue": (y) => i[e.field] = y
391
+ }, l), null)]
392
+ }), [[W, p]]);
393
+ }
394
+ });
395
+ }
396
+ runtimeGroupProcessor(e) {
397
+ let t;
398
+ return v(w.runtimeDoms.Group, {
399
+ schema: e
400
+ }, R(t = e.children.map((i) => this.runtimeItemProcessor(i))) ? t : {
401
+ default: () => [t]
402
+ });
403
+ }
404
+ addListItem(e) {
405
+ var t, i;
406
+ if (console.log(this.processors.rawModel, e.field), !((t = this.processors.rawModel[e.field]) != null && t[0]))
407
+ return Promise.reject({
408
+ code: "0001",
409
+ message: "异步默认值数据正在处理中,请您耐心等待... "
410
+ });
411
+ (i = this.processors.rawModel[e.field]) != null && i[0] && this.model.value[e.field].push(A(this.processors.rawModel[e.field][0]));
412
+ }
413
+ deleteListItem(e, t) {
414
+ this.model.value[e.field].splice(t, 1);
415
+ }
416
+ runtimeListProcessor(e) {
417
+ const t = this;
418
+ return v(w.runtimeDoms.List, {
419
+ schema: e
420
+ }, {
421
+ default() {
422
+ return t.model.value[e.field].map((i, f) => v(w.runtimeDoms.ListItem, null, {
423
+ default() {
424
+ return e.children.map((o, s) => t.runtimeItemProcessor(o, s, i, e));
425
+ },
426
+ delete({
427
+ container: o
428
+ } = {}) {
429
+ var l;
430
+ let s = o ?? v("button", null, null);
431
+ return V(v(s, {
432
+ onClick: () => t.deleteListItem(e, f)
433
+ }, null), [[W, ((l = t.model.value[e.field]) == null ? void 0 : l.length) > 1]]);
434
+ }
435
+ }));
436
+ },
437
+ add({
438
+ container: i
439
+ } = {}) {
440
+ let f = i ?? v("button", null, [G("添加")]);
441
+ return v(f, {
442
+ onClick: () => t.addListItem(e)
443
+ }, null);
444
+ }
445
+ });
446
+ }
447
+ runtimeProcessor(e) {
448
+ return e.map((t) => (t.type || (t.type = "item"), this.processorBySchemaType[t.type](t)));
449
+ }
450
+ exec() {
451
+ let e;
452
+ return v(w.runtimeDoms.Form, {
453
+ ref: this.formRef,
454
+ model: this.model.value
455
+ }, R(e = this.runtimeProcessor(this.schemas.value)) ? e : {
456
+ default: () => [e]
457
+ });
458
+ }
459
+ }
460
+ class w {
461
+ }
462
+ h(w, "runtimeDoms");
463
+ class k {
464
+ constructor() {
465
+ h(this, "effects", /* @__PURE__ */ new Set());
466
+ }
467
+ clearEffects() {
468
+ this.effects.clear();
469
+ }
470
+ triggerEffects() {
471
+ Array.from(this.effects).forEach((e) => e());
472
+ }
473
+ trackEffect(e) {
474
+ this.effects.add(e);
475
+ }
476
+ }
477
+ const J = /* @__PURE__ */ $({
478
+ props: {
479
+ setup: {
480
+ type: Function,
481
+ required: !0
482
+ }
483
+ },
484
+ setup(c) {
485
+ const e = new T(c.setup);
486
+ return () => e.exec();
487
+ }
488
+ });
489
+ function K(c) {
490
+ const e = new B(c);
491
+ return [
492
+ e.setup.bind(e),
493
+ {
494
+ submit: e.submit.bind(e)
495
+ }
496
+ ];
497
+ }
498
+ function Q(c) {
499
+ return {
500
+ install() {
501
+ w.runtimeDoms = c;
502
+ }
503
+ };
504
+ }
505
+ export {
506
+ J as ProForm,
507
+ K as useForm,
508
+ Q as useFormRenderer
509
+ };
@@ -0,0 +1,3 @@
1
+ export { default as ProForm } from "./src/form/index";
2
+ export { useForm } from "./src/interactions/useForm";
3
+ export { useFormRenderer } from "./src/interactions/useFormRenderer";
@@ -0,0 +1,14 @@
1
+ import { PropType } from "vue";
2
+ import { Setup } from "../types";
3
+ declare const _default: import("vue").DefineComponent<{
4
+ setup: {
5
+ type: PropType<Setup>;
6
+ required: true;
7
+ };
8
+ }, () => JSX.Element, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, Readonly<import("vue").ExtractPropTypes<{
9
+ setup: {
10
+ type: PropType<Setup>;
11
+ required: true;
12
+ };
13
+ }>>, {}, {}>;
14
+ export default _default;
@@ -0,0 +1,2 @@
1
+ import { FormCustomization, UseForm } from "../types";
2
+ export declare function useForm(formCustomization: FormCustomization): UseForm;
@@ -0,0 +1,4 @@
1
+ import { RuntimeDomCustomizer } from "../types";
2
+ export declare function useFormRenderer(runtimeDomCustomizer: RuntimeDomCustomizer): {
3
+ install(): void;
4
+ };
@@ -0,0 +1,4 @@
1
+ import { RuntimeDomCustomizer } from "../types";
2
+ export default class Context {
3
+ static runtimeDoms: RuntimeDomCustomizer;
4
+ }
@@ -0,0 +1,8 @@
1
+ import { AnyFunction } from "../types";
2
+ export default class Effect {
3
+ effects: Set<Function>;
4
+ constructor();
5
+ clearEffects(): void;
6
+ triggerEffects(): void;
7
+ trackEffect(effect: AnyFunction): void;
8
+ }
@@ -0,0 +1,10 @@
1
+ import { AnyObject, FormCustomization } from "../types";
2
+ import { RuntimeCore } from "./index";
3
+ export default class FormCustomizer {
4
+ formCustomization: FormCustomization;
5
+ runtimeCore: RuntimeCore;
6
+ private cleanFallbackFields;
7
+ constructor(formCustomization: FormCustomization);
8
+ setup(_runtimeCore: RuntimeCore): FormCustomization;
9
+ submit(): Promise<AnyObject>;
10
+ }
@@ -0,0 +1,24 @@
1
+ import { Ref } from "vue";
2
+ import { AnyObject, Schema, ItemSchema, ProxyedSchema, AnyFunction } from "../types";
3
+ import { Effect } from "../services";
4
+ export default class Processors {
5
+ processedSchemas: Ref<Schema[]>;
6
+ processedModel: Ref<AnyObject>;
7
+ rawSchemas: ProxyedSchema[];
8
+ rawModel: AnyObject;
9
+ schemaPreset: Record<keyof ItemSchema, any>;
10
+ componentPropsPreset: AnyObject;
11
+ uniqueEffectMap: any;
12
+ schemaEffect: Effect;
13
+ modelEffect: Effect;
14
+ stopWatchEffect: Effect;
15
+ constructor(processedSchemas: Ref<Schema[]>, processedModel: Ref<AnyObject>);
16
+ schemaAnalyzer(schemas: ProxyedSchema[], baseSchema?: Schema[], baseRawSchema?: ProxyedSchema[], parentField?: string, schemaIndex?: number): void;
17
+ schemaProcessor(schema: ProxyedSchema, index: number, setter: AnyFunction, schemaIndex?: number, parentField?: string): void;
18
+ replaceFunctionsWithUndefined(obj: AnyObject): AnyObject;
19
+ runtimeMeta(): {
20
+ model: AnyObject;
21
+ };
22
+ propsProcessor<T extends object = any>(pendingProcess: T, preset: Record<keyof T, any>, processed: AnyObject, update: AnyFunction, schemaIndexOrChildrenIndex: number, schemaIndex?: number, parentField?: string): void;
23
+ modelProcessor(schema: ProxyedSchema, baseModel?: AnyObject): void;
24
+ }
@@ -0,0 +1,20 @@
1
+ import { Ref } from "vue";
2
+ import { Setup, Schema, FormCustomization, AnyObject, ItemSchema, GroupSchema, ListSchema, ProcessorBySchemaType } from "../types";
3
+ import Processors from "./Processors";
4
+ export default class RuntimeCore {
5
+ setup: Setup;
6
+ processors: Processors;
7
+ schemas: Ref<Schema[]>;
8
+ model: Ref<AnyObject>;
9
+ processorBySchemaType: ProcessorBySchemaType;
10
+ formRef: Ref<AnyObject>;
11
+ constructor(setup: Setup);
12
+ analyze(formCustomization: FormCustomization): void;
13
+ runtimeItemProcessor(schema: ItemSchema, index?: number, baseModel?: AnyObject, parentSchema?: ListSchema): JSX.Element;
14
+ runtimeGroupProcessor(schema: GroupSchema): JSX.Element;
15
+ addListItem(schema: AnyObject): Promise<never> | undefined;
16
+ deleteListItem(schema: AnyObject, index: number): void;
17
+ runtimeListProcessor(schema: ListSchema): JSX.Element;
18
+ runtimeProcessor(schemas: Schema[]): any[];
19
+ exec(): JSX.Element;
20
+ }
@@ -0,0 +1,5 @@
1
+ export { default as FormCustomizer } from "./FormCustomizer";
2
+ export { default as RuntimeCore } from "./RuntimeCore";
3
+ export { default as Context } from "./Context";
4
+ export { default as Processors } from "./Processors";
5
+ export { default as Effect } from "./Effect";
@@ -0,0 +1,12 @@
1
+ import { AnyObject } from "./utilTypes";
2
+ export type DomType = new (...args: any) => AnyObject & {
3
+ $props: AnyObject;
4
+ };
5
+ export interface RuntimeDomCustomizer {
6
+ Form: DomType;
7
+ FormItem: DomType;
8
+ Item: DomType;
9
+ List: DomType;
10
+ ListItem: DomType;
11
+ Group: DomType;
12
+ }
@@ -0,0 +1,55 @@
1
+ import { AnyObject, DomType } from "./index";
2
+ export type SchemaType = "item" | "list" | "group";
3
+ export type FieldRule<T = any> = {
4
+ type?: "string" | "number" | "boolean" | "array" | "object" | "email" | "url" | "ip";
5
+ required?: boolean;
6
+ message?: string;
7
+ length?: number;
8
+ maxLength?: number;
9
+ minLength?: number;
10
+ match?: RegExp;
11
+ uppercase?: boolean;
12
+ lowercase?: boolean;
13
+ min?: number;
14
+ max?: number;
15
+ equal?: number;
16
+ positive?: boolean;
17
+ negative?: boolean;
18
+ true?: boolean;
19
+ false?: boolean;
20
+ includes?: any[];
21
+ deepEqual?: any;
22
+ empty?: boolean;
23
+ hasKeys?: string[];
24
+ validator?: (value: T | undefined, callback: (error?: string) => void) => void;
25
+ };
26
+ export interface ItemSchema {
27
+ type?: "item";
28
+ rules?: FieldRule[];
29
+ show?: boolean;
30
+ label: string;
31
+ field: string;
32
+ component: DomType;
33
+ componentProps?: AnyObject;
34
+ defaultValue?: PropertyKey | object;
35
+ }
36
+ export interface GroupSchema {
37
+ type: "group";
38
+ label: string;
39
+ children: ProxyedSchema[];
40
+ }
41
+ export interface ListSchema {
42
+ type: "list";
43
+ field: string;
44
+ label: string;
45
+ children: ProxyedSchema[];
46
+ }
47
+ export type Schema = ItemSchema | GroupSchema | ListSchema;
48
+ export type ProFormProxyRule<T> = T | ((...args: any) => T) | ((...args: any) => Promise<T>);
49
+ export type ProFormProxy<T> = {
50
+ [K in keyof T]: ProFormProxyRule<T[K]>;
51
+ };
52
+ export type ProxyedSchema = ProFormProxy<ItemSchema | GroupSchema | ListSchema>;
53
+ export interface FormCustomization {
54
+ schemas: ProxyedSchema[];
55
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./useFormTypes";
2
+ export * from "./contextTypes";
3
+ export * from "./formCustomizerTypes";
4
+ export * from "./utilTypes";
5
+ export * from "./runtimeTypes";
@@ -0,0 +1,6 @@
1
+ import { GroupSchema, ItemSchema, ListSchema } from "./index";
2
+ export interface ProcessorBySchemaType {
3
+ item: (schema: ItemSchema) => any;
4
+ group: (schema: GroupSchema) => any;
5
+ list: (schema: ListSchema) => any;
6
+ }
@@ -0,0 +1,9 @@
1
+ import { AnyObject } from "../types";
2
+ import type { RuntimeCore } from "../services";
3
+ export type Setup = (runtimeCore: RuntimeCore) => any;
4
+ export type UseForm = [
5
+ Setup,
6
+ {
7
+ submit: () => Promise<AnyObject>;
8
+ }
9
+ ];
@@ -0,0 +1,2 @@
1
+ export type AnyObject = Record<PropertyKey, any>;
2
+ export type AnyFunction = (...args: any) => any;
@@ -0,0 +1,2 @@
1
+ export declare function deepAssign(target: Record<PropertyKey, any>, ...sources: any[]): Record<PropertyKey, any>;
2
+ export declare function deepClone<T>(source: T): T;
@@ -0,0 +1,2 @@
1
+ export { default as IS } from "./is";
2
+ export * from "./deep";
@@ -0,0 +1,13 @@
1
+ import { GroupSchema, ItemSchema, ListSchema } from "../types";
2
+ export default class IS {
3
+ private static typeChecker;
4
+ static isArray(data: any): data is any[];
5
+ static isFunction(data: any): data is (...args: any) => any;
6
+ static isAsyncFunction(data: any): data is (...args: any) => Promise<any>;
7
+ static isUndefined(data: any): data is undefined;
8
+ static isArrayEmpty(data: any[]): boolean;
9
+ static isObjectEmpty(data: Record<PropertyKey, any>): boolean;
10
+ static isListSchema(data: any): data is ListSchema;
11
+ static isGroupSchema(data: any): data is GroupSchema;
12
+ static isItemSchema(data: any): data is ItemSchema;
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@harbor-design/proform",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",