Skip to content

Commit

Permalink
Merge pull request #5018 from open-formulieren/chore/4908-enable-mult…
Browse files Browse the repository at this point in the history
…i-for-variable-selection-component

Add possibility to enable multi-selection in SelectWithoutFormik
  • Loading branch information
viktorvanwijk authored Jan 15, 2025
2 parents e28b4c0 + 350799f commit 7023c49
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
21 changes: 14 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,15 @@ 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',
}),
clearIndicator: (...args) => ({
...initialStyles.clearIndicator(...args),
padding: '4px 2px',
}),
};

Expand Down Expand Up @@ -90,9 +91,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,21 @@ import {VARIABLE_SOURCES} from '../form_design/variables/constants';
import {ReactSelectContext} from './ReactSelect';
import VariableSelection from './VariableSelection';

const render = ({name, includeStaticVariables, filter, menuIsOpen = false}) => {
// workaround for https://github.com/JedWatson/react-select/issues/3708
const resetParentSelector = Story => (
<ReactSelectContext.Provider value={{parentSelector: () => undefined}}>
<Story />
</ReactSelectContext.Provider>
);

const render = ({
name,
includeStaticVariables,
filter,
menuIsOpen = false,
isMulti = false,
isClearable = false,
}) => {
const [{value}, updateArgs] = useArgs();
return (
<VariableSelection
Expand All @@ -16,6 +30,8 @@ const render = ({name, includeStaticVariables, filter, menuIsOpen = false}) => {
onChange={event => updateArgs({value: event.target.value})}
filter={filter}
menuIsOpen={menuIsOpen}
isMulti={isMulti}
isClearable={isClearable}
/>
);
};
Expand Down Expand Up @@ -118,15 +134,23 @@ export default {
export const Default = {};

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

export const multiSelection = {
decorators: [resetParentSelector],
args: {
value: ['key2', 'key5'],
menuIsOpen: true,
isMulti: true,
},
};

export const Clearable = {
args: {
isClearable: true,
},
};

0 comments on commit 7023c49

Please sign in to comment.