Update Select to work on an array of values; so long as values are contained in options array

This commit is contained in:
dannyrb authored and James A. Petts committed 2020-05-15 14:02:00 +02:00
1 parent 92923789e5
commit 17e2680da8
2 files changed
+23 -16

No files matched your search

@@ -30,13 +30,13 @@ const InputMultiSelect = ({
isSearchable={false} isSearchable={false}
closeMenuOnSelect={false} closeMenuOnSelect={false}
hideSelectedOptions={false} hideSelectedOptions={false}
onChange={(inputvalues, { action }) => { onChange={(selectedOptions, action) => {
switch (action) { switch (action) {
case 'select-option': case 'select-option':
case 'remove-value': case 'remove-value':
case 'deselect-option': case 'deselect-option':
case 'clear': case 'clear':
onChange(inputvalues); onChange(selectedOptions);
break; break;
default: default:
break; break;
+21 -14
View File
@@ -46,6 +46,17 @@ const Select = ({
value, value,
}) => { }) => {
const _components = isMulti ? { Option, MultiValue } : {}; const _components = isMulti ? { Option, MultiValue } : {};
const selectedOptions = [];
// Map array of values to an array of selected options
if (value && Array.isArray(value)) {
value.forEach(val => {
const found = options.find(opt => opt.value === val);
if (found) {
selectedOptions.push(JSON.parse(JSON.stringify(found)));
}
});
}
return ( return (
<ReactSelect <ReactSelect
@@ -63,8 +74,14 @@ const Select = ({
components={_components} components={_components}
placeholder={placeholder} placeholder={placeholder}
options={options} options={options}
value={value} value={selectedOptions}
onChange={onChange} onChange={(selectedOptions, { action }) => {
const newSelection = selectedOptions.reduce(
(acc, curr) => acc.concat([curr.value]),
[]
);
onChange(newSelection, action);
}}
></ReactSelect> ></ReactSelect>
); );
}; };
@@ -77,6 +94,7 @@ Select.defaultProps = {
isDisabled: false, isDisabled: false,
isMulti: false, isMulti: false,
isSearchable: true, isSearchable: true,
value: [],
}; };
Select.propTypes = { Select.propTypes = {
@@ -95,18 +113,7 @@ Select.propTypes = {
}) })
), ),
placeholder: PropTypes.string, placeholder: PropTypes.string,
value: PropTypes.oneOfType([ value: PropTypes.arrayOf(PropTypes.string),
PropTypes.arrayOf(
PropTypes.shape({
value: PropTypes.string,
label: PropTypes.string,
})
),
PropTypes.shape({
value: PropTypes.string,
label: PropTypes.string,
}),
]),
}; };
export default Select; export default Select;