New Select: Set highlighted index to selected item on open (#91951)

* Set highlighted index on open

* Remove console log

* Use defaultHighLightedIndex
This commit is contained in:
Tobias Skarhed 2024-08-16 14:47:39 +02:00 committed by GitHub
parent f432a1713b
commit b0031c0781
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -48,7 +48,11 @@ function estimateSize() {
export const Combobox = ({ options, onChange, value, ...restProps }: ComboboxProps) => {
const MIN_WIDTH = 400;
const [items, setItems] = useState(options);
const selectedItem = useMemo(() => options.find((option) => option.value === value) || null, [options, value]);
const selectedItemIndex = useMemo(
() => options.findIndex((option) => option.value === value) || null,
[options, value]
);
const selectedItem = selectedItemIndex ? options[selectedItemIndex] : null;
const inputRef = useRef<HTMLInputElement>(null);
const floatingRef = useRef(null);
@ -66,6 +70,7 @@ export const Combobox = ({ options, onChange, value, ...restProps }: ComboboxPro
items,
itemToString,
selectedItem,
defaultHighlightedIndex: selectedItemIndex ?? undefined,
scrollIntoView: () => {},
onInputValueChange: ({ inputValue }) => {
setItems(options.filter(itemFilter(inputValue)));