8a22e84f
梁灏
init Form component
|
1
|
<template>
|
9f5e2c7e
梁灏
update Form
|
2
|
<div :class="classes">
|
acb79ba3
梁灏
fixed #433
|
3
|
<label :class="[prefixCls + '-label']" :for="labelFor" :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
|
import AsyncValidator from 'async-validator';
|
257f80f1
梁灏
support Form
|
14
|
import Emitter from '../../mixins/emitter';
|
9f5e2c7e
梁灏
update Form
|
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
|
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
|
41
|
export default {
|
257f80f1
梁灏
support Form
|
42
43
|
name: 'FormItem',
mixins: [ Emitter ],
|
9f5e2c7e
梁灏
update Form
|
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
props: {
label: {
type: String,
default: ''
},
labelWidth: {
type: Number
},
prop: {
type: String
},
required: {
type: Boolean,
default: false
},
rules: {
type: [Object, Array]
},
error: {
|
8a83e7c4
梁灏
update Form
|
63
|
type: String
|
9f5e2c7e
梁灏
update Form
|
64
65
66
|
},
validateStatus: {
type: Boolean
|
6986d055
梁灏
Form add show-mes...
|
67
68
69
70
|
},
showMessage: {
type: Boolean,
default: true
|
acb79ba3
梁灏
fixed #433
|
71
72
73
|
},
labelFor: {
type: String
|
9f5e2c7e
梁灏
update Form
|
74
75
|
}
},
|
8a22e84f
梁灏
init Form component
|
76
|
data () {
|
9f5e2c7e
梁灏
update Form
|
77
78
79
80
81
82
83
84
85
86
87
|
return {
prefixCls: prefixCls,
isRequired: false,
validateState: '',
validateMessage: '',
validateDisabled: false,
validator: {}
};
},
watch: {
error (val) {
|
02bf8fab
梁灏
simplify
|
88
89
|
this.validateMessage = val;
this.validateState = val === '' ? '' : 'error';
|
9f5e2c7e
梁灏
update Form
|
90
91
92
|
},
validateStatus (val) {
this.validateState = val;
|
f203b14f
yanyan
form-item watch r...
|
93
|
},
|
ac6fe12b
yanyan
lint
|
94
|
rules (){
|
f203b14f
yanyan
form-item watch r...
|
95
|
this.setRules();
|
9f5e2c7e
梁灏
update Form
|
96
97
|
}
},
|
4e310856
BillyWang
* 采用inject方法,修正问题...
|
98
|
inject: ['form'],
|
9f5e2c7e
梁灏
update Form
|
99
100
101
102
103
104
105
106
107
108
109
|
computed: {
classes () {
return [
`${prefixCls}`,
{
[`${prefixCls}-required`]: this.required || this.isRequired,
[`${prefixCls}-error`]: this.validateState === 'error',
[`${prefixCls}-validating`]: this.validateState === 'validating'
}
];
},
|
4e310856
BillyWang
* 采用inject方法,修正问题...
|
110
111
112
113
114
115
116
|
// form() {
// let parent = this.$parent;
// while (parent.$options.name !== 'iForm') {
// parent = parent.$parent;
// }
// return parent;
// },
|
9f5e2c7e
梁灏
update Form
|
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
|
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: {
|
f203b14f
yanyan
form-item watch r...
|
149
150
151
152
153
154
155
156
157
158
159
160
|
setRules() {
let rules = this.getRules();
if (rules.length) {
rules.every((rule) => {
this.isRequired = rule.required;
});
this.$off('on-form-blur', this.onFieldBlur);
this.$off('on-form-change', this.onFieldChange);
this.$on('on-form-blur', this.onFieldBlur);
this.$on('on-form-change', this.onFieldChange);
}
},
|
9f5e2c7e
梁灏
update Form
|
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
|
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
|
191
|
validator.validate(model, { firstFields: true }, errors => {
|
9f5e2c7e
梁灏
update Form
|
192
193
194
195
196
|
this.validateState = !errors ? 'success' : 'error';
this.validateMessage = errors ? errors[0].message : '';
callback(this.validateMessage);
});
|
c103a2a0
lwio
fix #1534
|
197
|
this.validateDisabled = false;
|
9f5e2c7e
梁灏
update Form
|
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
},
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
|
212
213
214
215
216
217
218
219
|
// 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
|
220
|
this.validateDisabled = true;
|
05e2dda0
梁灏
fixed #768
|
221
222
|
prop.o[prop.k] = [].concat(this.initialValue);
} else {
|
9f5e2c7e
梁灏
update Form
|
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
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
|
239
|
mounted () {
|
9f5e2c7e
梁灏
update Form
|
240
|
if (this.prop) {
|
257f80f1
梁灏
support Form
|
241
|
this.dispatch('iForm', 'on-form-item-add', this);
|
9f5e2c7e
梁灏
update Form
|
242
243
244
245
246
|
Object.defineProperty(this, 'initialValue', {
value: this.fieldValue
});
|
f203b14f
yanyan
form-item watch r...
|
247
|
this.setRules();
|
9f5e2c7e
梁灏
update Form
|
248
|
}
|
8a22e84f
梁灏
init Form component
|
249
|
},
|
9f5e2c7e
梁灏
update Form
|
250
|
beforeDestroy () {
|
257f80f1
梁灏
support Form
|
251
|
this.dispatch('iForm', 'on-form-item-remove', this);
|
9f5e2c7e
梁灏
update Form
|
252
|
}
|
8a22e84f
梁灏
init Form component
|
253
|
};
|
4109caad
吕庆安
Update form-item.vue
|
254
|
</script>
|