mirror of https://github.com/grafana/grafana.git
Combobox: Combine loading and search/caret icons (#95191)
Don't have both loading and search/caret icon at the same time
This commit is contained in:
parent
32d224223e
commit
2a5afd0d1a
|
|
@ -156,6 +156,7 @@ export const Combobox = <T extends string | number>({
|
|||
onChange(selectedItem);
|
||||
},
|
||||
defaultHighlightedIndex: selectedItemIndex ?? 0,
|
||||
|
||||
scrollIntoView: () => {},
|
||||
onInputValueChange: ({ inputValue }) => {
|
||||
const customValueOption =
|
||||
|
|
@ -223,8 +224,19 @@ export const Combobox = <T extends string | number>({
|
|||
setInputValue(selectedItem?.label ?? value?.toString() ?? '');
|
||||
}, [selectedItem, setInputValue, value]);
|
||||
|
||||
const handleSuffixClick = useCallback(() => {
|
||||
isOpen ? closeMenu() : openMenu();
|
||||
}, [isOpen, openMenu, closeMenu]);
|
||||
|
||||
const InputComponent = width === 'auto' ? AutoSizeInput : Input;
|
||||
|
||||
const suffixIcon = asyncLoading
|
||||
? 'spinner'
|
||||
: // If it's loading, show loading icon. Otherwise, icon indicating menu state
|
||||
isOpen
|
||||
? 'search'
|
||||
: 'angle-down';
|
||||
|
||||
return (
|
||||
<div>
|
||||
<InputComponent
|
||||
|
|
@ -248,19 +260,14 @@ export const Combobox = <T extends string | number>({
|
|||
}}
|
||||
/>
|
||||
)}
|
||||
<Icon
|
||||
name={isOpen ? 'search' : 'angle-down'}
|
||||
onClick={() => {
|
||||
if (isOpen) {
|
||||
closeMenu();
|
||||
} else {
|
||||
openMenu();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
|
||||
{/* When you click the input, it should just focus the text box. However, clicks on input suffix arent
|
||||
translated to the input, so it blocks the input from being focused. So we need an additional event
|
||||
handler here to open/close the menu. It should not have button role because we intentionally don't
|
||||
want it in the a11y tree. */}
|
||||
<Icon name={suffixIcon} onClick={handleSuffixClick} />
|
||||
</>
|
||||
}
|
||||
loading={asyncLoading}
|
||||
{...restProps}
|
||||
{...getInputProps({
|
||||
ref: inputRef,
|
||||
|
|
|
|||
Loading…
Reference in New Issue