UNPKG

cjd-parkball

Version:

> 中后台业务组件库,中后台就像公园,进入需要买门票(登录),所以以 Parkball(公园球) 命名,公园内必定捕获!作为一个组件库,提供使用方法文档,方便开发者的调用

90 lines (75 loc) 2.33 kB
--- category: 2 title: 表单数据存储于上层组件 title_en: Store Form Data into Upper Component --- zh-CN 通过使用 `onFieldsChange` 与 `mapPropsToFields`,可以把表单的数据存储到上层组件或者 [Redux](https://github.com/reactjs/redux)、[dva](https://github.com/dvajs/dva) 中,更多可参考 [rc-form 示例](http://react-component.github.io/form/examples/redux.html)。 **注意:**`mapPropsToFields` 里面返回的表单域数据必须使用 `Form.createFormField` 包装。 en-US We can store form data into upper component or [Redux](https://github.com/reactjs/redux) or [dva](https://github.com/dvajs/dva) by using `onFieldsChange` and `mapPropsToFields`, see more at this [rc-form demo](http://react-component.github.io/form/examples/redux.html). **Note:** You must wrap field data with `Form.createFormField` in `mapPropsToFields`. ````jsx import { Form, Input } from 'parkball'; const FormItem = Form.Item; const CustomizedForm = Form.create({ onFieldsChange(props, changedFields) { props.onChange(changedFields); }, mapPropsToFields(props) { return { username: Form.createFormField({ ...props.username, value: props.username.value, }), }; }, onValuesChange(_, values) { console.log(values); }, })((props) => { const { getFieldDecorator } = props.form; return ( <Form layout="inline"> <FormItem label="Username"> {getFieldDecorator('username', { rules: [{ required: true, message: 'Username is required!' }], })(<Input />)} </FormItem> </Form> ); }); class Demo extends React.Component { state = { fields: { username: { value: 'benjycui', }, }, }; handleFormChange = (changedFields) => { this.setState(({ fields }) => ({ fields: { ...fields, ...changedFields }, })); } render() { const fields = this.state.fields; return ( <div> <CustomizedForm {...fields} onChange={this.handleFormChange} /> <pre className="language-bash"> {JSON.stringify(fields, null, 2)} </pre> </div> ); } } ReactDOM.render(<Demo />, mountNode); ```` <style> #components-form-demo-global-state .language-bash { max-width: 400px; border-radius: 6px; margin-top: 24px; } </style>