Make sure the selected option is immediately visible when the menu is open
Hello we were looking for a feature that seems broken : https://github.com/JedWatson/react-select/pull/724 if not please let me know.
We have a fetch on focus from our backend And we want the same as pull 724: "Make sure the selected option is immediately visible when the menu is open"
actually ("react-select": "^2.1.1") the React-Select doesn't scroll to make the curent choice visible
we have :

we want :

The same problem with version 2.4.3
i did some investigation and it seems that menuListRef isn't being set. perhaps the issue is that onMenuOpen should be called after menuList has been truly attached to the DOM. maybe this is some race condition issue. maybe we should look at it here: https://github.com/JedWatson/react-select/blob/98f469eeb34c689f2697ffabd43c920077130bbc/packages/react-select/src/Select.js#L502
more bizzare things happen in this codesandbox: https://codesandbox.io/s/x95l82m81q
if I look at chrome debugger, sel1.current is null, but codensandbox console shows HTMLDIvElement. but if I console.log sel1.current, both console shows null.
it easier to debug with source :) actually found out that this.focusedOptionRef is not correctly updated. when I select last item, this.focusedOptionRef still points to the first item. in other words, isFocused prop on each option is not correctly recalculated and thus points to first option. I would appreciate some guidance where is this best to fix since there is a lot of logic :)
I solved this problem using handleMenuOpen event and manual scrollIntoView on focused item:
export class View extends React.Component {
focusedRef: any;
Option(props: any) {
return (
<MenuItem
ref={(el: any) => {
if (props.isSelected && this.focusedRef !== el) {
this.focusedRef = el;
}
if (props.innerRef) {
props.innerRef(el);
}
}}
selected={props.isSelected}
component='div'
style={{
fontWeight: props.isSelected ? 500 : 400,
}}
{...props.innerProps}
>
{props.children}
</MenuItem>
);
}
handleMenuOpen() {
const thisObject = this;
setTimeout(() => {
if (thisObject.focusedRef) {
thisObject.focusedRef.scrollIntoView();
}
}, 100);
}
render() {
const components = {
Option: this.Option.bind(this),
...
};
return (
<Select
...
components={components}
onMenuOpen={this.handleMenuOpen.bind(this)}
/>
);
}
}
thanks for this temporary solution! i'll try to find the solution, just didn't have the time to really dig in :(
#3648 will be solved, when the PR (https://github.com/JedWatson/react-select/pull/3652) will pass
Same issue as https://github.com/JedWatson/react-select/issues/3656
Thanks @ankush29 - will be fixed on PR https://github.com/JedWatson/react-select/pull/3652
Hello -
In an effort to sustain the react-select project going forward, we're closing old issues.
We understand this might be inconvenient but in the best interest of supporting the broader community we have to direct our efforts towards the current major version.
If you aren't using the latest version of react-select please consider upgrading to see if it resolves any issues you're having.
However, if you feel this issue is still relevant and you'd like us to review it - please leave a comment and we'll do our best to get back to you!