MCPcopy Create free account
hub / github.com/decaporg/decap-cms / SelectControl

Class SelectControl

packages/decap-cms-widget-select/src/SelectControl.js:40–142  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

38}
39
40export default class SelectControl extends React.Component {
41 static propTypes = {
42 onChange: PropTypes.func.isRequired,
43 value: PropTypes.node,
44 forID: PropTypes.string.isRequired,
45 classNameWrapper: PropTypes.string.isRequired,
46 setActiveStyle: PropTypes.func.isRequired,
47 setInactiveStyle: PropTypes.func.isRequired,
48 field: ImmutablePropTypes.contains({
49 options: ImmutablePropTypes.listOf(
50 PropTypes.oneOfType([
51 PropTypes.string,
52 PropTypes.number,
53 ImmutablePropTypes.contains({
54 label: PropTypes.string.isRequired,
55 value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
56 }),
57 ]),
58 ).isRequired,
59 }),
60 };
61
62 isValid = () => {
63 const { field, value, t } = this.props;
64 const min = field.get('min');
65 const max = field.get('max');
66
67 if (!field.get('multiple')) {
68 return { error: false };
69 }
70
71 const error = validations.validateMinMax(
72 t,
73 field.get('label', field.get('name')),
74 value,
75 min,
76 max,
77 );
78
79 return error ? { error } : { error: false };
80 };
81
82 handleChange = selectedOption => {
83 const { onChange, field } = this.props;
84 const isMultiple = field.get('multiple', false);
85 const isEmpty = isMultiple ? !selectedOption?.length : !selectedOption;
86
87 if (field.get('required') && isEmpty && isMultiple) {
88 onChange(List());
89 } else if (isEmpty) {
90 onChange(null);
91 } else if (isMultiple) {
92 const options = selectedOption.map(optionToString);
93 onChange(fromJS(options));
94 } else {
95 onChange(optionToString(selectedOption));
96 }
97 };

Callers

nothing calls this directly

Calls 5

onChangeFunction · 0.85
fromJSFunction · 0.85
optionToStringFunction · 0.85
getMethod · 0.80
mapMethod · 0.80

Tested by

no test coverage detected