index.js
3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* @Description: 表单框架
* @Author: huyue
* @Date: 2023-04-28 16:19:53
* @LastEditors: huyue
* @LastEditTime: 2023-08-10 16:54:00
*/
import WxValidate from '../WxValidate';
const utils = require('../utils');
Component({
externalClasses: [
'switch-active-class',
'switch-class',
'switch-item-class',
'lable-class',
],
options: {
multipleSlots: true, // 复数插槽: 是
},
data: {
formDataObj: {},
formDataList: [],
},
properties: {
/**
* formObj:表单数据绑定的对象
*/
formObj: {
type: Object,
value: {},
observer: function (n) {
this.setData({
formDataObj: n,
});
},
},
/**
* formData:表单配置数据
*/
formData: {
type: Array,
value: [],
observer: function (n) {
console.log(n);
this.initValidate(n);
this.setData({
formDataList: n,
});
},
},
/**
* inline:表单配置是否一行展示
*/
inline: {
type: Boolean,
value: true,
},
/**
* themeStyle: 主题样式
*/
themeStyle: {
type: String,
value: '#3582EE'
},
textPostion: {
type: String,
value: 'right'
}
},
methods: {
// 初始化选项值
getOptionsList(optionList, refreshSigle) {
console.log(optionList, refreshSigle);
if (refreshSigle) {
// 深拷贝传值
const cloneList = utils.deepClone(optionList[refreshSigle]);
const index = this.data.formDataList.findIndex(
(k) => k.name === refreshSigle
);
this.setData({
[`formDataList[${index}].valueList`]: cloneList,
});
} else {
this.data.formDataList.forEach((item, index) => {
if (optionList[item.name] && optionList[item.name].length) {
// 深拷贝传值
const cloneList = utils.deepClone(optionList[item.name]);
this.setData({
[`formDataList[${index}].valueList`]: cloneList,
});
}
});
console.log(this.data.formDataList, '===');
}
},
// 表单验证规则
initValidate(obj) {
const rules = {};
// const messages = {}
obj.forEach((item) => {
if (item.rules) {
rules[item.name] = item.rules;
// messages[item.name] = {
// required: item.rulesMessage || '请输入' + item.lable
// }
}
});
console.log(rules);
// console.log(messages);
this.WxValidate = new WxValidate(rules);
},
// 获取GlsxFormItem的值
getFormItemValue(e) {
// const value = this.selectComponent('#GlsxFormItem').formDataObj
console.log(e);
const {name, value, type} = e.detail;
if (type === 'muiltiImg') {
this.data.formDataObj[name].splice(value, 1)
this.setData({
[`formDataObj.${name}`]: this.data.formDataObj[name],
})
return
}
this.setData({
[`formDataObj.${name}`]: value,
})
// 加change事件,告知表单项变化
this.triggerEvent('changeFormItem', e.detail)
},
// 验证表单数据
// eslint-disable-next-line consistent-return
validateFormData() {
const params = this.data.formDataObj;
if (!this.WxValidate.checkForm(params)) {
const error = this.WxValidate.errorList;
return error;
}
},
// 返回formObj的值
returnFormObj() {
return this.data.formDataObj;
},
// select事件
customClick(e) {
const clickName = e.detail + 'Click';
console.log(clickName);
this.triggerEvent(clickName, e.detail);
}
},
// 组件页面生周期
lifetimes: {
created: function () {
// 在组件实例刚刚被创建时执行
// 初始化验证规则函数
this.initValidate();
},
},
});