Blame view

src/components/form/form.vue 2.86 KB
8a22e84f   梁灏   init Form component
1
  <template>
d22e3671   marvinwilliam   fix form default ...
2
3
4
      <form :class="classes" @submit="formSubmit">
          <slot></slot>
      </form>
8a22e84f   梁灏   init Form component
5
6
  </template>
  <script>
9f5e2c7e   梁灏   update Form
7
      // https://github.com/ElemeFE/element/blob/dev/packages/form/src/form.vue
184dba1c   梁灏   update Form
8
      import { oneOf } from '../../utils/assist';
9f5e2c7e   梁灏   update Form
9
10
11
  
      const prefixCls = 'ivu-form';
  
8a22e84f   梁灏   init Form component
12
      export default {
9f5e2c7e   梁灏   update Form
13
14
15
16
17
18
19
20
21
22
23
          name: 'iForm',
          props: {
              model: {
                  type: Object
              },
              rules: {
                  type: Object
              },
              labelWidth: {
                  type: Number
              },
184dba1c   梁灏   update Form
24
25
26
27
28
29
              labelPosition: {
                  validator (value) {
                      return oneOf(value, ['left', 'right', 'top']);
                  },
                  default: 'right'
              },
9f5e2c7e   梁灏   update Form
30
31
32
              inline: {
                  type: Boolean,
                  default: false
6986d055   梁灏   Form add show-mes...
33
34
35
36
              },
              showMessage: {
                  type: Boolean,
                  default: true
9f5e2c7e   梁灏   update Form
37
38
              }
          },
8a22e84f   梁灏   init Form component
39
          data () {
9f5e2c7e   梁灏   update Form
40
41
42
43
44
45
46
47
              return {
                  fields: []
              };
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
184dba1c   梁灏   update Form
48
                      `${prefixCls}-label-${this.labelPosition}`,
9f5e2c7e   梁灏   update Form
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
                      {
                          [`${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);
d22e3671   marvinwilliam   fix form default ...
80
81
82
              },
              formSubmit(event){
                  event.preventDefault();
9f5e2c7e   梁灏   update Form
83
84
85
86
87
88
              }
          },
          watch: {
              rules() {
                  this.validate();
              }
8a22e84f   梁灏   init Form component
89
          },
257f80f1   梁灏   support Form
90
91
          created () {
              this.$on('on-form-item-add', (field) => {
9f5e2c7e   梁灏   update Form
92
93
                  if (field) this.fields.push(field);
                  return false;
257f80f1   梁灏   support Form
94
95
              });
              this.$on('on-form-item-remove', (field) => {
9f5e2c7e   梁灏   update Form
96
97
                  if (field.prop) this.fields.splice(this.fields.indexOf(field), 1);
                  return false;
257f80f1   梁灏   support Form
98
              });
9f5e2c7e   梁灏   update Form
99
          }
8a22e84f   梁灏   init Form component
100
      };
d22e3671   marvinwilliam   fix form default ...
101
  </script>