ant-design-vue/components/vc-form/demo/nested-field.js

184 lines
4.7 KiB
JavaScript
Raw Normal View History

2018-05-04 08:02:31 +00:00
/* eslint react/no-multi-comp:0, no-console:0 */
2019-01-12 03:33:27 +00:00
import createForm from '../src/createDOMForm';
2018-05-04 08:02:31 +00:00
const Form = {
props: {
form: Object,
},
methods: {
2019-01-12 03:33:27 +00:00
onSubmit(e) {
e.preventDefault();
console.log('Values of member[0].name.firstname and a[0][1].b.c[0]');
console.log(this.form.getFieldsValue(['member[0].name.firstname', 'a[0][1].b.c[0]']));
console.log('Values of all fields');
console.log(this.form.getFieldsValue());
2018-05-04 08:02:31 +00:00
this.form.validateFieldsAndScroll((error, values) => {
if (!error) {
2019-01-12 03:33:27 +00:00
console.log('ok', values);
2018-05-04 08:02:31 +00:00
} else {
2019-01-12 03:33:27 +00:00
console.log('error', error, values);
2018-05-04 08:02:31 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-05-04 08:02:31 +00:00
},
2019-01-12 03:33:27 +00:00
onChange(e) {
console.log(e.target.value);
2018-05-04 08:02:31 +00:00
},
2019-01-12 03:33:27 +00:00
setField() {
2018-05-04 08:02:31 +00:00
this.form.setFieldsValue({
member: [
{
name: {
firstname: 'm1 first',
lastname: 'm1 last',
},
},
{
name: {
firstname: 'm2 first',
lastname: 'm2 last',
},
},
],
a: [
2019-01-12 03:33:27 +00:00
[
undefined,
{
b: {
c: ['Value of a[0][1].b.c[0]'],
},
2018-05-04 08:02:31 +00:00
},
2019-01-12 03:33:27 +00:00
],
2018-05-04 08:02:31 +00:00
],
w: {
x: {
y: {
z: ['Value of w.x.y.z[0]'],
},
},
},
2019-01-12 03:33:27 +00:00
});
2018-05-04 08:02:31 +00:00
},
2019-01-12 03:33:27 +00:00
resetFields() {
console.log('reset');
this.form.resetFields();
2018-05-04 08:02:31 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { getFieldDecorator, getFieldError } = this.form;
2018-05-04 08:02:31 +00:00
return (
<form onSubmit={this.onSubmit}>
<div>Member 0 firstname</div>
{getFieldDecorator('member[0].name.firstname', {
initialValue: '',
2019-01-12 03:33:27 +00:00
rules: [
{
required: true,
message: "What's the member_0 firstname?",
},
],
})(<input onInput={this.onChange} />)}
2018-05-04 08:02:31 +00:00
<div style={{ color: 'red' }}>
{(getFieldError('member[0].name.firstname') || []).join(', ')}
</div>
<div>Member 0 lastname</div>
{getFieldDecorator('member[0].name.lastname', {
initialValue: '',
2019-01-12 03:33:27 +00:00
rules: [
{
required: true,
message: "What's the member_0 lastname?",
},
],
})(<input onInput={this.onChange} />)}
2018-05-04 08:02:31 +00:00
<div style={{ color: 'red' }}>
{(getFieldError('member[0].name.firstname') || []).join(', ')}
</div>
<div>Member 1 firstname</div>
{getFieldDecorator('member[1].name.firstname', {
initialValue: '',
2019-01-12 03:33:27 +00:00
rules: [
{
required: true,
message: "What's the member_1 fistname?",
},
],
})(<input onInput={this.onChange} />)}
2018-05-04 08:02:31 +00:00
<div style={{ color: 'red' }}>
{(getFieldError('member[1].name.firstname') || []).join(', ')}
</div>
<div>Member 1 lastname</div>
{getFieldDecorator('member[1].name.lastname', {
initialValue: '',
2019-01-12 03:33:27 +00:00
rules: [
{
required: true,
message: "What's the member_1 lastname?",
},
],
})(<input onInput={this.onChange} />)}
2018-05-04 08:02:31 +00:00
<div style={{ color: 'red' }}>
{(getFieldError('member[1].name.firstname') || []).join(', ')}
</div>
<div>a[0][1].b.c[0]</div>
{getFieldDecorator('a[0][1].b.c[0]', {
initialValue: '',
2019-01-12 03:33:27 +00:00
rules: [
{
required: true,
message: "What's a[0][1].b.c[0]?",
},
],
})(<input onInput={this.onChange} />)}
<div style={{ color: 'red' }}>{(getFieldError('a[0][1].b.c[0]') || []).join(', ')}</div>
2018-05-04 08:02:31 +00:00
<div>w.x.y.z[0]</div>
{getFieldDecorator('w.x.y.z[0]', {
initialValue: '',
2019-01-12 03:33:27 +00:00
rules: [
{
required: true,
message: "What's w.x.y.z[0]?",
},
],
})(<input onInput={this.onChange} />)}
<div style={{ color: 'red' }}>{(getFieldError('w.x.y.z[0]') || []).join(', ')}</div>
2018-05-04 08:02:31 +00:00
<button onClick={this.setField}>Set field</button>
<button onClick={this.resetFields}>Reset fields</button>
<button>Submit</button>
</form>
2019-01-12 03:33:27 +00:00
);
2018-05-04 08:02:31 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-05-04 08:02:31 +00:00
const NewForm = createForm({
2019-01-12 03:33:27 +00:00
onFieldsChange(_, changedFields, allFields) {
console.log('onFieldsChange: ', changedFields, allFields);
2018-05-04 08:02:31 +00:00
},
2019-01-12 03:33:27 +00:00
onValuesChange(_, changedValues, allValues) {
console.log('onValuesChange: ', changedValues, allValues);
2018-05-04 08:02:31 +00:00
},
2019-01-12 03:33:27 +00:00
})(Form);
2018-05-04 08:02:31 +00:00
export default {
2019-01-12 03:33:27 +00:00
render() {
return (
<div>
<h2>setFieldsValue</h2>
<NewForm />
</div>
);
2018-05-04 08:02:31 +00:00
},
2019-01-12 03:33:27 +00:00
};