CustomCheckGroupField.js
10.5 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import React, { PropTypes } from 'react';
import { Form, Input, InputNumber, Tooltip, Icon, Cascader, Radio, Select, Row, Col, Checkbox, Button, TimePicker, DatePicker, notification } from 'antd';
const CheckboxGroup = Checkbox.Group;
const RadioGroup = Radio.Group;
const { MonthPicker } = DatePicker;
import FetchUserPick from '../FetchUserPick';
import { Field, reduxForm } from 'redux-form';
import moment from 'moment';
import { required, maxLength, number, email, mobile, startCharacter } from './validate';
import UploadWrap from '../UploadWrap';
import { OSSURL } from '../../../redux/constants/Constants';
const FormItem = Form.Item;
const Option = Select.Option;
const defaultOptions = [];
import { safetyOption } from '../../socialinsurances/socialHelpUtils';
import cx from 'classnames';
const diffChoiceInsurance = (preInfos, newInfos) => {
const tempMap = {}, resuls = [];
preInfos.map((preItem, i) => {
tempMap[preItem.value] = true;
});
newInfos.map((newItem, i) => {
if (!tempMap[newItem.value])
resuls.push(newItem);
});
return resuls;
}
const mergeChoiceInsurance = (preInfos, newInfos) => {//按照险种名合并数组
const tempMap = {};
newInfos.map((newItem, i) => {
tempMap[newItem.value] = newItem;
});
preInfos.map((preItem, i) => {
if (tempMap[preItem.value])
tempMap[preItem.value] = preItem;
else {
tempMap[preItem.value] = preItem;
}
});
return Object.values(tempMap);
}
class CustomCheckGroupField extends React.Component {
constructor(props) {
super(props);
this.addSafety = this.addSafety.bind(this);
this.validateStatus = this.validateStatus.bind(this);
this.showErrMessage = this.showErrMessage.bind(this);
this.renderField = this.renderField.bind(this);
this.validateRequired = this.validateRequired.bind(this);
this.state = {
needReload: false,
value: null,
inputVlaue: '',
addOptions: [],
checkValues: []
}
}
static propTypes = {
formItemLayout: PropTypes.object,
name: PropTypes.string,
type: PropTypes.string,
format: PropTypes.string,
formatDate: PropTypes.string,
formatMonth: PropTypes.string,
placeholder: PropTypes.string,
label: PropTypes.string,
validate: PropTypes.array,
onKeyUp: PropTypes.func,
size: PropTypes.string,
inputStyle: PropTypes.object,
options: PropTypes.array,
rows: PropTypes.number,
id: PropTypes.string,
defaultValue: PropTypes.any,
optionKey: PropTypes.string,
optionValue: PropTypes.string,
multiple: PropTypes.bool,
defaultRadio: PropTypes.any,
showSearch: PropTypes.bool,
showInput: PropTypes.bool,
is_required: PropTypes.bool
}
static defaultProps = {
formItemLayout: {
labelCol: { span: 6 },
wrapperCol: { span: 14 },
},
placeholder: "",
type: "text",
validate: [],
size: "default",
rows: 6,
id: '',
is_required: false,
disabled: false,
inputStyle: { width: '100%' },
onKeyUp: () => { },
onSelect: (value, option) => { },
options: defaultOptions,
defaultValue: '',
optionKey: 'id',
optionValue: 'name',
multiple: false,
showSearch: false,
showInput: true,
defaultRadio: '',
optionFilterProp: null,
checkValues: [],
customOption: safetyOption
}
validateStatus(field) {
if (field && field.meta && field.meta.touched && field.meta.error) {
return 'error'
} else {
return null;
}
}
showErrMessage(field) {
if (field && field.meta && field.meta.touched && field.meta.error) {
return field.meta.error
} else {
return '';
}
}
validateRequired(validate) {
let isRequireFlag = false;
validate.map((val) => {
if (val.toString().indexOf('此项是必填项') != -1) {
isRequireFlag = true
}
});
return isRequireFlag;
}
addSafety(e) {//添加险种
let { addOptions, inputVlaue } = this.state;
const { extOption, options = [] } = this.props;
if (!inputVlaue || !inputVlaue.trim()) {
return;
}
if (inputVlaue.trim().length > 10) {
notification.error({
message: '提示',
description: '最多只能录入10个字符哦'
});
return;
}
let isPush = false;
options.map((opt, i) => {
if (opt.value == inputVlaue) {
notification.error({
message: '提示',
description: inputVlaue + ',此险种名称已存在哦'
});
isPush = true;
}
});
addOptions.map((opt, i) => {
if (opt.value == inputVlaue) {
notification.error({
message: '提示',
description: inputVlaue + ',此险种名称已存在哦'
});
isPush = true;
}
});
if (!isPush) {
addOptions.push({ label: inputVlaue, value: inputVlaue, ...extOption });
this.setState({
addOptions,
inputVlaue: ''
});
}
}
removeItem(option, i, field) {
let { addOptions, checkValues } = this.state;
const { options, customOption } = this.props;
const deleteOpt = addOptions.splice(i, 1);
const newCheckValues = [];
if (checkValues.length == 0) {
checkValues = [].concat(field.input.value);
}
checkValues.map((val, i) => {
if (deleteOpt[0].value != val) {
newCheckValues.push(val)
}
})
this.setState({ addOptions, checkValues: newCheckValues });
if (newCheckValues && newCheckValues.length > 0) {
if (this.props.onChange) {
const tempOptions = [], newOptions = [];
options.concat(addOptions).map((op, i) => {
newCheckValues.map((val, j) => {
if (op.label == val) {
tempOptions.push(op);
}
})
if (op.label != option.label) {
newOptions.push(op);
}
});
this.props.onChange(newCheckValues, field, tempOptions, newOptions);
} else {
if (newCheckValues.length == 0) {
field.input.onChange(null);
} else
field.input.onChange(newCheckValues);
}
}
}
componentDidMount() {
const { options, needReload, customOption } = this.props;
const { addOptions } = this.state;
this.setState({
needReload: !needReload,
addOptions: mergeChoiceInsurance(diffChoiceInsurance(customOption, options), addOptions)
});
}
componentWillReceiveProps(nextProps) {
const { options, needReload, customOption } = nextProps;
if (JSON.stringify(customOption) != JSON.stringify(options)) {
const { addOptions } = this.state;
this.setState({
needReload: !needReload,
addOptions: mergeChoiceInsurance(diffChoiceInsurance(customOption, options), addOptions)
})
}
}
renderField(field) {
let { formItemLayout, label, format, rows, id, formatDate, formatMonth, placeholder, name, type, onKeyUp, inputStyle, size, options, multiple, disabled,
defaultValue, optionKey, optionValue, defaultRadio, showSearch, optionFilterProp, has, uploadType, validate, disabledDate, formFiled, disabledHours,
disabledMinutes, is_required, customOption
} = this.props;
const { file, value, addOptions } = this.state;
if (type && type == 'checkboxGroup') {
return (
<FormItem className='formItems'
{...formItemLayout} label={label}
required={formFiled ? formFiled.is_required : this.validateRequired(validate)}
validateStatus={this.validateStatus(field)}
help={this.showErrMessage(field)} >
<CheckboxGroup style={{ width: '500px' }} value={field.input.value ? field.input.value : null} defaultValue={defaultValue}
onChange={
(value) => {
const tempOptions = [];
options.concat(addOptions).map((option, i) => {
value.map((val, j) => {
if (option.label == val) {
tempOptions.push(option);
}
})
});
if (this.props.onChange) {
this.props.onChange(value, field, tempOptions);
} else {
if (value.length == 0) {
field.input.onChange(null);
} else
field.input.onChange(value);
}
this.setState({
checkValues: value.length == 0 ? [] : value
})
}
}>
<Row>
{customOption.map((option, i) => {//option.disabled
return (
<Col span={5} key={i}>
<div>
<Checkbox value={option.value} disabled={false} >{option.label}</Checkbox>
</div>
</Col>
)
})}
{addOptions.map((option, i) => {//option.disabled
return (
<Col span={5} key={i} >
<div>
<Checkbox value={option.value} disabled={false}>{option.label}</Checkbox>
<Icon type="close" style={{ cursor: 'pointer', color: 'red', marginLeft: '-8px' }} onClick={this.removeItem.bind(this, option, i, field)} />
</div>
</Col>
)
})}
</Row>
</CheckboxGroup>
</FormItem>
)
}
}
render() {
const { formItemLayout, label, name, type, validate, options, formFiled, showInput, addInsStyle } = this.props;
const tempValidate = [required], otherValidate = [];
return (
<div className={cx('custom_check_box')}>
<Row>
<Col span={24}>
<Field name={name} label={label} type={type} component={(field) => { return this.renderField(field) }} validate={formFiled ? formFiled.is_required ? tempValidate : otherValidate : validate} />
</Col>
{
showInput &&
<Col span={24} style={{ marginTop: '-16px' }}>
<div style={addInsStyle ? addInsStyle : { width: '130px', margin: '0 0 32px 200px' }}>
<Input size="default" placeholder="添加险种" value={this.state.inputVlaue} onPressEnter={this.addSafety}
onChange={(e) => {
this.setState({ inputVlaue: e.target.value });
}}
addonAfter={<span style={{ cursor: 'pointer' }} onClick={this.addSafety}>确认</span>}
/>
</div>
</Col>
}
</Row>
</div>
);
}
}
export default CustomCheckGroupField;