Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrolling of the selected menu option into view for menuportals #5980

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
33 changes: 8 additions & 25 deletions packages/react-select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,6 @@ export default class Select<
initialTouchX = 0;
initialTouchY = 0;
openAfterFocus = false;
scrollToFocusedOptionOnUpdate = false;
userIsDragging?: boolean;
isAppleDevice = isAppleDevice();

Expand All @@ -669,10 +668,12 @@ export default class Select<
focusedOptionRef: HTMLDivElement | null = null;
getFocusedOptionRef: RefCallback<HTMLDivElement> = (ref) => {
this.focusedOptionRef = ref;
this.tryScrollIntoView();
};
menuListRef: HTMLDivElement | null = null;
getMenuListRef: RefCallback<HTMLDivElement> = (ref) => {
this.menuListRef = ref;
this.tryScrollIntoView();
};
inputRef: HTMLInputElement | null = null;
getInputRef: RefCallback<HTMLInputElement> = (ref) => {
Expand Down Expand Up @@ -805,16 +806,6 @@ export default class Select<
if (this.props.autoFocus) {
this.focusInput();
}

// Scroll focusedOption into view if menuIsOpen is set on mount (e.g. defaultMenuIsOpen)
if (
this.props.menuIsOpen &&
this.state.focusedOption &&
this.menuListRef &&
this.focusedOptionRef
) {
scrollIntoView(this.menuListRef, this.focusedOptionRef);
}
}
componentDidUpdate(prevProps: Props<Option, IsMulti, Group>) {
const { isDisabled, menuIsOpen } = this.props;
Expand Down Expand Up @@ -843,16 +834,6 @@ export default class Select<
// eslint-disable-next-line react/no-did-update-set-state
this.setState({ isFocused: true });
}

// scroll the focused option into view if necessary
if (
this.menuListRef &&
this.focusedOptionRef &&
this.scrollToFocusedOptionOnUpdate
) {
scrollIntoView(this.menuListRef, this.focusedOptionRef);
this.scrollToFocusedOptionOnUpdate = false;
}
}
componentWillUnmount() {
this.stopListeningComposition();
Expand All @@ -864,6 +845,11 @@ export default class Select<
// Consumer Handlers
// ==============================

tryScrollIntoView() {
if (this.menuListRef && this.focusedOptionRef) {
scrollIntoView(this.menuListRef, this.focusedOptionRef);
}
}
onMenuOpen() {
this.props.onMenuOpen();
}
Expand Down Expand Up @@ -908,9 +894,6 @@ export default class Select<
}
}

// only scroll if the menu isn't already open
this.scrollToFocusedOptionOnUpdate = !(isFocused && this.menuListRef);

this.setState(
{
inputIsHiddenAfterUpdate: false,
Expand Down Expand Up @@ -990,7 +973,7 @@ export default class Select<
} else if (direction === 'last') {
nextFocus = options.length - 1;
}
this.scrollToFocusedOptionOnUpdate = true;

this.setState({
focusedOption: options[nextFocus],
focusedValue: null,
Expand Down