Blame view

src/components/form/form-item.vue 7.49 KB
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>
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
194
195
196
                      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 ...
197
                  } else if (value !== this.initialValue) {
9f5e2c7e   梁灏   update Form
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
                      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
214
          mounted () {
9f5e2c7e   梁灏   update Form
215
              if (this.prop) {
257f80f1   梁灏   support Form
216
                  this.dispatch('iForm', 'on-form-item-add', this);
9f5e2c7e   梁灏   update Form
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  
                  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
231
232
                      this.$on('on-form-blur', this.onFieldBlur);
                      this.$on('on-form-change', this.onFieldChange);
9f5e2c7e   梁灏   update Form
233
234
                  }
              }
8a22e84f   梁灏   init Form component
235
          },
9f5e2c7e   梁灏   update Form
236
          beforeDestroy () {
257f80f1   梁灏   support Form
237
              this.dispatch('iForm', 'on-form-item-remove', this);
9f5e2c7e   梁灏   update Form
238
          }
8a22e84f   梁灏   init Form component
239
240
      };
  </script>