Blame view

src/components/form/form.vue 3.22 KB
8a22e84f   梁灏   init Form component
1
  <template>
c17c5ad6   Sergio Crisostomo   normalize autocom...
2
      <form :class="classes" :autocomplete="autocomplete"><slot></slot></form>
8a22e84f   梁灏   init Form component
3
4
  </template>
  <script>
184dba1c   梁灏   update Form
5
      import { oneOf } from '../../utils/assist';
9f5e2c7e   梁灏   update Form
6
7
8
  
      const prefixCls = 'ivu-form';
  
8a22e84f   梁灏   init Form component
9
      export default {
9f5e2c7e   梁灏   update Form
10
11
12
13
14
15
16
17
18
19
20
          name: 'iForm',
          props: {
              model: {
                  type: Object
              },
              rules: {
                  type: Object
              },
              labelWidth: {
                  type: Number
              },
184dba1c   梁灏   update Form
21
22
23
24
25
26
              labelPosition: {
                  validator (value) {
                      return oneOf(value, ['left', 'right', 'top']);
                  },
                  default: 'right'
              },
9f5e2c7e   梁灏   update Form
27
28
29
              inline: {
                  type: Boolean,
                  default: false
6986d055   梁灏   Form add show-mes...
30
31
32
33
              },
              showMessage: {
                  type: Boolean,
                  default: true
c17c5ad6   Sergio Crisostomo   normalize autocom...
34
35
36
37
38
39
              },
              autocomplete: {
                  validator (value) {
                      return oneOf(value, ['on', 'off']);
                  },
                  default: 'off'
9f5e2c7e   梁灏   update Form
40
41
              }
          },
4e310856   BillyWang   * 采用inject方法,修正问题...
42
43
44
          provide() {
              return { form : this };
          },
8a22e84f   梁灏   init Form component
45
          data () {
9f5e2c7e   梁灏   update Form
46
47
48
49
50
51
52
53
              return {
                  fields: []
              };
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
184dba1c   梁灏   update Form
54
                      `${prefixCls}-label-${this.labelPosition}`,
9f5e2c7e   梁灏   update Form
55
56
57
58
59
60
61
62
63
64
65
66
67
                      {
                          [`${prefixCls}-inline`]: this.inline
                      }
                  ];
              }
          },
          methods: {
              resetFields() {
                  this.fields.forEach(field => {
                      field.resetField();
                  });
              },
              validate(callback) {
0c0683e4   Haven   feat(form) : supp...
68
69
70
71
72
73
74
75
76
77
                  return new Promise(resolve => {
                      let valid = true;
                      let count = 0;
                      this.fields.forEach(field => {
                          field.validate('', errors => {
                              if (errors) {
                                  valid = false;
                              }
                              if (++count === this.fields.length) {
                                  // all finish
c17c5ad6   Sergio Crisostomo   normalize autocom...
78
                                  resolve(valid);
0c0683e4   Haven   feat(form) : supp...
79
80
81
82
83
                                  if (typeof callback === 'function') {
                                      callback(valid);
                                  }
                              }
                          });
9f5e2c7e   梁灏   update Form
84
                      });
c17c5ad6   Sergio Crisostomo   normalize autocom...
85
                  });
9f5e2c7e   梁灏   update Form
86
87
88
89
90
91
              },
              validateField(prop, cb) {
                  const field = this.fields.filter(field => field.prop === prop)[0];
                  if (!field) { throw new Error('[iView warn]: must call validateField with valid prop string!'); }
  
                  field.validate('', cb);
9f5e2c7e   梁灏   update Form
92
93
94
95
96
97
              }
          },
          watch: {
              rules() {
                  this.validate();
              }
8a22e84f   梁灏   init Form component
98
          },
257f80f1   梁灏   support Form
99
100
          created () {
              this.$on('on-form-item-add', (field) => {
9f5e2c7e   梁灏   update Form
101
102
                  if (field) this.fields.push(field);
                  return false;
257f80f1   梁灏   support Form
103
104
              });
              this.$on('on-form-item-remove', (field) => {
9f5e2c7e   梁灏   update Form
105
106
                  if (field.prop) this.fields.splice(this.fields.indexOf(field), 1);
                  return false;
257f80f1   梁灏   support Form
107
              });
9f5e2c7e   梁灏   update Form
108
          }
8a22e84f   梁灏   init Form component
109
      };
d22e3671   marvinwilliam   fix form default ...
110
  </script>