Skip to content

Commit

Permalink
✨ [#4908] Add possibility to enable multi in SelectWithoutFormik comp…
Browse files Browse the repository at this point in the history
…onent.

Part of #4908, where multi-selection of the variables is needed.
  • Loading branch information
viktorvanwijk committed Jan 14, 2025
1 parent f16d700 commit 364b6e0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/openforms/js/components/admin/forms/ReactSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ const styles = {
control: (...args) => ({
...initialStyles.control(...args),
minHeight: '1.875rem',
height: '1.875rem',
}),
valueContainer: (...args) => ({
...initialStyles.valueContainer(...args),
height: 'calc(1.875rem - 2px)',
padding: '0 6px',
}),
input: (...args) => ({
Expand All @@ -24,7 +22,6 @@ const styles = {
}),
indicatorsContainer: baseStyles => ({
...baseStyles,
height: 'calc(1.875rem - 2px)',
padding: '0 2px',
}),
dropdownIndicator: (...args) => ({
Expand Down Expand Up @@ -67,6 +64,16 @@ const getValue = (options, value) => {
return null;
};

/**
* Convenience method around 'getValue', which allows the `values` argument to be an array of
* values (used for multi-select components).
*/
const getValueStringOrArray = (options, value) => {
return Object.prototype.toString.call(value) !== '[object Array]'
? getValue(options, value)
: value.map(v => getValue(options, v));
};

export const ReactSelectContext = createContext({parentSelector: () => document.body});
ReactSelectContext.displayName = 'ReactSelectContext';

Expand All @@ -90,9 +97,15 @@ const SelectWithoutFormik = ({name, options, value, className, onChange, ...prop
menuPlacement="auto"
menuPortalTarget={parentSelector()}
options={options}
value={getValue(options, value)}
value={getValueStringOrArray(options, value)}
onChange={selectedOption => {
onChange(selectedOption === null ? undefined : selectedOption.value);
onChange(
Object.prototype.toString.call(selectedOption) !== '[object Array]'
? selectedOption === null
? undefined
: selectedOption.value
: selectedOption.map(option => (option === null ? undefined : option.value))
);
}}
{...props}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const VariableSelection = ({
VariableSelection.propTypes = {
id: PropTypes.string,
name: PropTypes.string,
value: PropTypes.string,
value: PropTypes.string | PropTypes.array,
onChange: PropTypes.func.isRequired,
includeStaticVariables: PropTypes.bool,
filter: PropTypes.func,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {VARIABLE_SOURCES} from '../form_design/variables/constants';
import {ReactSelectContext} from './ReactSelect';
import VariableSelection from './VariableSelection';

const render = ({name, includeStaticVariables, filter, menuIsOpen = false}) => {
const render = ({name, includeStaticVariables, filter, menuIsOpen = false, isMulti = false}) => {
const [{value}, updateArgs] = useArgs();
return (
<VariableSelection
Expand All @@ -16,6 +16,7 @@ const render = ({name, includeStaticVariables, filter, menuIsOpen = false}) => {
onChange={event => updateArgs({value: event.target.value})}
filter={filter}
menuIsOpen={menuIsOpen}
isMulti={isMulti}
/>
);
};
Expand Down Expand Up @@ -130,3 +131,19 @@ export const menuOpen = {
menuIsOpen: true,
},
};

export const multiSelection = {
decorators: [
// workaround for https://github.com/JedWatson/react-select/issues/3708
Story => (
<ReactSelectContext.Provider value={{parentSelector: () => undefined}}>
<Story />
</ReactSelectContext.Provider>
),
],
args: {
value: ['key2', 'key5'],
menuIsOpen: true,
isMulti: true,
},
};

0 comments on commit 364b6e0

Please sign in to comment.