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

This commit is contained in:
dannyrb 2020-05-12 01:04:20 -04:00 committed by James A. Petts
parent 92923789e5
commit 17e2680da8
2 changed files with 23 additions and 16 deletions

View File

@ -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;

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;