Upgrade rc form (#4430)

* support nested form item name by upgrade rc-form

react-component/form#48

* Fix dynamic form demo
This commit is contained in:
偏右 2016-12-31 23:47:07 +08:00 committed by ddcat1115
parent 2390c055aa
commit e4bee3425a
5 changed files with 37 additions and 37 deletions

View File

@ -376,34 +376,6 @@ exports[`test renders ./components/form/demo/customized-form-controls.md correct
exports[`test renders ./components/form/demo/dynamic-form-item.md correctly 1`] = ` exports[`test renders ./components/form/demo/dynamic-form-item.md correctly 1`] = `
<form <form
class="ant-form ant-form-horizontal"> class="ant-form ant-form-horizontal">
<div
class="ant-row ant-form-item">
<div
class="ant-col-4 ant-form-item-label">
<label
class=""
for="names-0">
Passengers
</label>
</div>
<div
class="ant-col-20">
<div
class="ant-form-item-control ">
<input
class="ant-input ant-input-lg"
data-__meta="[object Object]"
id="names-0"
placeholder="passenger name"
style="width:60%;margin-right:8px;"
type="text"
value="" />
<i
class="anticon anticon-minus-circle-o dynamic-delete-button"
disabled="" />
</div>
</div>
</div>
<div <div
class="ant-row ant-form-item"> class="ant-row ant-form-item">
<div <div
@ -417,7 +389,23 @@ exports[`test renders ./components/form/demo/dynamic-form-item.md correctly 1`]
<i <i
class="anticon anticon-plus" /> class="anticon anticon-plus" />
<span> <span>
Add Add field
</span>
</button>
</div>
</div>
</div>
<div
class="ant-row ant-form-item">
<div
class="ant-col-20 ant-col-offset-4">
<div
class="ant-form-item-control ">
<button
class="ant-btn ant-btn-primary ant-btn-lg"
type="submit">
<span>
Submit
</span> </span>
</button> </button>
</div> </div>

View File

@ -53,6 +53,15 @@ class DynamicFieldSet extends React.Component {
}); });
} }
handleSubmit = (e) => {
e.preventDefault();
this.props.form.validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
}
});
}
render() { render() {
const { getFieldDecorator, getFieldValue } = this.props.form; const { getFieldDecorator, getFieldValue } = this.props.form;
const formItemLayout = { const formItemLayout = {
@ -62,7 +71,7 @@ class DynamicFieldSet extends React.Component {
const formItemLayoutWithOutLabel = { const formItemLayoutWithOutLabel = {
wrapperCol: { span: 20, offset: 4 }, wrapperCol: { span: 20, offset: 4 },
}; };
getFieldDecorator('keys', { initialValue: [] });
const keys = getFieldValue('keys'); const keys = getFieldValue('keys');
const formItems = keys.map((k, index) => { const formItems = keys.map((k, index) => {
return ( return (
@ -92,13 +101,16 @@ class DynamicFieldSet extends React.Component {
); );
}); });
return ( return (
<Form horizontal> <Form onSubmit={this.handleSubmit} horizontal>
{formItems} {formItems}
<FormItem {...formItemLayoutWithOutLabel}> <FormItem {...formItemLayoutWithOutLabel}>
<Button type="dashed" onClick={this.add} style={{ width: '60%' }}> <Button type="dashed" onClick={this.add} style={{ width: '60%' }}>
<Icon type="plus" /> Add <Icon type="plus" /> Add field
</Button> </Button>
</FormItem> </FormItem>
<FormItem {...formItemLayoutWithOutLabel}>
<Button type="primary" htmlType="submit" size="large">Submit</Button>
</FormItem>
</Form> </Form>
); );
} }

View File

@ -86,11 +86,11 @@ After wrapped by `getFieldDecorator`, `value`(or other property defined by `valu
If you use `react@<15.3.0`, then, you can't use `getFieldDecorator` in stateless component: https://github.com/facebook/react/pull/6534 If you use `react@<15.3.0`, then, you can't use `getFieldDecorator` in stateless component: https://github.com/facebook/react/pull/6534
#### getFieldDecorator's parameters #### getFieldDecorator(id, options) parameters
| Property | Description | Type | Default Value | | Property | Description | Type | Default Value |
|-----------|-----------------------------------------|------|---------------| |-----------|-----------------------------------------|------|---------------|
| options.id | The unique identifier is required. | string | | | id | The unique identifier is required. support [nested fields format](https://github.com/react-component/form/pull/48). | string | |
| options.valuePropName | Props of children node, for example, the prop of Switch is 'checked'. | string | 'value' | | options.valuePropName | Props of children node, for example, the prop of Switch is 'checked'. | string | 'value' |
| options.initialValue | You can specify initial value, type, optional value of children node. | | n/a | | options.initialValue | You can specify initial value, type, optional value of children node. | | n/a |
| options.trigger | When to collect the value of children node | string | 'onChange' | | options.trigger | When to collect the value of children node | string | 'onChange' |

View File

@ -87,11 +87,11 @@ CustomizedForm = Form.create({})(CustomizedForm);
如果使用的是 `react@<15.3.0`,则 `getFieldDecorator` 调用不能位于纯函数组件中: https://github.com/facebook/react/pull/6534 如果使用的是 `react@<15.3.0`,则 `getFieldDecorator` 调用不能位于纯函数组件中: https://github.com/facebook/react/pull/6534
#### getFieldDecorator 参数 #### getFieldDecorator(id, options) 参数
| 参数 | 说明 | 类型 | 默认值 | | 参数 | 说明 | 类型 | 默认值 |
|-----------|-----------------------------------------|-----|--------| |-----------|-----------------------------------------|-----|--------|
| id | 必填输入控件唯一标志 | string | | | id | 必填输入控件唯一标志。支持嵌套式的[写法](https://github.com/react-component/form/pull/48)。 | string | |
| options.valuePropName | 子节点的值的属性,如 Switch 的是 'checked' | string | 'value' | | options.valuePropName | 子节点的值的属性,如 Switch 的是 'checked' | string | 'value' |
| options.initialValue | 子节点的初始值,类型、可选值均由子节点决定 | | | | options.initialValue | 子节点的初始值,类型、可选值均由子节点决定 | | |
| options.trigger | 收集子节点的值的时机 | string | 'onChange' | | options.trigger | 收集子节点的值的时机 | string | 'onChange' |

View File

@ -48,7 +48,7 @@
"rc-dialog": "~6.5.0", "rc-dialog": "~6.5.0",
"rc-dropdown": "~1.4.8", "rc-dropdown": "~1.4.8",
"rc-editor-mention": "~0.3.0", "rc-editor-mention": "~0.3.0",
"rc-form": "~1.0.0", "rc-form": "~1.1.0",
"rc-input-number": "~2.8.3", "rc-input-number": "~2.8.3",
"rc-menu": "~5.0.1", "rc-menu": "~5.0.1",
"rc-notification": "~1.3.4", "rc-notification": "~1.3.4",