tui-form.vue
8.85 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<template>
<view class="tui-form__box" :style="{backgroundColor:backgroundColor,padding:padding,borderRadius:radius}">
<slot></slot>
<view class="tui-form__errmsg"
:style="{top:tipTop+'px',padding:tipPadding,backgroundColor:getTipBgColor,borderRadius:tipRidus}"
v-if="showMessage" :class="{'tui-message__show':errorMsg}"><text class="tui-form__text"
:style="{fontSize:tipSize+'rpx',color:tipColor}">{{errorMsg}}</text></view>
<view class="tui-form__mask" v-if="disabled"></view>
</view>
</template>
<script>
import form from "./tui-validation.js"
export default {
name: "tui-form",
provide() {
return {
form: this
}
},
props: {
//表单数据对象
model: {
type: Object,
default () {
return {}
}
},
//表单验证规则,即将废弃
rules: {
type: Array,
default () {
return []
}
},
//表单背景颜色
backgroundColor: {
type: String,
default: 'transparent'
},
//表单padding值
padding: {
type: String,
default: '0'
},
//是否顶部弹出方式显示校验错误信息【为false时则可关联FormItem组件显示校验错误信息】
showMessage: {
type: Boolean,
default: true
},
//表单圆角值
radius: {
type: String,
default: '0'
},
//是否禁用该表单内的所有组件,透明遮罩层
disabled: {
type: Boolean,
default: false
},
//提示框top值 px
tipTop: {
type: [Number, String],
// #ifdef H5
default: 44,
// #endif
// #ifndef H5
default: 0
// #endif
},
//错误提示框padding值
tipPadding: {
type: String,
default: '20rpx'
},
//错误提示框背景色
tipBackgroundColor: {
type: String,
default: ''
},
//错误提示字体大小
tipSize: {
type: [Number, String],
default: 28
},
//错误提示字体颜色
tipColor: {
type: String,
default: '#fff'
},
//错误提示框圆角值
tipRidus: {
type: String,
default: '12rpx'
},
//错误消息显示时间 ms
duration: {
type: [Number, String],
default: 0
}
},
computed: {
getTipBgColor() {
return this.tipBackgroundColor || (uni && uni.$tui && uni.$tui.tuiForm.tipBackgroundColor) ||
'#f74d54';
}
},
watch: {
showMessage(val) {
if (this.children && this.children.length > 0) {
this.children.forEach(item => {
item.showError = val ? false : true
})
}
}
},
data() {
return {
errorMsg: '',
timer: null,
formRules: [],
isImmediate: false,
concatRules: []
};
},
created() {
this.children = []
},
// #ifndef VUE3
beforeDestroy() {
this.clearTimer()
},
// #endif
// #ifdef VUE3
beforeUnmount() {
this.clearTimer()
},
// #endif
methods: {
clearTimer() {
clearTimeout(this.timer)
this.timer = null;
this.children = null
},
getFormItemRules() {
let rules = []
if (this.children && this.children.length > 0) {
this.children.forEach(child => {
let rule = child.getRules()
rule && rules.push(rule)
})
}
return rules;
},
getMergeRules(rules) {
if (this.concatRules.length === 0) return rules;
let formRules = [...rules]
//合并并替换当前rules数据
this.concatRules.forEach(item => {
const index = rules.findIndex(e => e.name === item.name)
if (index === -1) {
formRules.push(item)
} else {
formRules[index] = item;
}
})
return formRules;
},
//{Object} model 表单数据对象,传null则使用属性中model值
//{Array} rules 表单验证规则,传null则使用FormItem项内传入的rules值
//{Boolean} checkAll 是否返回所有错误信息
validate(model, rules, checkAll = false) {
model = model || this.model
rules = rules || this.rules || []
return new Promise((resolve, reject) => {
try {
if (rules.length === 0) {
rules = this.getFormItemRules()
} else {
rules = this.getMergeRules(rules)
}
let res = form.validation(model, rules, checkAll);
if (!res.isPass) {
let errors = res.errorMsg;
if (this.showMessage) {
this.clearTimer()
if (checkAll) {
errors = errors[0].msg
}
this.errorMsg = errors;
const duration = this.duration || (uni && uni.$tui && uni.$tui.tuiForm.duration) ||
2000
this.timer = setTimeout(() => {
this.errorMsg = ''
}, Number(duration))
} else {
if (checkAll && this.children && this.children.length > 0) {
//FormItem组件 显示提示
this.children.forEach(item => {
const index = errors.findIndex(err => err.name === item.prop)
if (item.prop && item.prop !== true && ~index) {
item.errorMsg = errors[index].msg
item.itemValue = model[item.prop]
}
})
}
}
}
resolve(res)
} catch (e) {
//TODO handle the exception
reject({
isPass: false,
errorMsg: '校验出错,请检查数据格式是否有误!'
})
}
})
},
//结合FormItem组件进行校验时 是否开启即时校验
immediateValidate(isOpen, rules = []) {
this.isImmediate = isOpen;
if (isOpen) {
if (!rules || rules.length === 0) {
rules = this.getFormItemRules()
} else {
rules = this.getMergeRules(rules)
}
this.formRules = rules || []
}
if (this.children && this.children.length > 0) {
this.children.forEach(item => {
item.immediateValidate(isOpen)
})
}
},
//校验
immediateValidator(prop, model, rules) {
return new Promise((resolve, reject) => {
try {
let res = form.validation(model || this.model, rules || this.formRules, true);
if (!res.isPass) {
//显示提示
let errors = res.errorMsg;
const index = errors.findIndex(err => err.name === prop)
if (~index) {
res.errorMsg = errors[index].msg
} else {
res.isPass = true
res.errorMsg = ''
}
}
resolve(res)
} catch (e) {
reject({
isPass: false,
errorMsg: '校验出错,请检查数据格式是否有误!'
})
}
})
},
clearValidate(props = []) {
let arr = props;
arr = !arr ? [] : arr
if (typeof props === 'string') {
arr = [props]
}
if (this.children && this.children.length > 0) {
//清除指定字段的表单验证信息
if (arr && arr.length > 0) {
this.children.forEach(item => {
if (item.prop && ~arr.indexOf(item.prop)) {
item.errorMsg = ''
}
})
} else {
//清除所有字段的表单验证信息
this.children.forEach(item => {
item.errorMsg = ''
})
}
}
},
/**
* 验证具体的某个字段
* @param {Array<string> | String} props 字段key
* @param {Array} rules 表单验证规则,当传null 或空数组时使用FormItem组件内rules
* @param {Object} model 表单数据对象,不传则使用属性中model值
*/
validateField(props, rules, model) {
if (!rules || rules.length === 0) {
rules = this.getFormItemRules()
} else {
rules = this.getMergeRules(rules)
}
const isString = typeof props === 'string';
const formRules = rules.filter(item => props === item.name || (!isString && props
.indexOf(item.name) !== -1));
model = model || this.model
return this.validate(model, formRules, true)
},
// 移除表单项
uninstall(instance) {
if (this.children && this.children.length > 0) {
const index = this.children.findIndex(item => item === instance)
if (index !== -1) {
this.children.splice(index, 1)
}
const rules = instance.getRules() || {}
const prop = instance.prop || rules.name || ''
const idx = this.concatRules.findIndex(ru => ru.name === prop)
if (idx !== -1) {
this.concatRules.splice(idx, 1)
}
}
}
}
}
</script>
<style scoped>
.tui-form__box {
/* #ifndef APP-NVUE */
width: 100%;
box-sizing: border-box;
/* #endif */
flex: 1;
position: relative;
}
.tui-form__errmsg {
position: fixed;
z-index: 900;
text-align: center;
left: 20rpx;
right: 20rpx;
/* #ifndef APP-NVUE */
box-sizing: border-box;
display: flex;
word-break: break-all;
/* #endif */
align-items: center;
justify-content: center;
padding: 24rpx;
opacity: 0;
transform: translateZ(0) translateY(-100%);
transition-property: transform, opacity;
transition-duration: 0.25s;
transition-delay: 0s;
transition-timing-function: ease-in-out;
}
.tui-form__text {
text-align: center;
}
.tui-message__show {
transform: translateY(0) translateZ(0);
opacity: 1;
}
.tui-form__mask {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0);
z-index: 99;
}
</style>