|
1 | 1 | import type { ChangeEvent } from 'react';
|
2 |
| -import { useContext } from 'react'; |
| 2 | +import { useContext, useRef } from 'react'; |
3 | 3 | import cl from 'clsx/lite';
|
4 | 4 | import { ChevronUpIcon, ChevronDownIcon } from '@navikt/aksel-icons';
|
5 | 5 | import { useMergeRefs } from '@floating-ui/react';
|
@@ -32,6 +32,7 @@ const ComboboxInput = ({
|
32 | 32 | }: ComboboxInputProps) => {
|
33 | 33 | const context = useContext(ComboboxContext);
|
34 | 34 | const idDispatch = useComboboxIdDispatch();
|
| 35 | + const clearButtonRef = useRef<HTMLButtonElement>(null); |
35 | 36 |
|
36 | 37 | if (!context) {
|
37 | 38 | throw new Error('ComboboxContext is missing');
|
@@ -89,11 +90,12 @@ const ComboboxInput = ({
|
89 | 90 | 'aria-controls': null,
|
90 | 91 | 'aria-expanded': null,
|
91 | 92 | 'aria-haspopup': null,
|
92 |
| - /* If we click the wrapper, open the list, set index to first option, and focus the input */ |
93 |
| - onClick() { |
| 93 | + /* If we click the wrapper, toggle open, set index to first option, and focus the input */ |
| 94 | + onClick(event: React.MouseEvent<HTMLDivElement>) { |
94 | 95 | if (disabled) return;
|
95 | 96 | if (readOnly) return;
|
96 |
| - setOpen(true); |
| 97 | + if (clearButtonRef.current?.contains(event.target as Node)) return; |
| 98 | + setOpen(!open); |
97 | 99 | setActiveIndex(0);
|
98 | 100 | inputRef.current?.focus();
|
99 | 101 | },
|
@@ -152,7 +154,7 @@ const ComboboxInput = ({
|
152 | 154 | </Paragraph>
|
153 | 155 | </div>
|
154 | 156 | {/* Clear button if we are in multiple mode and have at least one active value */}
|
155 |
| - {showClearButton && <ComboboxClearButton />} |
| 157 | + {showClearButton && <ComboboxClearButton ref={clearButtonRef} />} |
156 | 158 | {/* Arrow for combobox. Click is handled by the wrapper */}
|
157 | 159 | <div className={'ds-combobox__arrow'}>
|
158 | 160 | {open ? (
|
|
0 commit comments