|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import ReactDOM from 'react-dom'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | +import classNames from 'classnames'; |
| 5 | +import noop from '@feizheng/noop'; |
| 6 | +import objectAssign from 'object-assign'; |
| 7 | +import { Form, Button } from 'antd'; |
| 8 | + |
| 9 | +const CLASS_NAME = 'react-ant-form'; |
| 10 | +export default Form.create()( |
| 11 | + class ReactAntForm extends Component { |
| 12 | + static displayName = CLASS_NAME; |
| 13 | + static version = '__VERSION__'; |
| 14 | + static propTypes = { |
| 15 | + /** |
| 16 | + * The extended className for component. |
| 17 | + */ |
| 18 | + className: PropTypes.string, |
| 19 | + /** |
| 20 | + * Default fileds value object. |
| 21 | + */ |
| 22 | + fieldsValue: PropTypes.object, |
| 23 | + /** |
| 24 | + * Form schema. |
| 25 | + */ |
| 26 | + items: PropTypes.array, |
| 27 | + /** |
| 28 | + * The onSubmit event. |
| 29 | + */ |
| 30 | + onSubmit: PropTypes.func, |
| 31 | + /** |
| 32 | + * When component did mount. |
| 33 | + */ |
| 34 | + onLoad: PropTypes.func, |
| 35 | + /** |
| 36 | + * The formLayout for antd.Form. |
| 37 | + */ |
| 38 | + formLayout: PropTypes.object, |
| 39 | + /** |
| 40 | + * The submit label. |
| 41 | + */ |
| 42 | + submitLabel: PropTypes.string, |
| 43 | + /** |
| 44 | + * The submit props. |
| 45 | + */ |
| 46 | + submitProps: PropTypes.object |
| 47 | + }; |
| 48 | + |
| 49 | + static defaultProps = { |
| 50 | + fieldsValue: {}, |
| 51 | + items: [], |
| 52 | + onSubmit: noop, |
| 53 | + onLoad: noop, |
| 54 | + formLayout: { |
| 55 | + labelCol: { span: 6 }, |
| 56 | + wrapperCol: { span: 16 } |
| 57 | + }, |
| 58 | + submitLabel: ' ', |
| 59 | + submitProps: { |
| 60 | + type: 'primary', |
| 61 | + htmlType: 'submit', |
| 62 | + children: 'Save' |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + componentDidMount() { |
| 67 | + const { onLoad, fieldsValue, form } = this.props; |
| 68 | + const { getFieldDecorator, setFields } = form; |
| 69 | + objectAssign(this, { $form: form }); |
| 70 | + setFields(fieldsValue); |
| 71 | + onLoad({ |
| 72 | + target: { |
| 73 | + sender: this, |
| 74 | + value: this.props |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + handleSubmit = (inEvent) => { |
| 80 | + inEvent.preventDefault(); |
| 81 | + const { onSubmit, form } = this.props; |
| 82 | + form.validateFields((err, values) => { |
| 83 | + if (!err) { |
| 84 | + onSubmit(values); |
| 85 | + } |
| 86 | + }); |
| 87 | + }; |
| 88 | + |
| 89 | + render() { |
| 90 | + const { className, items, formLayout, submitLabel, submitProps } = this.props; |
| 91 | + const { getFieldDecorator } = this.props.form; |
| 92 | + |
| 93 | + return ( |
| 94 | + <Form |
| 95 | + data-component={CLASS_NAME} |
| 96 | + className={classNames(CLASS_NAME, className)} |
| 97 | + onSubmit={this.handleSubmit}> |
| 98 | + {items.map((item, index) => { |
| 99 | + return ( |
| 100 | + <Form.Item |
| 101 | + className="react-ant-form-field" |
| 102 | + {...formLayout} |
| 103 | + key={index} |
| 104 | + label={item.label}> |
| 105 | + {getFieldDecorator(item.field, { |
| 106 | + rules: item.rules |
| 107 | + })(<item.component {...item.props} />)} |
| 108 | + </Form.Item> |
| 109 | + ); |
| 110 | + })} |
| 111 | + <Form.Item |
| 112 | + {...formLayout} |
| 113 | + className="react-ant-form-submit" |
| 114 | + label={submitLabel} |
| 115 | + colon={false}> |
| 116 | + <Button {...submitProps} /> |
| 117 | + </Form.Item> |
| 118 | + </Form> |
| 119 | + ); |
| 120 | + } |
| 121 | + } |
| 122 | +); |
0 commit comments