|
@@ -1,185 +0,0 @@
|
1
|
|
-import React from 'react';
|
2
|
|
-import { Form, Select } from 'antd';
|
3
|
|
-import PropTypes from 'prop-types';
|
4
|
|
-import { hasOwnProperty, isFunction, isBoolean } from './utils';
|
5
|
|
-
|
6
|
|
-const AntFormItem = Form.Item
|
7
|
|
-
|
8
|
|
-const tailFormItemLayout = {
|
9
|
|
- wrapperCol: {
|
10
|
|
- xs: {
|
11
|
|
- span: 24,
|
12
|
|
- offset: 0,
|
13
|
|
- },
|
14
|
|
- sm: {
|
15
|
|
- span: 16,
|
16
|
|
- offset: 8,
|
17
|
|
- },
|
18
|
|
- },
|
19
|
|
-};
|
20
|
|
-
|
21
|
|
-class Item extends React.PureComponent {
|
22
|
|
- state = {
|
23
|
|
- allFieldValues: this.getFormValues(),
|
24
|
|
- }
|
25
|
|
-
|
26
|
|
- componentDidMount() {
|
27
|
|
- if (this.props.formChange) {
|
28
|
|
- this.props.formChange((p, c, values) => {
|
29
|
|
- this.setState({ allFieldValues: values || this.getFormValues()})
|
30
|
|
- })
|
31
|
|
- }
|
32
|
|
- }
|
33
|
|
-
|
34
|
|
- getFormValues() {
|
35
|
|
- if (this.props.form) {
|
36
|
|
- return this.props.form.getFieldsValue() || {}
|
37
|
|
- }
|
38
|
|
-
|
39
|
|
- return {}
|
40
|
|
- }
|
41
|
|
-
|
42
|
|
- getActualValue(fn, defVal) {
|
43
|
|
- const val = isFunction(fn) ? fn(this.state.allFieldValues, this.props) : fn
|
44
|
|
- return val === undefined ? defVal : val
|
45
|
|
- }
|
46
|
|
-
|
47
|
|
- getValueFromEvent = e => {
|
48
|
|
- if (hasOwnProperty(e, 'checked')) {
|
49
|
|
- return e.checked;
|
50
|
|
- } else if (hasOwnProperty(e, 'target')) {
|
51
|
|
- return e.target.value;
|
52
|
|
- } else {
|
53
|
|
- return e;
|
54
|
|
- }
|
55
|
|
- }
|
56
|
|
-
|
57
|
|
- renderSelectOptions (dict) {
|
58
|
|
- return (dict || []).map(x => <Select.Option key={x.value} value={x.value}>{x.label}</Select.Option>)
|
59
|
|
- }
|
60
|
|
-
|
61
|
|
- renderField() {
|
62
|
|
- const {
|
63
|
|
- form,
|
64
|
|
- name,
|
65
|
|
- node,
|
66
|
|
- children,
|
67
|
|
- placeholder,
|
68
|
|
- render,
|
69
|
|
- rules,
|
70
|
|
- value,
|
71
|
|
- action,
|
72
|
|
- props,
|
73
|
|
- options,
|
74
|
|
- } = this.props || {}
|
75
|
|
-
|
76
|
|
- const config = {
|
77
|
|
- rules,
|
78
|
|
- initialValue: value,
|
79
|
|
- getValueFromEvent: this.getValueFromEvent,
|
80
|
|
- }
|
81
|
|
-
|
82
|
|
- const fieldProps = {
|
83
|
|
- placeholder: Array.isArray(placeholder) ? placeholder(this.state.allFieldValues, this.props) : placeholder,
|
84
|
|
- style: { width: '100%' },
|
85
|
|
- ...(props || {})
|
86
|
|
- }
|
87
|
|
-
|
88
|
|
- const Childs = Array.isArray(options) ? this.renderSelectOptions(options) : (isFunction(children) ? children() : (children || null));
|
89
|
|
- const Field = isFunction(render) ? render(this.props) : React.createElement(node, fieldProps, Childs);
|
90
|
|
-
|
91
|
|
- return action ? Field : form.getFieldDecorator(name, config)(Field)
|
92
|
|
- }
|
93
|
|
-
|
94
|
|
- render () {
|
95
|
|
- const {
|
96
|
|
- form,
|
97
|
|
- label,
|
98
|
|
- name,
|
99
|
|
- node,
|
100
|
|
- children,
|
101
|
|
- placeholder,
|
102
|
|
- render,
|
103
|
|
- rules,
|
104
|
|
- value,
|
105
|
|
- action,
|
106
|
|
- props = {},
|
107
|
|
- hidden = false,
|
108
|
|
- reportChange,
|
109
|
|
- listenChange,
|
110
|
|
- options,
|
111
|
|
- ...restProps
|
112
|
|
- } = this.props;
|
113
|
|
-
|
114
|
|
- const initialValue = value === undefined ? {} : { initialValue: value }
|
115
|
|
-
|
116
|
|
- const config = {
|
117
|
|
- rules,
|
118
|
|
- getValueFromEvent: this.getValueFromEvent,
|
119
|
|
- ...initialValue,
|
120
|
|
- }
|
121
|
|
-
|
122
|
|
- // 以下支持可变内容
|
123
|
|
- // 暂时只支持 hidden, disable, label 的可变
|
124
|
|
- const hiddenActual = isFunction(hidden) ? hidden(this.state.allFieldValues, this.props) : (
|
125
|
|
- isBoolean(hidden) ? hidden : false
|
126
|
|
- );
|
127
|
|
- const labelActual = isFunction(label) ? label(this.state.allFieldValues, this.props) : label
|
128
|
|
- const disabledActual = isFunction(props.disabled) ? props.disabled(this.state.allFieldValues, this.props) : (
|
129
|
|
- isBoolean(props.disabled) ? props.disabled : false
|
130
|
|
- )
|
131
|
|
-
|
132
|
|
- // 没有类型与组件, 生成隐藏字段
|
133
|
|
- if (name && hiddenActual === true) {
|
134
|
|
- if (form && form.getFieldDecorator) {
|
135
|
|
- form.getFieldDecorator(name, config);
|
136
|
|
- }
|
137
|
|
- return null;
|
138
|
|
- }
|
139
|
|
-
|
140
|
|
- const Field = this.renderField()
|
141
|
|
-
|
142
|
|
- if (!labelActual && !name && !action) return !hiddenActual ? Field : null;
|
143
|
|
-
|
144
|
|
- const itemProps = action ? { ...restProps, ...tailFormItemLayout, disabled: disabledActual } : { ...restProps, disabled: disabledActual }
|
145
|
|
-
|
146
|
|
- return !hiddenActual ? (
|
147
|
|
- <AntFormItem label={labelActual} {...itemProps}>{Field}</AntFormItem>
|
148
|
|
- ) : null
|
149
|
|
- }
|
150
|
|
-};
|
151
|
|
-
|
152
|
|
-Item.propTypes = {
|
153
|
|
- formChange: PropTypes.func,
|
154
|
|
- form: PropTypes.object,
|
155
|
|
- label: PropTypes.oneOfType([
|
156
|
|
- PropTypes.node,
|
157
|
|
- PropTypes.func,
|
158
|
|
- ]),
|
159
|
|
- placeholder: PropTypes.oneOfType([
|
160
|
|
- PropTypes.string,
|
161
|
|
- PropTypes.func,
|
162
|
|
- ]),
|
163
|
|
- name: PropTypes.string,
|
164
|
|
- node: PropTypes.oneOfType([
|
165
|
|
- PropTypes.elementType,
|
166
|
|
- PropTypes.func,
|
167
|
|
- ]),
|
168
|
|
- children: PropTypes.oneOfType([
|
169
|
|
- PropTypes.element,
|
170
|
|
- PropTypes.func,
|
171
|
|
- PropTypes.arrayOf(PropTypes.element),
|
172
|
|
- ]),
|
173
|
|
- render: PropTypes.func,
|
174
|
|
- rules: PropTypes.array,
|
175
|
|
- value: PropTypes.any,
|
176
|
|
- action: PropTypes.bool,
|
177
|
|
- props: PropTypes.object,
|
178
|
|
- options: PropTypes.array,
|
179
|
|
- hidden: PropTypes.oneOfType([
|
180
|
|
- PropTypes.bool,
|
181
|
|
- PropTypes.func,
|
182
|
|
- ]),
|
183
|
|
-}
|
184
|
|
-
|
185
|
|
-export default Item;
|