Blame view

src/components/form/form.vue 2.3 KB
8a22e84f   梁灏   init Form component
1
  <template>
9f5e2c7e   梁灏   update Form
2
      <form :class="classes"><slot></slot></form>
8a22e84f   梁灏   init Form component
3
4
  </template>
  <script>
9f5e2c7e   梁灏   update Form
5
6
7
8
      // https://github.com/ElemeFE/element/blob/dev/packages/form/src/form.vue
  
      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
21
22
23
24
25
          name: 'iForm',
          props: {
              model: {
                  type: Object
              },
              rules: {
                  type: Object
              },
              labelWidth: {
                  type: Number
              },
              inline: {
                  type: Boolean,
                  default: false
              }
          },
8a22e84f   梁灏   init Form component
26
          data () {
9f5e2c7e   梁灏   update Form
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
              return {
                  fields: []
              };
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-inline`]: this.inline
                      }
                  ];
              }
          },
          methods: {
              resetFields() {
                  this.fields.forEach(field => {
                      field.resetField();
                  });
              },
              validate(callback) {
                  let valid = true;
                  let count = 0;
                  this.fields.forEach(field => {
                      field.validate('', errors => {
                          if (errors) {
                              valid = false;
                          }
                          if (typeof callback === 'function' && ++count === this.fields.length) {
                              callback(valid);
                          }
                      });
                  });
              },
              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);
              }
          },
          watch: {
              rules() {
                  this.validate();
              }
8a22e84f   梁灏   init Form component
72
          },
9f5e2c7e   梁灏   update Form
73
74
75
76
77
78
79
80
81
82
          events: {
              'on-form-item-add' (field) {
                  if (field) this.fields.push(field);
                  return false;
              },
              'on-form-item-remove' (field) {
                  if (field.prop) this.fields.splice(this.fields.indexOf(field), 1);
                  return false;
              }
          }
8a22e84f   梁灏   init Form component
83
84
      };
  </script>