cy-element-ui 1.0.77 → 1.0.79

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,403 +1,364 @@
1
- <template>
2
- <div class="el-form-item" :class="[{
3
- 'el-form-item--feedback': elForm && elForm.statusIcon,
4
- 'is-error': validateState === 'error',
5
- 'is-validating': validateState === 'validating',
6
- 'is-success': validateState === 'success',
7
- 'is-required': isRequired || required,
8
- 'is-no-asterisk': elForm && elForm.hideRequiredAsterisk
9
- },
10
- sizeClass ? 'el-form-item--' + sizeClass : ''
11
- ]">
12
- <label-wrap
13
- :is-auto-width="labelStyle && labelStyle.width === 'auto'"
14
- :update-all="form.labelWidth === 'auto'">
15
- <label :for="labelFor" class="el-form-item__label" :style="labelStyle" v-if="label || $slots.label">
16
- <slot name="label">{{ label + form.labelSuffix }}</slot>
17
- </label>
18
- </label-wrap>
19
- <div class="el-form-item__content" :style="contentStyle">
20
- <slot></slot>
21
- <transition name="el-zoom-in-top">
22
- <slot
23
- v-if="validateState === 'error' && showMessage && form.showMessage"
24
- name="error"
25
- :error="validateMessage">
26
- <div
27
- class="el-form-item__error"
28
- :class="{
29
- 'el-form-item__error--inline': typeof inlineMessage === 'boolean'
30
- ? inlineMessage
31
- : (elForm && elForm.inlineMessage || false)
32
- }"
33
- >
34
- {{ validateMessage }}
35
- </div>
36
- </slot>
37
- </transition>
38
- </div>
39
- </div>
40
- </template>
41
- <script>
42
- import AsyncValidator from 'async-validator';
43
- import emitter from '../../../src/mixins/emitter';
44
- import objectAssign from '../../../src/utils/merge';
45
- import { noop, getPropByPath } from '../../../src/utils/util';
46
-
47
- const LabelWrap = {
48
- props: {
49
- isAutoWidth: Boolean,
50
- updateAll: Boolean
51
- },
52
-
53
- inject: ['elForm', 'elFormItem'],
54
-
55
- render: function(h) {
56
- var slots = this.$slots.default;
57
- if (!slots) return null;
58
- if (this.isAutoWidth) {
59
- var autoLabelWidth = this.elForm.autoLabelWidth;
60
- var style = {};
61
- if (autoLabelWidth && autoLabelWidth !== 'auto') {
62
- var marginLeft = parseInt(autoLabelWidth, 10) - this.computedWidth;
63
- if (marginLeft) {
64
- style.marginLeft = marginLeft + 'px';
65
- }
66
- }
67
- return h(
68
- 'div',
69
- { 'class': 'el-form-item__label-wrap', style: style },
70
- [slots]
71
- );
72
- } else {
73
- return slots[0];
74
- }
75
- },
76
-
77
- methods: {
78
- getLabelWidth: function() {
79
- if (this.$el && this.$el.firstElementChild) {
80
- var computedWidth = window.getComputedStyle(this.$el.firstElementChild).width;
81
- return Math.ceil(parseFloat(computedWidth));
82
- } else {
83
- return 0;
84
- }
85
- },
86
- updateLabelWidth: function(action) {
87
- action = action || 'update';
88
- if (this.$slots.default && this.isAutoWidth && this.$el.firstElementChild) {
89
- if (action === 'update') {
90
- this.computedWidth = this.getLabelWidth();
91
- } else if (action === 'remove') {
92
- this.elForm.deregisterLabelWidth(this.computedWidth);
93
- }
94
- }
95
- }
96
- },
97
-
98
- watch: {
99
- computedWidth: function(val, oldVal) {
100
- if (this.updateAll) {
101
- this.elForm.registerLabelWidth(val, oldVal);
102
- this.elFormItem.updateComputedLabelWidth(val);
103
- }
104
- }
105
- },
106
-
107
- data: function() {
108
- return {
109
- computedWidth: 0
110
- };
111
- },
112
-
113
- mounted: function() {
114
- this.updateLabelWidth('update');
115
- },
116
-
117
- updated: function() {
118
- this.updateLabelWidth('update');
119
- },
120
-
121
- beforeDestroy: function() {
122
- this.updateLabelWidth('remove');
123
- }
124
- };
125
-
126
- export default {
127
- name: 'ElFormItem',
128
-
129
- componentName: 'ElFormItem',
130
-
131
- mixins: [emitter],
132
-
133
- provide() {
134
- return {
135
- elFormItem: this
136
- };
137
- },
138
-
139
- inject: ['elForm'],
140
-
141
- props: {
142
- label: String,
143
- labelWidth: String,
144
- prop: String,
145
- required: {
146
- type: Boolean,
147
- default: undefined
148
- },
149
- rules: [Object, Array],
150
- error: String,
151
- validateStatus: String,
152
- for: String,
153
- inlineMessage: {
154
- type: [String, Boolean],
155
- default: ''
156
- },
157
- showMessage: {
158
- type: Boolean,
159
- default: true
160
- },
161
- size: String
162
- },
163
- components: {
164
- // use this component to calculate auto width
165
- LabelWrap
166
- },
167
- watch: {
168
- error: {
169
- immediate: true,
170
- handler(value) {
171
- this.validateMessage = value;
172
- this.validateState = value ? 'error' : '';
173
- }
174
- },
175
- validateStatus(value) {
176
- this.validateState = value;
177
- },
178
- rules(value) {
179
- if ((!value || value.length === 0) && this.required === undefined) {
180
- this.clearValidate();
181
- }
182
- }
183
- },
184
- computed: {
185
- labelFor() {
186
- return this.for || this.prop;
187
- },
188
- labelStyle() {
189
- const ret = {};
190
- if (this.form.labelPosition === 'top') return ret;
191
- const labelWidth = this.labelWidth || this.form.labelWidth;
192
- if (labelWidth) {
193
- ret.width = labelWidth;
194
- }
195
- return ret;
196
- },
197
- contentStyle() {
198
- const ret = {};
199
- const label = this.label;
200
- if (this.form.labelPosition === 'top' || this.form.inline) return ret;
201
- if (!label && !this.labelWidth && this.isNested) return ret;
202
- const labelWidth = this.labelWidth || this.form.labelWidth;
203
- if (labelWidth === 'auto') {
204
- if (this.labelWidth === 'auto') {
205
- ret.marginLeft = this.computedLabelWidth;
206
- } else if (this.form.labelWidth === 'auto') {
207
- ret.marginLeft = this.elForm.autoLabelWidth;
208
- }
209
- } else {
210
- ret.marginLeft = labelWidth;
211
- }
212
- return ret;
213
- },
214
- form() {
215
- let parent = this.$parent;
216
- let parentName = parent.$options.componentName;
217
- while (parentName !== 'ElForm') {
218
- if (parentName === 'ElFormItem') {
219
- this.isNested = true;
220
- }
221
- parent = parent.$parent;
222
- parentName = parent.$options.componentName;
223
- }
224
- return parent;
225
- },
226
- fieldValue() {
227
- const model = this.form.model;
228
- if (!model || !this.prop) { return; }
229
-
230
- let path = this.prop;
231
- if (path.indexOf(':') !== -1) {
232
- path = path.replace(/:/, '.');
233
- }
234
-
235
- return getPropByPath(model, path, true).v;
236
- },
237
- isRequired() {
238
- let rules = this.getRules();
239
- let isRequired = false;
240
-
241
- if (rules && rules.length) {
242
- rules.every(rule => {
243
- if (rule.required) {
244
- isRequired = true;
245
- return false;
246
- }
247
- return true;
248
- });
249
- }
250
- return isRequired;
251
- },
252
- _formSize() {
253
- return this.elForm.size;
254
- },
255
- elFormItemSize() {
256
- return this.size || this._formSize;
257
- },
258
- sizeClass() {
259
- return this.elFormItemSize || (this.$ELEMENT || {}).size;
260
- }
261
- },
262
- data() {
263
- return {
264
- validateState: '',
265
- validateMessage: '',
266
- validateDisabled: false,
267
- validator: {},
268
- isNested: false,
269
- computedLabelWidth: ''
270
- };
271
- },
272
- methods: {
273
- validate(trigger, callback = noop) {
274
- this.validateDisabled = false;
275
- const rules = this.getFilteredRule(trigger);
276
- if ((!rules || rules.length === 0) && this.required === undefined) {
277
- callback();
278
- return true;
279
- }
280
-
281
- this.validateState = 'validating';
282
-
283
- const descriptor = {};
284
- if (rules && rules.length > 0) {
285
- rules.forEach(rule => {
286
- delete rule.trigger;
287
- });
288
- }
289
- descriptor[this.prop] = rules;
290
-
291
- const validator = new AsyncValidator(descriptor);
292
- const model = {};
293
-
294
- model[this.prop] = this.fieldValue;
295
-
296
- validator.validate(model, { firstFields: true }, (errors, invalidFields) => {
297
- this.validateState = !errors ? 'success' : 'error';
298
- this.validateMessage = errors ? errors[0].message : '';
299
-
300
- callback(this.validateMessage, invalidFields);
301
- this.elForm && this.elForm.$emit('validate', this.prop, !errors, this.validateMessage || null);
302
- });
303
- },
304
- clearValidate() {
305
- this.validateState = '';
306
- this.validateMessage = '';
307
- this.validateDisabled = false;
308
- },
309
- resetField() {
310
- this.validateState = '';
311
- this.validateMessage = '';
312
-
313
- let model = this.form.model;
314
- let value = this.fieldValue;
315
- let path = this.prop;
316
- if (path.indexOf(':') !== -1) {
317
- path = path.replace(/:/, '.');
318
- }
319
-
320
- let prop = getPropByPath(model, path, true);
321
-
322
- this.validateDisabled = true;
323
- if (Array.isArray(value)) {
324
- prop.o[prop.k] = [].concat(this.initialValue);
325
- } else {
326
- prop.o[prop.k] = this.initialValue;
327
- }
328
-
329
- // reset validateDisabled after onFieldChange triggered
330
- this.$nextTick(() => {
331
- this.validateDisabled = false;
332
- });
333
-
334
- this.broadcast('ElTimeSelect', 'fieldReset', this.initialValue);
335
- },
336
- getRules() {
337
- let formRules = this.form.rules;
338
- const selfRules = this.rules;
339
- const requiredRule = this.required !== undefined ? { required: !!this.required } : [];
340
-
341
- const prop = getPropByPath(formRules, this.prop || '');
342
- formRules = formRules ? (prop.o[this.prop || ''] || prop.v) : [];
343
-
344
- return [].concat(selfRules || formRules || []).concat(requiredRule);
345
- },
346
- getFilteredRule(trigger) {
347
- const rules = this.getRules();
348
-
349
- return rules.filter(rule => {
350
- if (!rule.trigger || trigger === '') return true;
351
- if (Array.isArray(rule.trigger)) {
352
- return rule.trigger.indexOf(trigger) > -1;
353
- } else {
354
- return rule.trigger === trigger;
355
- }
356
- }).map(rule => objectAssign({}, rule));
357
- },
358
- onFieldBlur() {
359
- this.validate('blur');
360
- },
361
- onFieldChange() {
362
- if (this.validateDisabled) {
363
- this.validateDisabled = false;
364
- return;
365
- }
366
-
367
- this.validate('change');
368
- },
369
- updateComputedLabelWidth(width) {
370
- this.computedLabelWidth = width ? `${ width }px` : '';
371
- },
372
- addValidateEvents() {
373
- const rules = this.getRules();
374
-
375
- if (rules.length || this.required !== undefined) {
376
- this.$on('el.form.blur', this.onFieldBlur);
377
- this.$on('el.form.change', this.onFieldChange);
378
- }
379
- },
380
- removeValidateEvents() {
381
- this.$off();
382
- }
383
- },
384
- mounted() {
385
- if (this.prop) {
386
- this.dispatch('ElForm', 'el.form.addField', [this]);
387
-
388
- let initialValue = this.fieldValue;
389
- if (Array.isArray(initialValue)) {
390
- initialValue = [].concat(initialValue);
391
- }
392
- Object.defineProperty(this, 'initialValue', {
393
- value: initialValue
394
- });
395
-
396
- this.addValidateEvents();
397
- }
398
- },
399
- beforeDestroy() {
400
- this.dispatch('ElForm', 'el.form.removeField', [this]);
401
- }
402
- };
403
- </script>
1
+ <template>
2
+ <div class="el-form-item" :class="[{
3
+ 'el-form-item--feedback': elForm && elForm.statusIcon,
4
+ 'is-error': validateState === 'error',
5
+ 'is-validating': validateState === 'validating',
6
+ 'is-success': validateState === 'success',
7
+ 'is-required': isRequired || required,
8
+ 'is-no-asterisk': elForm && elForm.hideRequiredAsterisk
9
+ },
10
+ sizeClass ? 'el-form-item--' + sizeClass : ''
11
+ ]">
12
+ <template v-if="labelStyle && labelStyle.width === 'auto'">
13
+ <div class="el-form-item__label-wrap" :style="labelWrapStyle">
14
+ <label :for="labelFor" class="el-form-item__label" :style="labelStyle" v-if="label || $slots.label">
15
+ <slot name="label">{{ label + form.labelSuffix }}</slot>
16
+ </label>
17
+ </div>
18
+ </template>
19
+ <template v-else>
20
+ <label :for="labelFor" class="el-form-item__label" :style="labelStyle" v-if="label || $slots.label">
21
+ <slot name="label">{{ label + form.labelSuffix }}</slot>
22
+ </label>
23
+ </template>
24
+ <div class="el-form-item__content" :style="contentStyle">
25
+ <slot></slot>
26
+ <transition name="el-zoom-in-top">
27
+ <slot
28
+ v-if="validateState === 'error' && showMessage && form.showMessage"
29
+ name="error"
30
+ :error="validateMessage">
31
+ <div
32
+ class="el-form-item__error"
33
+ :class="{
34
+ 'el-form-item__error--inline': typeof inlineMessage === 'boolean'
35
+ ? inlineMessage
36
+ : (elForm && elForm.inlineMessage || false)
37
+ }"
38
+ >
39
+ {{ validateMessage }}
40
+ </div>
41
+ </slot>
42
+ </transition>
43
+ </div>
44
+ </div>
45
+ </template>
46
+ <script>
47
+ import AsyncValidator from 'async-validator';
48
+ import emitter from '../../../src/mixins/emitter';
49
+ import objectAssign from '../../../src/utils/merge';
50
+ import { noop, getPropByPath } from '../../../src/utils/util';
51
+
52
+ export default {
53
+ name: 'ElFormItem',
54
+
55
+ componentName: 'ElFormItem',
56
+
57
+ mixins: [emitter],
58
+
59
+ provide() {
60
+ return {
61
+ elFormItem: this
62
+ };
63
+ },
64
+
65
+ inject: ['elForm'],
66
+
67
+ props: {
68
+ label: String,
69
+ labelWidth: String,
70
+ prop: String,
71
+ required: {
72
+ type: Boolean,
73
+ default: undefined
74
+ },
75
+ rules: [Object, Array],
76
+ error: String,
77
+ validateStatus: String,
78
+ for: String,
79
+ inlineMessage: {
80
+ type: [String, Boolean],
81
+ default: ''
82
+ },
83
+ showMessage: {
84
+ type: Boolean,
85
+ default: true
86
+ },
87
+ size: String
88
+ },
89
+ watch: {
90
+ error: {
91
+ immediate: true,
92
+ handler(value) {
93
+ this.validateMessage = value;
94
+ this.validateState = value ? 'error' : '';
95
+ }
96
+ },
97
+ validateStatus(value) {
98
+ this.validateState = value;
99
+ },
100
+ rules(value) {
101
+ if ((!value || value.length === 0) && this.required === undefined) {
102
+ this.clearValidate();
103
+ }
104
+ },
105
+ computedLabelWidth(val, oldVal) {
106
+ if (this.form.labelWidth === 'auto') {
107
+ this.elForm.registerLabelWidth(val, oldVal);
108
+ }
109
+ }
110
+ },
111
+ computed: {
112
+ labelFor() {
113
+ return this.for || this.prop;
114
+ },
115
+ labelStyle() {
116
+ const ret = {};
117
+ if (this.form.labelPosition === 'top') return ret;
118
+ const labelWidth = this.labelWidth || this.form.labelWidth;
119
+ if (labelWidth) {
120
+ ret.width = labelWidth;
121
+ }
122
+ return ret;
123
+ },
124
+ labelWrapStyle() {
125
+ const style = {};
126
+ const autoLabelWidth = this.elForm.autoLabelWidth;
127
+ if (autoLabelWidth && autoLabelWidth !== 'auto') {
128
+ const marginLeft = parseInt(autoLabelWidth, 10) - this.computedLabelWidth;
129
+ if (marginLeft) {
130
+ style.marginLeft = marginLeft + 'px';
131
+ }
132
+ }
133
+ return style;
134
+ },
135
+ contentStyle() {
136
+ const ret = {};
137
+ const label = this.label;
138
+ if (this.form.labelPosition === 'top' || this.form.inline) return ret;
139
+ if (!label && !this.labelWidth && this.isNested) return ret;
140
+ const labelWidth = this.labelWidth || this.form.labelWidth;
141
+ if (labelWidth === 'auto') {
142
+ if (this.labelWidth === 'auto') {
143
+ ret.marginLeft = this.computedLabelWidth;
144
+ } else if (this.form.labelWidth === 'auto') {
145
+ ret.marginLeft = this.elForm.autoLabelWidth;
146
+ }
147
+ } else {
148
+ ret.marginLeft = labelWidth;
149
+ }
150
+ return ret;
151
+ },
152
+ form() {
153
+ let parent = this.$parent;
154
+ let parentName = parent.$options.componentName;
155
+ while (parentName !== 'ElForm') {
156
+ if (parentName === 'ElFormItem') {
157
+ this.isNested = true;
158
+ }
159
+ parent = parent.$parent;
160
+ parentName = parent.$options.componentName;
161
+ }
162
+ return parent;
163
+ },
164
+ fieldValue() {
165
+ const model = this.form.model;
166
+ if (!model || !this.prop) { return; }
167
+
168
+ let path = this.prop;
169
+ if (path.indexOf(':') !== -1) {
170
+ path = path.replace(/:/, '.');
171
+ }
172
+
173
+ return getPropByPath(model, path, true).v;
174
+ },
175
+ isRequired() {
176
+ let rules = this.getRules();
177
+ let isRequired = false;
178
+
179
+ if (rules && rules.length) {
180
+ rules.every(rule => {
181
+ if (rule.required) {
182
+ isRequired = true;
183
+ return false;
184
+ }
185
+ return true;
186
+ });
187
+ }
188
+ return isRequired;
189
+ },
190
+ _formSize() {
191
+ return this.elForm.size;
192
+ },
193
+ elFormItemSize() {
194
+ return this.size || this._formSize;
195
+ },
196
+ sizeClass() {
197
+ return this.elFormItemSize || (this.$ELEMENT || {}).size;
198
+ }
199
+ },
200
+ data() {
201
+ return {
202
+ validateState: '',
203
+ validateMessage: '',
204
+ validateDisabled: false,
205
+ validator: {},
206
+ isNested: false,
207
+ computedLabelWidth: 0
208
+ };
209
+ },
210
+ methods: {
211
+ getLabelWidth() {
212
+ if (this.$el && this.$el.querySelector('.el-form-item__label')) {
213
+ const labelEl = this.$el.querySelector('.el-form-item__label');
214
+ const computedWidth = window.getComputedStyle(labelEl).width;
215
+ return Math.ceil(parseFloat(computedWidth));
216
+ } else {
217
+ return 0;
218
+ }
219
+ },
220
+ updateLabelWidth(action) {
221
+ action = action || 'update';
222
+ if ((this.label || this.$slots.label) && this.form.labelWidth === 'auto') {
223
+ if (action === 'update') {
224
+ this.computedLabelWidth = this.getLabelWidth();
225
+ } else if (action === 'remove') {
226
+ this.elForm.deregisterLabelWidth(this.computedLabelWidth);
227
+ }
228
+ }
229
+ },
230
+ validate(trigger, callback = noop) {
231
+ this.validateDisabled = false;
232
+ const rules = this.getFilteredRule(trigger);
233
+ if ((!rules || rules.length === 0) && this.required === undefined) {
234
+ callback();
235
+ return true;
236
+ }
237
+
238
+ this.validateState = 'validating';
239
+
240
+ const descriptor = {};
241
+ if (rules && rules.length > 0) {
242
+ rules.forEach(rule => {
243
+ delete rule.trigger;
244
+ });
245
+ }
246
+ descriptor[this.prop] = rules;
247
+
248
+ const validator = new AsyncValidator(descriptor);
249
+ const model = {};
250
+
251
+ model[this.prop] = this.fieldValue;
252
+
253
+ validator.validate(model, { firstFields: true }, (errors, invalidFields) => {
254
+ this.validateState = !errors ? 'success' : 'error';
255
+ this.validateMessage = errors ? errors[0].message : '';
256
+
257
+ callback(this.validateMessage, invalidFields);
258
+ this.elForm && this.elForm.$emit('validate', this.prop, !errors, this.validateMessage || null);
259
+ });
260
+ },
261
+ clearValidate() {
262
+ this.validateState = '';
263
+ this.validateMessage = '';
264
+ this.validateDisabled = false;
265
+ },
266
+ resetField() {
267
+ this.validateState = '';
268
+ this.validateMessage = '';
269
+
270
+ let model = this.form.model;
271
+ let value = this.fieldValue;
272
+ let path = this.prop;
273
+ if (path.indexOf(':') !== -1) {
274
+ path = path.replace(/:/, '.');
275
+ }
276
+
277
+ let prop = getPropByPath(model, path, true);
278
+
279
+ this.validateDisabled = true;
280
+ if (Array.isArray(value)) {
281
+ prop.o[prop.k] = [].concat(this.initialValue);
282
+ } else {
283
+ prop.o[prop.k] = this.initialValue;
284
+ }
285
+
286
+ this.$nextTick(() => {
287
+ this.validateDisabled = false;
288
+ });
289
+
290
+ this.broadcast('ElTimeSelect', 'fieldReset', this.initialValue);
291
+ },
292
+ getRules() {
293
+ let formRules = this.form.rules;
294
+ const selfRules = this.rules;
295
+ const requiredRule = this.required !== undefined ? { required: !!this.required } : [];
296
+
297
+ const prop = getPropByPath(formRules, this.prop || '');
298
+ formRules = formRules ? (prop.o[this.prop || ''] || prop.v) : [];
299
+
300
+ return [].concat(selfRules || formRules || []).concat(requiredRule);
301
+ },
302
+ getFilteredRule(trigger) {
303
+ const rules = this.getRules();
304
+
305
+ return rules.filter(rule => {
306
+ if (!rule.trigger || trigger === '') return true;
307
+ if (Array.isArray(rule.trigger)) {
308
+ return rule.trigger.indexOf(trigger) > -1;
309
+ } else {
310
+ return rule.trigger === trigger;
311
+ }
312
+ }).map(rule => objectAssign({}, rule));
313
+ },
314
+ onFieldBlur() {
315
+ this.validate('blur');
316
+ },
317
+ onFieldChange() {
318
+ if (this.validateDisabled) {
319
+ this.validateDisabled = false;
320
+ return;
321
+ }
322
+
323
+ this.validate('change');
324
+ },
325
+ updateComputedLabelWidth(width) {
326
+ this.computedLabelWidth = width ? `${ width }px` : '';
327
+ },
328
+ addValidateEvents() {
329
+ const rules = this.getRules();
330
+
331
+ if (rules.length || this.required !== undefined) {
332
+ this.$on('el.form.blur', this.onFieldBlur);
333
+ this.$on('el.form.change', this.onFieldChange);
334
+ }
335
+ },
336
+ removeValidateEvents() {
337
+ this.$off();
338
+ }
339
+ },
340
+ mounted() {
341
+ this.updateLabelWidth('update');
342
+ if (this.prop) {
343
+ this.dispatch('ElForm', 'el.form.addField', [this]);
344
+
345
+ let initialValue = this.fieldValue;
346
+ if (Array.isArray(initialValue)) {
347
+ initialValue = [].concat(initialValue);
348
+ }
349
+ Object.defineProperty(this, 'initialValue', {
350
+ value: initialValue
351
+ });
352
+
353
+ this.addValidateEvents();
354
+ }
355
+ },
356
+ updated() {
357
+ this.updateLabelWidth('update');
358
+ },
359
+ beforeDestroy() {
360
+ this.updateLabelWidth('remove');
361
+ this.dispatch('ElForm', 'el.form.removeField', [this]);
362
+ }
363
+ };
364
+ </script>