index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import React, { Component } from 'react';
  2. import { Form, Button, Switch, Select, DatePicker, Input, InputNumber, Modal, Radio, Row, Col } from 'antd';
  3. import FileUpload from '../../components/FileUpload';
  4. const { TextArea } = Input;
  5. class FormLayout extends Component {
  6. constructor(props) {
  7. super(props);
  8. if (props.onEvent) {
  9. props.onEvent('confirm', this.onConfirm.bind(this));
  10. props.onEvent('cancel', this.onCancel.bind(this));
  11. }
  12. }
  13. onConfirm() {
  14. this.props.form.validateFields((err, fieldsValue) => {
  15. if (err) {
  16. return;
  17. }
  18. if (this.props.onConfirm) this.props.onConfirm(fieldsValue);
  19. });
  20. }
  21. onCancel() {
  22. if (this.props.onCancel) this.props.onCancel();
  23. }
  24. getItem(item, option) {
  25. switch (item.type) {
  26. case 'radio':
  27. return (
  28. <Radio.Group className={item.class}>
  29. {item.select.map((i, index) => {
  30. if (typeof i === 'string') {
  31. return (
  32. <Radio key={index} value={i}>
  33. {i}
  34. </Radio>
  35. );
  36. }
  37. return (
  38. <Radio key={index} value={i.value}>
  39. {i.label}
  40. </Radio>
  41. );
  42. })}
  43. </Radio.Group>
  44. );
  45. case 'select':
  46. return (
  47. <Select className={item.class} placeholder={item.placeholder}>
  48. {item.select.map((i, index) => {
  49. if (typeof item === 'string') {
  50. return (
  51. <Select.Option key={index} value={i}>
  52. {i}
  53. </Select.Option>
  54. );
  55. }
  56. return (
  57. <Select.Option key={index} value={i.value}>
  58. {i.label}
  59. </Select.Option>
  60. );
  61. })}
  62. </Select>
  63. );
  64. case 'render':
  65. return item.render(option);
  66. case 'textarea':
  67. return <TextArea autosize className={item.class} placeholder={item.placeholder} disabled={!!item.disabled} />;
  68. case 'input':
  69. return (
  70. <Input
  71. className={item.class}
  72. placeholder={item.placeholder}
  73. readOnly={!!item.readOnly}
  74. disabled={!!item.disabled}
  75. />
  76. );
  77. case 'number':
  78. return (
  79. <InputNumber
  80. className={item.class}
  81. placeholder={item.placeholder}
  82. readOnly={!!item.readOnly}
  83. disabled={!!item.disabled}
  84. />
  85. );
  86. case 'switch':
  87. return <Switch className={item.class} />;
  88. case 'date':
  89. return <DatePicker className={item.class} placeholder={item.placeholder} />;
  90. case 'hidden':
  91. return <Input type="hidden" />;
  92. case 'image':
  93. case 'file':
  94. return (
  95. <FileUpload
  96. {...item}
  97. value={this.props.form.getFieldValue(item.key)}
  98. onChange={file => this.props.form.setFieldsValue({ [item.key]: file })}
  99. onEnd={url => {
  100. this.props.form.setFieldsValue({ [item.key]: url });
  101. }}
  102. onError={err => {
  103. this.props.form.setFields({ [item.key]: { value: '', errors: [err] } });
  104. }}
  105. />
  106. );
  107. default:
  108. return <div />;
  109. }
  110. }
  111. getForm(modal) {
  112. const { itemList = [], hideAction = false, confirmText = '确定', cancelText = '取消', data = {} } = this.props;
  113. const { getFieldDecorator } = this.props.form;
  114. const hideSubmit = modal || (!modal && hideAction);
  115. return (
  116. <Form>
  117. <Row>
  118. {itemList.map((item, index) => {
  119. const option = item.option ? Object.assign({}, item.option) : {};
  120. if (data && data[item.key]) {
  121. option.initialValue = data[item.key];
  122. }
  123. if (item.type === 'hidden') {
  124. return <div>{getFieldDecorator(item.key, option)(this.getItem(item, option))}</div>;
  125. }
  126. return (
  127. <Col xs={12} sm={12} md={8} lg={8} xl={6}>
  128. <Form.Item
  129. labelCol={{ span: 6 }}
  130. wrapperCol={{ span: modal ? 16 : 16 }}
  131. key={index}
  132. label={`${item.name}:`}
  133. >
  134. {getFieldDecorator(item.key, option)(this.getItem(item, option))}
  135. </Form.Item>
  136. </Col>
  137. );
  138. })}
  139. </Row>
  140. {!hideSubmit && (
  141. <Form.Item wrapperCol={{ offset: 6 }}>
  142. <Button type="primary" onClick={() => this.onConfirm()} style={{ marginRight: 20 }}>
  143. {confirmText}
  144. </Button>
  145. <Button onClick={() => this.onCancel()}>{cancelText}</Button>
  146. </Form.Item>
  147. )}
  148. </Form>
  149. );
  150. }
  151. render() {
  152. const { modal = false, title, confirmText = '确定', cancelText = '取消' } = this.props;
  153. return modal ? (
  154. <Modal
  155. title={title}
  156. visible
  157. className="field-layout"
  158. okText={confirmText}
  159. cancelText={cancelText}
  160. onOk={() => this.onConfirm()}
  161. onCancel={() => this.onCancel()}
  162. >
  163. {this.getForm(modal)}
  164. </Modal>
  165. ) : (
  166. <div className="field-layout">{this.getForm()}</div>
  167. );
  168. }
  169. }
  170. export default Form.create()(FormLayout);