From 5083686164558f4a6be4b905131b3debefc49541 Mon Sep 17 00:00:00 2001 From: Adarsh Pastakia Date: Sun, 27 Oct 2024 12:08:01 +0400 Subject: [PATCH] fix(List): scroll selected into view --- packages/form/src/select/List.tsx | 41 +++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/packages/form/src/select/List.tsx b/packages/form/src/select/List.tsx index 5fd8f72..d35e966 100644 --- a/packages/form/src/select/List.tsx +++ b/packages/form/src/select/List.tsx @@ -32,7 +32,7 @@ import { import { Chip } from "@react-fabric/core"; import { isArray, isEmpty } from "@react-fabric/utilities"; import classNames from "classnames"; -import { Fragment, useCallback, useId, useMemo } from "react"; +import { Fragment, useCallback, useId, useLayoutEffect, useMemo } from "react"; import { FieldWrapper } from "../internal/FieldWrapper"; import { InputWrapper } from "../internal/InputWrapper"; import { type SelectProps } from "../types"; @@ -55,6 +55,7 @@ export const List = ({ valueProperty, multiple, allowCreate, + searchable, createOption, matcher, renderer, @@ -166,9 +167,12 @@ export const List = ({ } } else if (evt.key === "Backspace") { !state.query && handleRemove(); - } else if (["ArrowUp", "ArrowDown"].includes(evt.key)) { - if (state.activeIndex === null) { - setActiveIndex(state.selectedIndex); + } else if (!searchable || ["ArrowUp", "ArrowDown"].includes(evt.key)) { + if ( + ["ArrowUp", "ArrowDown"].includes(evt.key) && + state.activeIndex === null + ) { + setActiveIndex(state.selectedIndex ?? 0); } setTimeout(() => { props.onKeyDown?.(evt); @@ -225,6 +229,15 @@ export const List = ({ } }, [state.value, state.query, placeholder, renderer]); + useLayoutEffect(() => { + setTimeout(() => { + state.selectedIndex && + listRef.current[state.selectedIndex]?.scrollIntoView({ + block: "center", + }); + }, 100); + }, [state.selectedIndex]); + return ( ({ wrapperRef={refs.setReference} >
+ e.currentTarget.querySelector("input")?.focus() + } > {!hideSelected && displayValue} handleQuery(evt.target.value)} />
@@ -277,10 +302,12 @@ export const List = ({ {...floatingProps} items={state.items} active={state.activeIndex} + valueProperty={valueProperty} + labelProperty={labelProperty} itemRef={setItemRef} itemProps={makeItemProps} > - {(item) => renderer?.(item) ?? item[labelProperty] ?? item} + {(item, label) => renderer?.(item) ?? label}