forked from decaporg/decap-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectControl.js
106 lines (94 loc) · 2.97 KB
/
SelectControl.js
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
import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { Map, List, fromJS } from 'immutable';
import { find } from 'lodash';
import Select from 'react-select';
import { reactSelectStyles } from 'netlify-cms-ui-default';
function optionToString(option) {
return option && option.value ? option.value : null;
}
function convertToOption(raw) {
if (typeof raw === 'string') {
return { label: raw, value: raw };
}
return Map.isMap(raw) ? raw.toJS() : raw;
}
function getSelectedValue({ value, options, isMultiple }) {
if (isMultiple) {
const selectedOptions = List.isList(value) ? value.toJS() : value;
if (!selectedOptions || !Array.isArray(selectedOptions)) {
return null;
}
return selectedOptions
.map(i => options.find(o => o.value === (i.value || i)))
.filter(Boolean)
.map(convertToOption);
} else {
return find(options, ['value', value]) || null;
}
}
export default class SelectControl extends React.Component {
static propTypes = {
onChange: PropTypes.func.isRequired,
value: PropTypes.node,
forID: PropTypes.string.isRequired,
classNameWrapper: PropTypes.string.isRequired,
setActiveStyle: PropTypes.func.isRequired,
setInactiveStyle: PropTypes.func.isRequired,
field: ImmutablePropTypes.contains({
options: ImmutablePropTypes.listOf(
PropTypes.oneOfType([
PropTypes.string,
ImmutablePropTypes.contains({
label: PropTypes.string.isRequired,
value: PropTypes.string.isRequired,
}),
]),
).isRequired,
}),
};
handleChange = selectedOption => {
const { onChange, field } = this.props;
const isMultiple = field.get('multiple', false);
if (Array.isArray(selectedOption)) {
if (!isMultiple && selectedOption.length === 0) {
onChange(null);
} else {
onChange(fromJS(selectedOption.map(optionToString)));
}
} else {
onChange(optionToString(selectedOption));
}
};
render() {
const { field, value, forID, classNameWrapper, setActiveStyle, setInactiveStyle } = this.props;
const fieldOptions = field.get('options');
const isMultiple = field.get('multiple', false);
const isClearable = !field.get('required', true) || isMultiple;
if (!fieldOptions) {
return <div>Error rendering select control for {field.get('name')}: No options</div>;
}
const options = [...fieldOptions.map(convertToOption)];
const selectedValue = getSelectedValue({
options,
value,
isMultiple,
});
return (
<Select
inputId={forID}
value={selectedValue}
onChange={this.handleChange}
className={classNameWrapper}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}
options={options}
styles={reactSelectStyles}
isMulti={isMultiple}
isClearable={isClearable}
placeholder=""
/>
);
}
}