mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
53 lines
908 B
JavaScript
53 lines
908 B
JavaScript
// Utils
|
|
import { createNamespace } from '../utils';
|
|
|
|
const [createComponent, bem] = createNamespace('form');
|
|
|
|
export default createComponent({
|
|
provide() {
|
|
return {
|
|
vanForm: this,
|
|
};
|
|
},
|
|
|
|
data() {
|
|
return {
|
|
fields: [],
|
|
};
|
|
},
|
|
|
|
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.formValue;
|
|
return form;
|
|
}, {});
|
|
},
|
|
|
|
onSubmit(event) {
|
|
event.preventDefault();
|
|
|
|
const valid = this.validate();
|
|
|
|
if (valid) {
|
|
this.$emit('submit', this.getFormData());
|
|
} else {
|
|
this.$emit('failed');
|
|
}
|
|
},
|
|
},
|
|
|
|
render() {
|
|
return (
|
|
<form class={bem()} onSubmit={this.onSubmit}>
|
|
{this.slots()}
|
|
</form>
|
|
);
|
|
},
|
|
});
|