/* eslint react/no-multi-comp:0, no-console:0 */ import { createForm } from '../index'; import BaseMixin from '../../_util/BaseMixin'; const Form1 = { mixins: [BaseMixin], props: { form: Object, }, data() { return { useInput: true, }; }, methods: { onSubmit(e) { e.preventDefault(); this.form.validateFields((error, values) => { if (!error) { console.log('ok', values); } else { console.log('error', error, values); } }); }, changeUseInput(e) { this.setState({ useInput: e.target.checked, }); }, }, render() { const { getFieldError, getFieldDecorator } = this.form; return (

situation 1

{this.useInput ? getFieldDecorator('name', { initialValue: '', rules: [ { required: true, message: "What's your name 1?", }, ], })() : null} text content {this.useInput ? null : getFieldDecorator('name', { initialValue: '', rules: [ { required: true, message: "What's your name 2?", }, ], })()}
{(getFieldError('name') || []).join(', ')}
); }, }; const Form2 = { mixins: [BaseMixin], props: { form: Object, }, data() { return { useInput: true, }; }, beforeMount() { const { getFieldDecorator } = this.form; this.nameDecorator = getFieldDecorator('name', { initialValue: '', rules: [ { required: true, message: "What's your name?", }, ], }); }, methods: { onSubmit(e) { e.preventDefault(); this.form.validateFields((error, values) => { if (!error) { console.log('ok', values); } else { console.log('error', error, values); } }); }, changeUseInput(e) { this.setState({ useInput: e.target.checked, }); }, }, render() { const { getFieldError } = this.form; return (

situation 2

{this.useInput ? this.nameDecorator() : null} text content {this.useInput ? null : this.nameDecorator()}
{(getFieldError('name') || []).join(', ')}
); }, }; const Form3 = { mixins: [BaseMixin], props: { form: Object, }, data() { return { useInput: false, }; }, methods: { onSubmit(e) { e.preventDefault(); this.form.validateFields((error, values) => { if (!error) { console.log('ok', values); } else { console.log('error', error, values); } }); }, changeUseInput(e) { this.setState({ useInput: e.target.checked, }); }, }, render() { const { getFieldError, getFieldDecorator } = this.form; return (

situation 3

{getFieldDecorator('name', { initialValue: '', rules: [ { required: true, message: "What's your name 1?", }, ], })()} {this.useInput ? null : getFieldDecorator('name2', { initialValue: '', rules: [ { required: true, message: "What's your name 2?", }, ], })()}
{(getFieldError('name') || []).join(', ')}
); }, }; const Form4 = { mixins: [BaseMixin], props: { form: Object, }, data() { return { useInput: true, }; }, methods: { onSubmit(e) { e.preventDefault(); this.form.validateFields((error, values) => { if (!error) { console.log('ok', values); } else { console.log('error', error, values); } }); }, changeUseInput(e) { this.setState({ useInput: e.target.checked, }); }, }, render() { const { getFieldError, getFieldDecorator } = this.form; return (

situation 4

{this.useInput ? getFieldDecorator('name', { initialValue: '', trigger: 'input', rules: [ { required: true, message: "What's your name 1?", }, ], })() : getFieldDecorator('name2', { initialValue: '', trigger: 'input', rules: [ { required: true, message: "What's your name 2?", }, ], })()}
{(getFieldError('name') || []).join(', ')} {(getFieldError('name2') || []).join(', ')}
); }, }; const WrappedForm1 = createForm()(Form1); const WrappedForm2 = createForm()(Form2); const WrappedForm3 = createForm()(Form3); const WrappedForm4 = createForm()(Form4); export default { render() { return (
); }, };