Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to enable multi-selection in SelectWithoutFormik #5018

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => ({
sergei-maertens marked this conversation as resolved.
Show resolved Hide resolved
...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,
},
};
Loading