8a22e84f
梁灏
init Form component
|
1
|
<template>
|
9f5e2c7e
梁灏
update Form
|
2
|
<div :class="classes">
|
f3c6cd68
梁灏
fixed FormItem la...
|
3
|
<label :class="[prefixCls + '-label']" :style="labelStyles" v-if="label || $slots.label"><slot name="label">{{ label }}</slot></label>
|
184dba1c
梁灏
update Form
|
4
|
<div :class="[prefixCls + '-content']" :style="contentStyles">
|
9f5e2c7e
梁灏
update Form
|
5
|
<slot></slot>
|
257f80f1
梁灏
support Form
|
6
7
8
|
<transition name="fade">
<div :class="[prefixCls + '-error-tip']" v-if="validateState === 'error' && showMessage && form.showMessage">{{ validateMessage }}</div>
</transition>
|
9f5e2c7e
梁灏
update Form
|
9
10
|
</div>
</div>
|
8a22e84f
梁灏
init Form component
|
11
12
|
</template>
<script>
|
9f5e2c7e
梁灏
update Form
|
13
14
15
|
// https://github.com/ElemeFE/element/blob/dev/packages/form/src/form-item.vue
import AsyncValidator from 'async-validator';
|
257f80f1
梁灏
support Form
|
16
|
import Emitter from '../../mixins/emitter';
|
9f5e2c7e
梁灏
update Form
|
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
|
const prefixCls = 'ivu-form-item';
function getPropByPath(obj, path) {
let tempObj = obj;
path = path.replace(/\[(\w+)\]/g, '.$1');
path = path.replace(/^\./, '');
let keyArr = path.split('.');
let i = 0;
for (let len = keyArr.length; i < len - 1; ++i) {
let key = keyArr[i];
if (key in tempObj) {
tempObj = tempObj[key];
} else {
throw new Error('[iView warn]: please transfer a valid prop path to form item!');
}
}
return {
o: tempObj,
k: keyArr[i],
v: tempObj[keyArr[i]]
};
}
|
8a22e84f
梁灏
init Form component
|
43
|
export default {
|
257f80f1
梁灏
support Form
|
44
45
|
name: 'FormItem',
mixins: [ Emitter ],
|
9f5e2c7e
梁灏
update Form
|
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
props: {
label: {
type: String,
default: ''
},
labelWidth: {
type: Number
},
prop: {
type: String
},
required: {
type: Boolean,
default: false
},
rules: {
type: [Object, Array]
},
error: {
|
8a83e7c4
梁灏
update Form
|
65
|
type: String
|
9f5e2c7e
梁灏
update Form
|
66
67
68
|
},
validateStatus: {
type: Boolean
|
6986d055
梁灏
Form add show-mes...
|
69
70
71
72
|
},
showMessage: {
type: Boolean,
default: true
|
9f5e2c7e
梁灏
update Form
|
73
74
|
}
},
|
8a22e84f
梁灏
init Form component
|
75
|
data () {
|
9f5e2c7e
梁灏
update Form
|
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
|
return {
prefixCls: prefixCls,
isRequired: false,
validateState: '',
validateMessage: '',
validateDisabled: false,
validator: {}
};
},
watch: {
error (val) {
this.validateMessage = val;
this.validateState = 'error';
},
validateStatus (val) {
this.validateState = val;
}
},
computed: {
classes () {
return [
`${prefixCls}`,
{
[`${prefixCls}-required`]: this.required || this.isRequired,
[`${prefixCls}-error`]: this.validateState === 'error',
[`${prefixCls}-validating`]: this.validateState === 'validating'
}
];
},
form() {
let parent = this.$parent;
while (parent.$options.name !== 'iForm') {
parent = parent.$parent;
}
return parent;
},
fieldValue: {
cache: false,
get() {
const model = this.form.model;
if (!model || !this.prop) { return; }
let path = this.prop;
if (path.indexOf(':') !== -1) {
path = path.replace(/:/, '.');
}
return getPropByPath(model, path).v;
}
},
labelStyles () {
let style = {};
const labelWidth = this.labelWidth || this.form.labelWidth;
if (labelWidth) {
style.width = `${labelWidth}px`;
}
return style;
},
contentStyles () {
let style = {};
const labelWidth = this.labelWidth || this.form.labelWidth;
if (labelWidth) {
style.marginLeft = `${labelWidth}px`;
}
return style;
}
},
methods: {
getRules () {
let formRules = this.form.rules;
const selfRules = this.rules;
formRules = formRules ? formRules[this.prop] : [];
return [].concat(selfRules || formRules || []);
},
getFilteredRule (trigger) {
const rules = this.getRules();
return rules.filter(rule => !rule.trigger || rule.trigger.indexOf(trigger) !== -1);
},
validate(trigger, callback = function () {}) {
const rules = this.getFilteredRule(trigger);
if (!rules || rules.length === 0) {
callback();
return true;
}
this.validateState = 'validating';
let descriptor = {};
descriptor[this.prop] = rules;
const validator = new AsyncValidator(descriptor);
let model = {};
model[this.prop] = this.fieldValue;
|
9dbff364
梁灏
update Form-item
|
174
|
validator.validate(model, { firstFields: true }, errors => {
|
9f5e2c7e
梁灏
update Form
|
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
this.validateState = !errors ? 'success' : 'error';
this.validateMessage = errors ? errors[0].message : '';
callback(this.validateMessage);
});
},
resetField () {
this.validateState = '';
this.validateMessage = '';
let model = this.form.model;
let value = this.fieldValue;
let path = this.prop;
if (path.indexOf(':') !== -1) {
path = path.replace(/:/, '.');
}
let prop = getPropByPath(model, path);
|
05e2dda0
梁灏
fixed #768
|
194
195
196
197
198
199
200
201
|
// if (Array.isArray(value) && value.length > 0) {
// this.validateDisabled = true;
// prop.o[prop.k] = [];
// } else if (value !== this.initialValue) {
// this.validateDisabled = true;
// prop.o[prop.k] = this.initialValue;
// }
if (Array.isArray(value)) {
|
9f5e2c7e
梁灏
update Form
|
202
|
this.validateDisabled = true;
|
05e2dda0
梁灏
fixed #768
|
203
204
|
prop.o[prop.k] = [].concat(this.initialValue);
} else {
|
9f5e2c7e
梁灏
update Form
|
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
this.validateDisabled = true;
prop.o[prop.k] = this.initialValue;
}
},
onFieldBlur() {
this.validate('blur');
},
onFieldChange() {
if (this.validateDisabled) {
this.validateDisabled = false;
return;
}
this.validate('change');
}
},
|
257f80f1
梁灏
support Form
|
221
|
mounted () {
|
9f5e2c7e
梁灏
update Form
|
222
|
if (this.prop) {
|
257f80f1
梁灏
support Form
|
223
|
this.dispatch('iForm', 'on-form-item-add', this);
|
9f5e2c7e
梁灏
update Form
|
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
Object.defineProperty(this, 'initialValue', {
value: this.fieldValue
});
let rules = this.getRules();
if (rules.length) {
rules.every(rule => {
if (rule.required) {
this.isRequired = true;
return false;
}
});
|
578ca325
梁灏
fixed Radio bug
|
238
239
|
this.$on('on-form-blur', this.onFieldBlur);
this.$on('on-form-change', this.onFieldChange);
|
9f5e2c7e
梁灏
update Form
|
240
241
|
}
}
|
8a22e84f
梁灏
init Form component
|
242
|
},
|
9f5e2c7e
梁灏
update Form
|
243
|
beforeDestroy () {
|
257f80f1
梁灏
support Form
|
244
|
this.dispatch('iForm', 'on-form-item-remove', this);
|
9f5e2c7e
梁灏
update Form
|
245
|
}
|
8a22e84f
梁灏
init Form component
|
246
247
|
};
</script>
|