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 15, 2025
1 parent f16d700 commit 6ff9c0e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
17 changes: 10 additions & 7 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,12 +22,11 @@ const styles = {
}),
indicatorsContainer: baseStyles => ({
...baseStyles,
height: 'calc(1.875rem - 2px)',
padding: '0 2px',
padding: '0px 2px',
}),
dropdownIndicator: (...args) => ({
...initialStyles.dropdownIndicator(...args),
padding: '5px 2px',
padding: '4px 2px',
}),
};

Expand Down Expand Up @@ -90,9 +87,15 @@ const SelectWithoutFormik = ({name, options, value, className, onChange, ...prop
menuPlacement="auto"
menuPortalTarget={parentSelector()}
options={options}
value={getValue(options, value)}
value={props.isMulti ? value.map(v => getValue(options, v)) : getValue(options, value)}
onChange={selectedOption => {
onChange(selectedOption === null ? undefined : selectedOption.value);
let transformedValue;
if (props.isMulti) {
transformedValue = selectedOption.map(option => option.value);
} else {
transformedValue = selectedOption === null ? undefined : selectedOption.value;
}
onChange(transformedValue);
}}
{...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.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
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 6ff9c0e

Please sign in to comment.