Field

根据 props 中的 type 属性渲染不同的表单基础组件,props 中的其他属性为要渲染的组件的属性

import * as React from "react";
import PropTypes from "prop-types";
import {
  Input,
  InputNumber,
  Select,
  Radio,
  Checkbox,
  Switch,
  Rate,
  Slider
} from "antd";
import XUpload from "../XUpload";

const defaultSelectProps = {
  allowClear: true,
  showSearch: true,
  placeholder: "请选择",
  optionFilterProp: "children",
  filterOption: (input, option) =>
    option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
};

class Field extends React.Component {
  static propTypes = {};

  static defaultProps = {};

  // Input
  renderInput = props => {
    return <Input {...props} />;
  };

  // InputNumber
  renderInputNumber = props => {
    return <InputNumber {...props} />;
  };

  // Textarea
  renderTextArea = props => {
    return <Input.TextArea {...props} />;
  };

  // Select
  renderSelect = props => {
    const { options = [], ...restSelectProps } = props;
    return (
      <Select {...defaultSelectProps} {...restSelectProps}>
        {options.map(item => (
          <Select.Option key={item.value} value={item.value}>
            {item.label}
          </Select.Option>
        ))}
      </Select>
    );
  };

  // Radio
  renderRadio = props => {
    return <Radio.Group {...props}></Radio.Group>;
  };

  // Checkbox
  renderCheckbox = props => {
    return <Checkbox {...props} />;
  };

  // CheckBox Group
  renderCheckboxGroup = props => {
    return <Checkbox.Group {...props} />;
  };

  // Password
  renderPassword = props => {
    return <Input.Password {...props} />;
  };

  // Switch
  renderSwitch = props => {
    return <Switch {...props}></Switch>;
  };

  // Rate
  renderRate = props => {
    return <Rate {...props}></Rate>;
  };

  // Slider
  renderSlider = props => {
    return <Slider {...props}></Slider>;
  };

  // Upload
  renderUpload = props => {
    return <XUpload {...props}></XUpload>;
  };

  // 自定义表单项
  renderCustom = props => {
    const { node } = props;
    return <React.Fragment>{node}</React.Fragment>;
  };

  // 渲染表单项
  renderField = fieldProps => {
    const { type, ...props } = fieldProps;
    switch (type) {
      case "input":
        return this.renderInput(props);
      case "inputNumber":
        return this.renderInputNumber(props);
      case "textarea":
        return this.renderTextArea(props);
      case "select":
        return this.renderSelect(props);
      case "radio":
        return this.renderRadio(props);
      case "checkbox":
        return this.renderCheckbox(props);
      case "checkboxGroup":
        return this.renderCheckboxGroup(props);
      case "password":
        return this.renderPassword(props);
      case "switch":
        return this.renderSwitch(props);
      case "rate":
        return this.renderRate(props);
      case "slider":
        return this.renderSlider(props);
      case "upload":
        return this.renderUpload(props);
      case "custom":
        return this.renderCustom(props);
      default:
        return null;
    }
  };

  render() {
    return this.renderField(this.props);
  }
}

export default Field;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

思考

  • 在组件中同时引入了多个组件,如果在项目中大量应用会不会产生性能问题
  • 可不可以用react-loadable优化
上次更新: 11/5/2019, 1:01:36 AM