Blame view

src/components/form/form-item.vue 8.43 KB
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;
              // },
856cf774   梁灏   close #5313
117
118
119
              fieldValue () {
                  const model = this.form.model;
                  if (!model || !this.prop) { return; }
9f5e2c7e   梁灏   update Form
120
  
856cf774   梁灏   close #5313
121
122
123
                  let path = this.prop;
                  if (path.indexOf(':') !== -1) {
                      path = path.replace(/:/, '.');
9f5e2c7e   梁灏   update Form
124
                  }
856cf774   梁灏   close #5313
125
126
  
                  return getPropByPath(model, path).v;
9f5e2c7e   梁灏   update Form
127
128
129
              },
              labelStyles () {
                  let style = {};
e9f3d00f   梁灏   fixed Form label ...
130
131
132
                  const labelWidth = this.labelWidth === 0 || this.labelWidth ? this.labelWidth : this.form.labelWidth;
  
                  if (labelWidth || labelWidth === 0) {
9f5e2c7e   梁灏   update Form
133
134
135
136
137
138
                      style.width = `${labelWidth}px`;
                  }
                  return style;
              },
              contentStyles () {
                  let style = {};
e9f3d00f   梁灏   fixed Form label ...
139
140
141
                  const labelWidth = this.labelWidth === 0 || this.labelWidth ? this.labelWidth : this.form.labelWidth;
  
                  if (labelWidth || labelWidth === 0) {
9f5e2c7e   梁灏   update Form
142
143
144
145
146
147
                      style.marginLeft = `${labelWidth}px`;
                  }
                  return style;
              }
          },
          methods: {
f203b14f   yanyan   form-item watch r...
148
149
              setRules() {
                  let rules = this.getRules();
16f8b759   Eager   fix(FormItem): 解决...
150
151
152
                  if (rules.length&&this.required) {
                      return;
                  }else if (rules.length) {
f203b14f   yanyan   form-item watch r...
153
154
155
                      rules.every((rule) => {
                          this.isRequired = rule.required;
                      });
16f8b759   Eager   fix(FormItem): 解决...
156
157
                  }else if (this.required){
                      this.isRequired = this.required;
f203b14f   yanyan   form-item watch r...
158
                  }
16f8b759   Eager   fix(FormItem): 解决...
159
160
161
162
                  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);
f203b14f   yanyan   form-item watch r...
163
              },
9f5e2c7e   梁灏   update Form
164
165
166
167
168
169
170
171
172
173
174
175
176
177
              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 () {}) {
16f8b759   Eager   fix(FormItem): 解决...
178
                  let rules = this.getFilteredRule(trigger);
9f5e2c7e   梁灏   update Form
179
                  if (!rules || rules.length === 0) {
16f8b759   Eager   fix(FormItem): 解决...
180
181
182
183
184
185
                      if (!this.required) {
                          callback();
                          return true;
                      }else {
                          rules = [{required: true}];
                      }
9f5e2c7e   梁灏   update Form
186
187
188
189
190
191
192
193
194
195
196
197
                  }
  
                  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
198
                  validator.validate(model, { firstFields: true }, errors => {
9f5e2c7e   梁灏   update Form
199
200
201
202
203
                      this.validateState = !errors ? 'success' : 'error';
                      this.validateMessage = errors ? errors[0].message : '';
  
                      callback(this.validateMessage);
                  });
c103a2a0   lwio   fix #1534
204
                  this.validateDisabled = false;
9f5e2c7e   梁灏   update Form
205
206
207
208
209
210
211
212
213
214
215
216
217
218
              },
              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
219
220
221
222
223
224
225
226
  //                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
227
                      this.validateDisabled = true;
05e2dda0   梁灏   fixed #768
228
229
                      prop.o[prop.k] = [].concat(this.initialValue);
                  } else {
9f5e2c7e   梁灏   update Form
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
                      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
246
          mounted () {
9f5e2c7e   梁灏   update Form
247
              if (this.prop) {
257f80f1   梁灏   support Form
248
                  this.dispatch('iForm', 'on-form-item-add', this);
9f5e2c7e   梁灏   update Form
249
250
251
252
253
  
                  Object.defineProperty(this, 'initialValue', {
                      value: this.fieldValue
                  });
  
f203b14f   yanyan   form-item watch r...
254
                  this.setRules();
9f5e2c7e   梁灏   update Form
255
              }
8a22e84f   梁灏   init Form component
256
          },
9f5e2c7e   梁灏   update Form
257
          beforeDestroy () {
257f80f1   梁灏   support Form
258
              this.dispatch('iForm', 'on-form-item-remove', this);
9f5e2c7e   梁灏   update Form
259
          }
8a22e84f   梁灏   init Form component
260
      };
4109caad   吕庆安   Update form-item.vue
261
  </script>