diff --git a/src/components/input/Select.tsx b/src/components/input/Select.tsx index b4bf498ec..7c74840f4 100644 --- a/src/components/input/Select.tsx +++ b/src/components/input/Select.tsx @@ -65,6 +65,7 @@ function SelectOption({ disabled = false, classes, }: SelectOptionProps) { + const checkboxRef = useRef(null); const selectContext = useContext(SelectContext); if (!selectContext) { throw new Error( @@ -77,13 +78,20 @@ function SelectOption({ !disabled && ((multiple && currentValue.includes(value)) || currentValue === value); - const selectOrToggle = useCallback(() => { - // In single-select, just set current value + const selectOneValue = useCallback(() => { if (!multiple) { selectValue(value); return; } + selectValue(multiple ? [value] : []); + }, [multiple, selectValue, value]); + const toggleValue = useCallback(() => { + // This will never be invoked in single-select + if (!multiple) { + return; + } + // In multi-select, clear selection for nullish values if (!value) { selectValue([]); @@ -112,15 +120,17 @@ function SelectOption({ }, classes, )} - onClick={() => { - if (!disabled) { - selectOrToggle(); + onClick={e => { + // Do not invoke callback if clicked element is the checkbox, as it has + // its own event handler. + if (!disabled && e.target !== checkboxRef.current) { + selectOneValue(); } }} onKeyPress={e => { if (!disabled && ['Enter', 'Space'].includes(e.code)) { e.preventDefault(); - selectOrToggle(); + selectOneValue(); } }} role="option" @@ -154,11 +164,13 @@ function SelectOption({