Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/MenuList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function MenuList (props) {

const { classNamePrefix, isMulti } = selectProps || {};
const list = React.useRef<List>(null);
const scrollOffsetRef = React.useRef<number>(0);

React.useEffect(
() => {
Expand Down Expand Up @@ -120,10 +121,16 @@ function MenuList (props) {
React.useEffect(
() => {
/**
* enables scrolling on key down arrow
* For multi-select, preserve the scroll position when options are
* selected/deselected so the menu doesn't jump back to the top.
* For single-select, scroll to the focused item on keyboard navigation.
*/
if (currentIndex >= 0 && list.current !== null) {
list.current.scrollToItem(currentIndex);
if (list.current !== null) {
if (isMulti && scrollOffsetRef.current > 0) {
list.current.scrollTo(scrollOffsetRef.current);
} else if (currentIndex >= 0) {
list.current.scrollToItem(currentIndex);
}
}
},
[currentIndex, children, list]
Expand Down Expand Up @@ -151,6 +158,7 @@ function MenuList (props) {
itemCount={itemCount}
itemData={children}
itemSize={index => measuredHeights[index] || heights[index]}
onScroll={({ scrollOffset }) => { scrollOffsetRef.current = scrollOffset; }}
>
{({ data, index, style}: ListChildProps) => {
return (
Expand Down