iview/src/components/form/form.vue

96 lines
2.7 KiB
Vue
Raw Normal View History

<template>
2017-01-03 16:18:21 +08:00
<form :class="classes"><slot></slot></form>
</template>
<script>
2017-01-03 16:18:21 +08:00
// https://github.com/ElemeFE/element/blob/dev/packages/form/src/form.vue
2017-01-03 18:13:59 +08:00
import { oneOf } from '../../utils/assist';
2017-01-03 16:18:21 +08:00
const prefixCls = 'ivu-form';
export default {
2017-01-03 16:18:21 +08:00
name: 'iForm',
props: {
model: {
type: Object
},
rules: {
type: Object
},
labelWidth: {
type: Number
},
2017-01-03 18:13:59 +08:00
labelPosition: {
validator (value) {
return oneOf(value, ['left', 'right', 'top']);
},
default: 'right'
},
2017-01-03 16:18:21 +08:00
inline: {
type: Boolean,
default: false
},
showMessage: {
type: Boolean,
default: true
2017-01-03 16:18:21 +08:00
}
},
data () {
2017-01-03 16:18:21 +08:00
return {
fields: []
};
},
computed: {
classes () {
return [
`${prefixCls}`,
2017-01-03 18:13:59 +08:00
`${prefixCls}-label-${this.labelPosition}`,
2017-01-03 16:18:21 +08:00
{
[`${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();
}
},
2017-01-03 16:18:21 +08:00
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>