ant-design-vue/components/vc-form/demo/dynamic-fields.js

288 lines
6.4 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';
import BaseMixin from '../../_util/BaseMixin';
2018-05-04 04:16:17 +00:00
const Form1 = {
mixins: [BaseMixin],
props: {
form: Object,
},
2019-01-12 03:33:27 +00:00
data() {
2018-05-04 04:16:17 +00:00
return {
useInput: true,
2019-01-12 03:33:27 +00:00
};
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
changeUseInput(e) {
2018-05-04 04:16:17 +00:00
this.setState({
useInput: e.target.checked,
2019-01-12 03:33:27 +00:00
});
2018-05-04 04:16:17 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { getFieldError, getFieldDecorator } = this.form;
2018-05-04 04:16:17 +00:00
return (
<form onSubmit={this.onSubmit}>
<h2>situation 1</h2>
2019-01-12 03:33:27 +00:00
{this.useInput
? getFieldDecorator('name', {
initialValue: '',
rules: [
{
required: true,
message: "What's your name 1?",
},
],
})(<input />)
: null}
2018-05-04 04:16:17 +00:00
<span>text content</span>
2019-01-12 03:33:27 +00:00
{this.useInput
? null
: getFieldDecorator('name', {
initialValue: '',
rules: [
{
required: true,
message: "What's your name 2?",
},
],
})(<input />)}
2018-05-04 04:16:17 +00:00
<div>
<label>
2019-01-12 03:33:27 +00:00
<input type="checkbox" checked={this.useInput} onInput={this.changeUseInput} />
2018-05-04 04:16:17 +00:00
change input
</label>
{(getFieldError('name') || []).join(', ')}
</div>
<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
const Form2 = {
mixins: [BaseMixin],
props: {
form: Object,
},
2019-01-12 03:33:27 +00:00
data() {
2018-05-04 04:16:17 +00:00
return {
useInput: true,
2019-01-12 03:33:27 +00:00
};
2018-05-04 04:16:17 +00:00
},
2019-01-12 03:33:27 +00:00
beforeMount() {
const { getFieldDecorator } = this.form;
2018-05-04 04:16:17 +00:00
this.nameDecorator = 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
changeUseInput(e) {
2018-05-04 04:16:17 +00:00
this.setState({
useInput: e.target.checked,
2019-01-12 03:33:27 +00:00
});
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}>
<h2>situation 2</h2>
{this.useInput ? this.nameDecorator(<input />) : null}
<span>text content</span>
{this.useInput ? null : this.nameDecorator(<input />)}
<div>
<label>
2019-01-12 03:33:27 +00:00
<input type="checkbox" checked={this.useInput} onInput={this.changeUseInput} />
2018-05-04 04:16:17 +00:00
change input
</label>
{(getFieldError('name') || []).join(', ')}
</div>
<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
const Form3 = {
mixins: [BaseMixin],
props: {
form: Object,
},
2019-01-12 03:33:27 +00:00
data() {
2018-05-04 04:16:17 +00:00
return {
useInput: false,
2019-01-12 03:33:27 +00:00
};
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
changeUseInput(e) {
2018-05-04 04:16:17 +00:00
this.setState({
useInput: e.target.checked,
2019-01-12 03:33:27 +00:00
});
2018-05-04 04:16:17 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { getFieldError, getFieldDecorator } = this.form;
2018-05-04 04:16:17 +00:00
return (
<form onSubmit={this.onSubmit}>
<h2>situation 3</h2>
{getFieldDecorator('name', {
initialValue: '',
2019-01-12 03:33:27 +00:00
rules: [
{
required: true,
message: "What's your name 1?",
},
],
2018-05-04 04:16:17 +00:00
})(<input />)}
2019-01-12 03:33:27 +00:00
{this.useInput
? null
: getFieldDecorator('name2', {
initialValue: '',
rules: [
{
required: true,
message: "What's your name 2?",
},
],
})(<input />)}
2018-05-04 04:16:17 +00:00
<div>
<label>
2019-01-12 03:33:27 +00:00
<input type="checkbox" checked={this.useInput} onInput={this.changeUseInput} />
2018-05-04 04:16:17 +00:00
Hide second input
</label>
{(getFieldError('name') || []).join(', ')}
</div>
<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-14 14:42:04 +00:00
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 (
<form onSubmit={this.onSubmit}>
<h2>situation 4</h2>
{this.useInput
? getFieldDecorator('name', {
2019-01-15 01:41:07 +00:00
initialValue: '',
trigger: 'input',
rules: [
{
required: true,
message: "What's your name 1?",
},
],
})(<input />)
2019-01-14 14:42:04 +00:00
: getFieldDecorator('name2', {
initialValue: '',
trigger: 'input',
rules: [
{
required: true,
message: "What's your name 2?",
},
],
})(<input />)}
<div>
<label>
<input type="checkbox" checked={this.useInput} onInput={this.changeUseInput} />
toggle decorator name
</label>
{(getFieldError('name') || []).join(', ')}
{(getFieldError('name2') || []).join(', ')}
</div>
<button>Submit</button>
</form>
);
},
};
2019-01-12 03:33:27 +00:00
const WrappedForm1 = createForm()(Form1);
const WrappedForm2 = createForm()(Form2);
const WrappedForm3 = createForm()(Form3);
2019-01-14 14:42:04 +00:00
const WrappedForm4 = createForm()(Form4);
2018-05-04 04:16:17 +00:00
export default {
2019-01-12 03:33:27 +00:00
render() {
2018-05-04 04:16:17 +00:00
return (
<div>
<WrappedForm1 />
<WrappedForm2 />
<WrappedForm3 />
2019-01-14 14:42:04 +00:00
<WrappedForm4 />
2018-05-04 04:16:17 +00:00
</div>
2019-01-12 03:33:27 +00:00
);
2018-05-04 04:16:17 +00:00
},
2019-01-12 03:33:27 +00:00
};