8a22e84f
梁灏
init Form component
|
1
|
<template>
|
9f5e2c7e
梁灏
update Form
|
2
|
<div :class="classes">
|
867eca35
梁灏
FormItem add slot...
|
3
|
<label :class="[prefixCls + '-label']" :style="labelStyles" v-if="label"><slot name="label">{{ label }}</slot></label>
|
184dba1c
梁灏
update Form
|
4
|
<div :class="[prefixCls + '-content']" :style="contentStyles">
|
9f5e2c7e
梁灏
update Form
|
5
|
<slot></slot>
|
6986d055
梁灏
Form add show-mes...
|
6
|
<div transition="fade" :class="[prefixCls + '-error-tip']" v-if="validateState === 'error' && showMessage && form.showMessage">{{ validateMessage }}</div>
|
9f5e2c7e
梁灏
update Form
|
7
8
|
</div>
</div>
|
8a22e84f
梁灏
init Form component
|
9
10
|
</template>
<script>
|
9f5e2c7e
梁灏
update Form
|
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
|
// https://github.com/ElemeFE/element/blob/dev/packages/form/src/form-item.vue
import AsyncValidator from 'async-validator';
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
|
40
|
export default {
|
9f5e2c7e
梁灏
update Form
|
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
props: {
label: {
type: String,
default: ''
},
labelWidth: {
type: Number
},
prop: {
type: String
},
required: {
type: Boolean,
default: false
},
rules: {
type: [Object, Array]
},
error: {
|
8a83e7c4
梁灏
update Form
|
60
|
type: String
|
9f5e2c7e
梁灏
update Form
|
61
62
63
|
},
validateStatus: {
type: Boolean
|
6986d055
梁灏
Form add show-mes...
|
64
65
66
67
|
},
showMessage: {
type: Boolean,
default: true
|
9f5e2c7e
梁灏
update Form
|
68
69
|
}
},
|
8a22e84f
梁灏
init Form component
|
70
|
data () {
|
9f5e2c7e
梁灏
update Form
|
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
|
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
|
169
|
validator.validate(model, { firstFields: true }, errors => {
|
9f5e2c7e
梁灏
update Form
|
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
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);
if (Array.isArray(value) && value.length > 0) {
this.validateDisabled = true;
prop.o[prop.k] = [];
|
38646a11
梁灏
fixed Form reset ...
|
192
|
} else if (value !== this.initialValue) {
|
9f5e2c7e
梁灏
update Form
|
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
|
this.validateDisabled = true;
prop.o[prop.k] = this.initialValue;
}
},
onFieldBlur() {
this.validate('blur');
},
onFieldChange() {
if (this.validateDisabled) {
this.validateDisabled = false;
return;
}
this.validate('change');
}
},
ready () {
if (this.prop) {
this.$dispatch('on-form-item-add', this);
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
|
226
227
|
this.$on('on-form-blur', this.onFieldBlur);
this.$on('on-form-change', this.onFieldChange);
|
9f5e2c7e
梁灏
update Form
|
228
229
|
}
}
|
8a22e84f
梁灏
init Form component
|
230
|
},
|
9f5e2c7e
梁灏
update Form
|
231
232
233
|
beforeDestroy () {
this.$dispatch('on-form-item-remove', this);
}
|
8a22e84f
梁灏
init Form component
|
234
235
|
};
</script>
|