Skip to content

Commit b0031c0

Browse files
authored
New Select: Set highlighted index to selected item on open (grafana#91951)
* Set highlighted index on open * Remove console log * Use defaultHighLightedIndex
1 parent f432a17 commit b0031c0

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

packages/grafana-ui/src/components/Combobox/Combobox.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ function estimateSize() {
4848
export const Combobox = ({ options, onChange, value, ...restProps }: ComboboxProps) => {
4949
const MIN_WIDTH = 400;
5050
const [items, setItems] = useState(options);
51-
const selectedItem = useMemo(() => options.find((option) => option.value === value) || null, [options, value]);
51+
const selectedItemIndex = useMemo(
52+
() => options.findIndex((option) => option.value === value) || null,
53+
[options, value]
54+
);
55+
const selectedItem = selectedItemIndex ? options[selectedItemIndex] : null;
5256

5357
const inputRef = useRef<HTMLInputElement>(null);
5458
const floatingRef = useRef(null);
@@ -66,6 +70,7 @@ export const Combobox = ({ options, onChange, value, ...restProps }: ComboboxPro
6670
items,
6771
itemToString,
6872
selectedItem,
73+
defaultHighlightedIndex: selectedItemIndex ?? undefined,
6974
scrollIntoView: () => {},
7075
onInputValueChange: ({ inputValue }) => {
7176
setItems(options.filter(itemFilter(inputValue)));

0 commit comments

Comments
 (0)