ant-design-vue/components/vc-form/demo/getFieldDecorator.js

52 lines
1.0 KiB
JavaScript
Raw Normal View History

2018-05-04 04:16:17 +00:00
/* eslint react/no-multi-comp:0, no-console:0 */
2019-01-12 03:33:27 +00:00
import { createForm } from '../index';
2018-05-04 04:16:17 +00:00
const Form = {
props: {
form: Object,
},
2019-01-12 03:33:27 +00:00
beforeMount() {
2018-05-04 04:16:17 +00:00
this.nameDecorator = this.form.getFieldDecorator('name', {
initialValue: '',
2019-01-12 03:33:27 +00:00
rules: [
{
required: true,
message: "What's your name?",
},
],
});
2018-05-04 04:16:17 +00:00
},
methods: {
2019-01-12 03:33:27 +00:00
onSubmit(e) {
e.preventDefault();
2018-05-04 04:16:17 +00:00
this.form.validateFields((error, values) => {
if (!error) {
2019-01-12 03:33:27 +00:00
console.log('ok', values);
2018-05-04 04:16:17 +00:00
} else {
2019-01-12 03:33:27 +00:00
console.log('error', error, values);
2018-05-04 04:16:17 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-05-04 04:16:17 +00:00
},
2019-01-12 03:33:27 +00:00
onChange(e) {
console.log(e.target.value);
2018-05-04 04:16:17 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { getFieldError } = this.form;
2018-05-04 04:16:17 +00:00
return (
<form onSubmit={this.onSubmit}>
2019-01-12 03:33:27 +00:00
{this.nameDecorator(<input onInput={this.onChange} />)}
<div style={{ color: 'red' }}>{(getFieldError('name') || []).join(', ')}</div>
2018-05-04 04:16:17 +00:00
<button>Submit</button>
</form>
2019-01-12 03:33:27 +00:00
);
2018-05-04 04:16:17 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 04:16:17 +00:00
2019-01-12 03:33:27 +00:00
export default createForm()(Form);