react-select icon indicating copy to clipboard operation
react-select copied to clipboard

Make sure the selected option is immediately visible when the menu is open

Open ArthurOllier opened this issue 7 years ago • 9 comments

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 : image

we want : image

ArthurOllier avatar May 02 '19 09:05 ArthurOllier

The same problem with version 2.4.3

MaxGorshkov avatar May 08 '19 07:05 MaxGorshkov

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.

themre avatar Jun 04 '19 19:06 themre

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 :)

themre avatar Jun 04 '19 19:06 themre

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)}
            />
        );
    }
}

MaxGorshkov avatar Jun 24 '19 08:06 MaxGorshkov

thanks for this temporary solution! i'll try to find the solution, just didn't have the time to really dig in :(

themre avatar Jun 26 '19 08:06 themre

#3648 will be solved, when the PR (https://github.com/JedWatson/react-select/pull/3652) will pass

ArthurOllier avatar Jun 28 '19 12:06 ArthurOllier

Same issue as https://github.com/JedWatson/react-select/issues/3656

csantos1113 avatar Jun 28 '19 14:06 csantos1113

Thanks @ankush29 - will be fixed on PR https://github.com/JedWatson/react-select/pull/3652

csantos1113 avatar Jun 28 '19 14:06 csantos1113

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!

bladey avatar May 28 '20 03:05 bladey