From 17e2680da8b03a2eb6a5117a443165170776f33f Mon Sep 17 00:00:00 2001 From: dannyrb Date: Tue, 12 May 2020 01:04:20 -0400 Subject: [PATCH] Update Select to work on an array of values; so long as values are contained in options array --- .../InputMultiSelect/InputMultiSelect.jsx | 4 +-- platform/ui/src/components/Select/Select.jsx | 35 +++++++++++-------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/platform/ui/src/components/InputMultiSelect/InputMultiSelect.jsx b/platform/ui/src/components/InputMultiSelect/InputMultiSelect.jsx index 976eac8c9..c813a18fb 100644 --- a/platform/ui/src/components/InputMultiSelect/InputMultiSelect.jsx +++ b/platform/ui/src/components/InputMultiSelect/InputMultiSelect.jsx @@ -30,13 +30,13 @@ const InputMultiSelect = ({ isSearchable={false} closeMenuOnSelect={false} hideSelectedOptions={false} - onChange={(inputvalues, { action }) => { + onChange={(selectedOptions, action) => { switch (action) { case 'select-option': case 'remove-value': case 'deselect-option': case 'clear': - onChange(inputvalues); + onChange(selectedOptions); break; default: break; diff --git a/platform/ui/src/components/Select/Select.jsx b/platform/ui/src/components/Select/Select.jsx index c8c84638a..3a47d8a6a 100644 --- a/platform/ui/src/components/Select/Select.jsx +++ b/platform/ui/src/components/Select/Select.jsx @@ -46,6 +46,17 @@ const Select = ({ value, }) => { 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 ( { + const newSelection = selectedOptions.reduce( + (acc, curr) => acc.concat([curr.value]), + [] + ); + onChange(newSelection, action); + }} > ); }; @@ -77,6 +94,7 @@ Select.defaultProps = { isDisabled: false, isMulti: false, isSearchable: true, + value: [], }; Select.propTypes = { @@ -95,18 +113,7 @@ Select.propTypes = { }) ), placeholder: PropTypes.string, - value: PropTypes.oneOfType([ - PropTypes.arrayOf( - PropTypes.shape({ - value: PropTypes.string, - label: PropTypes.string, - }) - ), - PropTypes.shape({ - value: PropTypes.string, - label: PropTypes.string, - }), - ]), + value: PropTypes.arrayOf(PropTypes.string), }; export default Select;