starfish-form-custom 1.0.25 → 1.0.27

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.
@@ -1,288 +1,340 @@
1
1
  <template>
2
2
  <div class="starfish-dynamicform">
3
- <el-form ref="ruleForm" v-bind="$attrs" :model="formResult" :rules="rules" label-width="120px" class="demo-ruleForm" size="default" :validate-on-rule-change="false">
4
- <template v-for="item in allFormList" >
5
- <el-form-item :prop="item.data.fieldName" v-if="!item.layout && item.show" :key="item.id">
6
- <component ref="controlObj" @change="handleControlChange" :is="item.ControlType" v-bind="globalConfig" :item="item" :data="formResult || '{}'" :drag="false" size="default"></component>
3
+ <el-form
4
+ ref="ruleForm"
5
+ v-bind="$attrs"
6
+ :model="formResult"
7
+ :rules="rules"
8
+ label-width="120px"
9
+ class="demo-ruleForm"
10
+ size="default"
11
+ :validate-on-rule-change="false"
12
+ >
13
+ <template v-for="item in allFormList">
14
+ <el-form-item
15
+ :prop="item.data.fieldName"
16
+ v-if="!item.layout && item.show"
17
+ :key="item.id"
18
+ >
19
+ <component
20
+ ref="controlObj"
21
+ @change="handleControlChange"
22
+ :is="item.ControlType"
23
+ v-bind="globalConfig"
24
+ :readonly="readonly"
25
+ :item="item"
26
+ :data="formResult || '{}'"
27
+ :drag="false"
28
+ size="default"
29
+ ></component>
7
30
  </el-form-item>
8
31
  <template v-else-if="item.show">
9
- <component ref="controlObj" @change="handleControlChange" :is="item.ControlType" :key="item.id" v-bind="globalConfig" :item="item" :data="formResult || '{}'" :drag="false" size="default"></component>
32
+ <component
33
+ ref="controlObj"
34
+ @change="handleControlChange"
35
+ :is="item.ControlType"
36
+ :key="item.id"
37
+ v-bind="globalConfig"
38
+ :readonly="readonly"
39
+ :item="item"
40
+ :data="formResult || '{}'"
41
+ :drag="false"
42
+ size="default"
43
+ ></component>
10
44
  </template>
11
45
  </template>
12
46
  </el-form>
13
47
  </div>
14
48
  </template>
15
49
  <script lang="ts">
16
- import { defineComponent, ref, onMounted, getCurrentInstance, toRaw, PropType } from "vue";
17
- export default defineComponent({
18
- name: "Dynamicform",
19
- props: {
20
- allFormList: {
21
- type: Array as PropType<any>,
22
- default() {
23
- return [];
24
- },
50
+ import {
51
+ defineComponent,
52
+ ref,
53
+ onMounted,
54
+ getCurrentInstance,
55
+ toRaw,
56
+ PropType,
57
+ } from "vue";
58
+ export default defineComponent({
59
+ name: "Dynamicform",
60
+ props: {
61
+ allFormList: {
62
+ type: Array as PropType<any>,
63
+ default() {
64
+ return [];
25
65
  },
26
- formResult: {
27
- type: Object,
28
- default() {
29
- return {};
30
- },
66
+ },
67
+ formResult: {
68
+ type: Object,
69
+ default() {
70
+ return {};
31
71
  },
32
- globalConfig: {
33
- type: Object,
34
- default() {
35
- return {};
36
- },
37
- }
38
72
  },
39
- setup(props: any, { emit }) {
40
- const { proxy } = getCurrentInstance() as any;
41
- const rules: any = ref({});
42
- const ruleForm = ref();
43
- const controlObj = ref();
44
- props.allFormList?.forEach((item: any) => {
45
- getRules(item);
46
- });
73
+ globalConfig: {
74
+ type: Object,
75
+ default() {
76
+ return {};
77
+ },
78
+ },
79
+ readonly: {
80
+ type: Boolean,
81
+ default: false,
82
+ },
83
+ },
84
+ setup(props: any, { emit }) {
85
+ const { proxy } = getCurrentInstance() as any;
86
+ const rules: any = ref({});
87
+ const ruleForm = ref();
88
+ const controlObj = ref();
89
+ props.allFormList?.forEach((item: any) => {
90
+ getRules(item);
91
+ });
47
92
 
48
- function getFormListRules(rules: any[]) {
49
- const result: any[] = [];
50
- if (Array.isArray(rules) && rules && rules.length > 0) {
51
- rules.forEach((item) => {
52
- if (item.type == "enum") {
53
- const func = eval(`(${item.value})`);
54
- result.push({
55
- validator: func,
56
- trigger: "blur",
57
- });
58
- } else if (item.type == "func") {
59
- const mainData = props.formResult;
60
- const func = eval(`((rule, value, callback, mainData = mainData) => {${item.value.func}})`);
93
+ function getFormListRules(rules: any[]) {
94
+ const result: any[] = [];
95
+ if (Array.isArray(rules) && rules && rules.length > 0) {
96
+ rules.forEach((item) => {
97
+ if (item.type == "enum") {
98
+ const func = eval(`(${item.value})`);
99
+ result.push({
100
+ validator: func,
101
+ trigger: "blur",
102
+ });
103
+ } else if (item.type == "func") {
104
+ const mainData = props.formResult;
105
+ const func = eval(
106
+ `((rule, value, callback, mainData = mainData) => {${item.value.func}})`
107
+ );
108
+ result.push({
109
+ validator: func,
110
+ trigger: "blur",
111
+ });
112
+ console.log("mainData", mainData);
113
+ } else if (item.type == "high") {
114
+ if (item.value.ruleType == 5) {
61
115
  result.push({
62
- validator: func,
63
- trigger: "blur",
116
+ validator: eval(item.value.validor),
117
+ trigger: item.value.trigger,
64
118
  });
65
- console.log('mainData', mainData)
66
- } else if (item.type == "high") {
67
- if (item.value.ruleType == 5) {
68
- result.push({
69
- validator: eval(item.value.validor),
70
- trigger: item.value.trigger,
71
- });
72
- return;
73
- }
74
- // let high = JSON.parse(JSON.stringify(item.value));
75
- // delete high.ruleType;
76
- result.push(item.value);
119
+ return;
77
120
  }
78
- });
79
- }
80
- return result;
121
+ // let high = JSON.parse(JSON.stringify(item.value));
122
+ // delete high.ruleType;
123
+ result.push(item.value);
124
+ }
125
+ });
81
126
  }
127
+ return result;
128
+ }
82
129
 
83
- function getRules(item: any) {
84
- if (!item.layout) {
85
- let rule: any[] = [];
86
- if (item.data.required) {
87
- rule.push({
88
- required: true,
89
- message: "请输入" + item.data.label,
90
- trigger: "blur",
91
- });
92
- }
93
- // 新增 minLength 校验
94
- if (
95
- typeof item.data.minLength === "number" &&
96
- item.data.minLength > 0
97
- ) {
98
- rule.push({
99
- validator: (rule: any, value: any, callback: any) => {
100
- if (
101
- typeof value === "string" &&
102
- value.length < item.data.minLength
103
- ) {
104
- callback(
105
- new Error(
106
- `${item.data.label}最少输入${item.data.minLength}个字符`
107
- )
108
- );
109
- } else {
110
- callback();
111
- }
112
- },
113
- trigger: "blur",
130
+ function getRules(item: any) {
131
+ if (!item.layout) {
132
+ let rule: any[] = [];
133
+ if (item.data.required) {
134
+ rule.push({
135
+ required: true,
136
+ message: "请输入" + item.data.label,
137
+ trigger: "blur",
138
+ });
139
+ }
140
+ // 新增 minLength 校验
141
+ if (
142
+ typeof item.data.minLength === "number" &&
143
+ item.data.minLength > 0
144
+ ) {
145
+ rule.push({
146
+ validator: (rule: any, value: any, callback: any) => {
147
+ if (
148
+ typeof value === "string" &&
149
+ value.length < item.data.minLength
150
+ ) {
151
+ callback(
152
+ new Error(
153
+ `${item.data.label}最少输入${item.data.minLength}个字符`
154
+ )
155
+ );
156
+ } else {
157
+ callback();
158
+ }
159
+ },
160
+ trigger: "blur",
161
+ });
162
+ }
163
+ if (typeof item.data.rule == "string") {
164
+ rule = rule.concat(proxy.$Flex.tryParseJson(item.data.rule));
165
+ } else {
166
+ rule = rule.concat(getFormListRules(item.data.rule));
167
+ }
168
+ // 特殊的jsoneditor表单要单独处理
169
+ if (item.data.json) {
170
+ rule.push(...proxy.$Flex.getJsonValidate());
171
+ }
172
+ rules.value[item.data.fieldName] = rule;
173
+ } else if (item.layout) {
174
+ if (item.ControlType == "Grid") {
175
+ item.data.columns.forEach((colItem: any) => {
176
+ colItem.list.forEach((listItem: any) => {
177
+ getRules(listItem);
114
178
  });
115
- }
116
- if (typeof item.data.rule == "string") {
117
- rule = rule.concat(proxy.$Flex.tryParseJson(item.data.rule));
118
- } else {
119
- rule = rule.concat(getFormListRules(item.data.rule));
120
- }
121
- // 特殊的jsoneditor表单要单独处理
122
- if (item.data.json) {
123
- rule.push(...proxy.$Flex.getJsonValidate());
124
- }
125
- rules.value[item.data.fieldName] = rule;
126
- } else if (item.layout) {
127
- if (item.ControlType == "Grid") {
128
- item.data.columns.forEach((colItem: any) => {
129
- colItem.list.forEach((listItem: any) => {
179
+ });
180
+ } else if (item.ControlType == "TableLayout") {
181
+ const trs = item.data.trs;
182
+ trs.forEach((trItem: any) => {
183
+ trItem.tds.forEach((tdItem: any) => {
184
+ tdItem.list.forEach((listItem: any) => {
130
185
  getRules(listItem);
131
186
  });
132
187
  });
133
- } else if (item.ControlType == "TableLayout") {
134
- const trs = item.data.trs;
135
- trs.forEach((trItem: any) => {
136
- trItem.tds.forEach((tdItem: any) => {
137
- tdItem.list.forEach((listItem: any) => {
138
- getRules(listItem);
139
- });
140
- });
141
- });
142
- } else if (item.ControlType == "Collapse" || item.ControlType == "Tabs") {
143
- const items = item.data.items;
144
- items.forEach((colItem: any) => {
145
- colItem.list.forEach((listItem: any) => {
146
- getRules(listItem);
147
- });
188
+ });
189
+ } else if (
190
+ item.ControlType == "Collapse" ||
191
+ item.ControlType == "Tabs"
192
+ ) {
193
+ const items = item.data.items;
194
+ items.forEach((colItem: any) => {
195
+ colItem.list.forEach((listItem: any) => {
196
+ getRules(listItem);
148
197
  });
149
- }
198
+ });
150
199
  }
151
200
  }
201
+ }
152
202
 
153
- const handleControlChange = () => {
154
- const allFormLists: any = props.allFormList;
155
- allFormLists.forEach((item: any) => {
156
- if (item.data.showRule === "{}") {
157
- item.show = true;
158
- } else {
159
- try {
160
- if (Array.isArray(item.data.showRule)) {
161
- item.show = conditionChange(transformData(toRaw(item.data.showRule)));
162
- } else {
163
- item.show = conditionChange(toRaw(item.data.showRule));
164
- }
165
- } catch (e) {
166
- item.show = true;
203
+ const handleControlChange = () => {
204
+ const allFormLists: any = props.allFormList;
205
+ allFormLists.forEach((item: any) => {
206
+ if (item.data.showRule === "{}") {
207
+ item.show = true;
208
+ } else {
209
+ try {
210
+ if (Array.isArray(item.data.showRule)) {
211
+ item.show = conditionChange(
212
+ transformData(toRaw(item.data.showRule))
213
+ );
214
+ } else {
215
+ item.show = conditionChange(toRaw(item.data.showRule));
167
216
  }
217
+ } catch (e) {
218
+ item.show = true;
168
219
  }
220
+ }
221
+ });
222
+ executeFunc("updated");
223
+ emit("change");
224
+ };
225
+ function transformData(data: any) {
226
+ /**普通模式转为高级模式的数据结构,方便复用 */
227
+ const r: any = [];
228
+ data.forEach((item: any) => {
229
+ r.push({
230
+ type: "andgroup",
231
+ result: item.map((d: any) => {
232
+ return {
233
+ type: "data",
234
+ data: d,
235
+ };
236
+ }),
169
237
  });
170
- executeFunc("updated");
171
- emit("change");
238
+ });
239
+ const result = {
240
+ type: "orgroup",
241
+ result: r,
172
242
  };
173
- function transformData(data: any) {
174
- /**普通模式转为高级模式的数据结构,方便复用 */
175
- const r: any = [];
176
- data.forEach((item: any) => {
177
- r.push({
178
- type: "andgroup",
179
- result: item.map((d: any) => {
180
- return {
181
- type: "data",
182
- data: d,
183
- };
184
- }),
243
+ return result;
244
+ }
245
+ function conditionChange(data: any) {
246
+ if (data.type == "andgroup") {
247
+ const result = data.result
248
+ .map((item: any) => {
249
+ const r = conditionChange(item);
250
+ return r;
251
+ })
252
+ .find((item: boolean) => {
253
+ return item == false;
185
254
  });
186
- });
187
- const result = {
188
- type: "orgroup",
189
- result: r,
190
- };
191
- return result;
192
- }
193
- function conditionChange(data: any) {
194
- if (data.type == "andgroup") {
195
- const result = data.result
196
- .map((item: any) => {
197
- const r = conditionChange(item);
198
- return r;
199
- })
200
- .find((item: boolean) => {
201
- return item == false;
202
- });
203
- return result === undefined ? true : result;
204
- } else if (data.type == "orgroup") {
205
- const result = data.result
206
- .map((item: any) => {
207
- const r = conditionChange(item);
208
- return r;
209
- })
210
- .find((item: boolean) => {
211
- return item == true;
212
- });
213
- return result === undefined ? false : result;
214
- } else if (data.type == "data") {
215
- const result = data.data;
216
- const formResults: any = props.formResult;
217
- const value = formResults[result.field];
218
- let isShow = false;
219
- switch (result.logic) {
220
- case "=":
221
- isShow = value == result.value;
222
- break;
223
- case "!=":
224
- isShow = value != result.value;
225
- break;
226
- case "in":
227
- if (Array.isArray(value)) {
228
- value.find((item) => {
229
- if (result.value.include(item)) {
230
- isShow = result.value.includes(item);
231
- return item;
232
- }
233
- });
234
- } else {
235
- isShow = result.value.includes(value);
236
- }
237
- break;
238
- case "not in":
239
- if (Array.isArray(value)) {
240
- value.find((item) => {
241
- if (!result.value.include(item)) {
242
- isShow = !result.value.includes(item);
243
- return item;
244
- }
245
- });
246
- } else {
247
- isShow = !result.value.includes(value);
248
- }
249
- break;
250
- }
251
- return isShow;
255
+ return result === undefined ? true : result;
256
+ } else if (data.type == "orgroup") {
257
+ const result = data.result
258
+ .map((item: any) => {
259
+ const r = conditionChange(item);
260
+ return r;
261
+ })
262
+ .find((item: boolean) => {
263
+ return item == true;
264
+ });
265
+ return result === undefined ? false : result;
266
+ } else if (data.type == "data") {
267
+ const result = data.data;
268
+ const formResults: any = props.formResult;
269
+ const value = formResults[result.field];
270
+ let isShow = false;
271
+ switch (result.logic) {
272
+ case "=":
273
+ isShow = value == result.value;
274
+ break;
275
+ case "!=":
276
+ isShow = value != result.value;
277
+ break;
278
+ case "in":
279
+ if (Array.isArray(value)) {
280
+ value.find((item) => {
281
+ if (result.value.include(item)) {
282
+ isShow = result.value.includes(item);
283
+ return item;
284
+ }
285
+ });
286
+ } else {
287
+ isShow = result.value.includes(value);
288
+ }
289
+ break;
290
+ case "not in":
291
+ if (Array.isArray(value)) {
292
+ value.find((item) => {
293
+ if (!result.value.include(item)) {
294
+ isShow = !result.value.includes(item);
295
+ return item;
296
+ }
297
+ });
298
+ } else {
299
+ isShow = !result.value.includes(value);
300
+ }
301
+ break;
252
302
  }
303
+ return isShow;
253
304
  }
254
- function reset() {
255
- ruleForm.value.resetFields();
256
- }
257
- function getValidate() {
258
- return new Promise((resolve) => {
259
- ruleForm.value.validate((valide: boolean) => {
260
- resolve(valide);
261
- });
305
+ }
306
+ function reset() {
307
+ ruleForm.value.resetFields();
308
+ }
309
+ function getValidate() {
310
+ return new Promise((resolve) => {
311
+ ruleForm.value.validate((valide: boolean) => {
312
+ resolve(valide);
262
313
  });
263
- }
264
- onMounted(() => {
265
- handleControlChange();
266
- executeFunc("mounted");
267
314
  });
268
- function executeFunc(funcName: string) {
269
- const mountedAction = props.globalConfig.action?.find((item: any) => {
270
- if (item.type == funcName) {
271
- return item;
272
- }
273
- });
274
- if (mountedAction) {
275
- eval(`(function(){${mountedAction.funcStr}}).call(proxy)`);
315
+ }
316
+ onMounted(() => {
317
+ handleControlChange();
318
+ executeFunc("mounted");
319
+ });
320
+ function executeFunc(funcName: string) {
321
+ const mountedAction = props.globalConfig.action?.find((item: any) => {
322
+ if (item.type == funcName) {
323
+ return item;
276
324
  }
325
+ });
326
+ if (mountedAction) {
327
+ eval(`(function(){${mountedAction.funcStr}}).call(proxy)`);
277
328
  }
278
- return {
279
- rules,
280
- ruleForm,
281
- controlObj,
282
- handleControlChange,
283
- reset,
284
- getValidate,
285
- };
286
- },
287
- });
329
+ }
330
+ return {
331
+ rules,
332
+ ruleForm,
333
+ controlObj,
334
+ handleControlChange,
335
+ reset,
336
+ getValidate,
337
+ };
338
+ },
339
+ });
288
340
  </script>