update Form

update Form
This commit is contained in:
梁灏 2017-01-03 16:18:21 +08:00
parent 103cd35313
commit 9f5e2c7e4d
5 changed files with 326 additions and 13 deletions

View file

@ -1,13 +1,84 @@
<template>
<form :class="classes"><slot></slot></form>
</template>
<script>
// https://github.com/ElemeFE/element/blob/dev/packages/form/src/form.vue
const prefixCls = 'ivu-form';
export default {
props: {},
data () {
return {};
name: 'iForm',
props: {
model: {
type: Object
},
rules: {
type: Object
},
labelWidth: {
type: Number
},
inline: {
type: Boolean,
default: false
}
},
computed: {},
methods: {}
data () {
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();
}
},
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;
}
}
};
</script>