feat(Form): add submit、failed event

This commit is contained in:
陈嘉涵 2020-02-07 15:51:58 +08:00
parent 9d8cf7f8d3
commit 70ed34aa24
3 changed files with 26 additions and 6 deletions

View File

@ -51,7 +51,6 @@ export default createComponent({
data() { data() {
return { return {
focused: false, focused: false,
validateError: false,
validateMessage: '', validateMessage: '',
}; };
}, },
@ -59,6 +58,10 @@ export default createComponent({
watch: { watch: {
value() { value() {
this.$nextTick(this.adjustSize); this.$nextTick(this.adjustSize);
if (this.validateMessage) {
this.validateMessage = '';
}
}, },
}, },
@ -133,7 +136,6 @@ export default createComponent({
return !this.rules.some(rule => { return !this.rules.some(rule => {
if (rule.required && !this.value) { if (rule.required && !this.value) {
this.validateError = true;
this.validateMessage = rule.message; this.validateMessage = rule.message;
return true; return true;
} }
@ -395,7 +397,7 @@ export default createComponent({
titleClass={[bem('label', labelAlign), this.labelClass]} titleClass={[bem('label', labelAlign), this.labelClass]}
arrowDirection={this.arrowDirection} arrowDirection={this.arrowDirection}
class={bem({ class={bem({
error: this.error || this.validateError, error: this.error || this.validateMessage,
[`label-${labelAlign}`]: labelAlign, [`label-${labelAlign}`]: labelAlign,
'min-height': this.type === 'textarea' && !this.autosize, 'min-height': this.type === 'textarea' && !this.autosize,
})} })}

View File

@ -42,7 +42,9 @@ export default {
}, },
methods: { methods: {
onSubmit() {}, onSubmit(values) {
console.log(values);
},
}, },
}; };
</script> </script>

View File

@ -17,12 +17,28 @@ export default createComponent({
}, },
methods: { methods: {
// @exposed-api
validate() {
return this.fields.map(item => item.validate()).every(item => item);
},
getFormData() {
return this.fields.reduce((form, field) => {
form[field.name] = field.value;
return form;
}, {});
},
onSubmit(event) { onSubmit(event) {
event.preventDefault(); event.preventDefault();
const results = this.fields.map(item => item.validate()); const valid = this.validate();
console.log(results); if (valid) {
this.$emit('submit', this.getFormData());
} else {
this.$emit('failed');
}
}, },
}, },