最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react - select causing page to scroll down - Stack Overflow

programmeradmin3浏览0评论

I am using react-select in my project.

I have a numerous selects and I need the selects to be open so I am using the prop menuIsOpen={true} but for some reason this prop causing the page to scroll down to almost the middle of the page.

when I am setting menuIsOpen={false} the page is not scrolled down but it does not solve the problem because I must have the selects open

is anyone familiar with this problem?

      <Select
        styles={filter.name !== "More" ? basicStyles : moreStyles}
        isMulti={filter.name !== "colorType" ? true : false}
        options={options}
        hideSelectedOptions={false}
        closeMenuOnSelect={false}
        placeholder=""
        value={selectedValues ? selectedValues : []}
        isClearable={false}
        isSearchable={false}
        onChange={addSelectFilter}
        components={{ MultiValueLabel: customMultiValueLabel }}
        blurInputOnSelect={false}
        classNamePrefix={filter.name === "More" ? "more" : "basic-drop"}
        className={filter.name === "More" ? "more-select-container" : undefined}
        menuIsOpen={
          filter.name === "More" ? undefined : menuIsOpen ? true : undefined
        }
      />

I am using react-select in my project.

I have a numerous selects and I need the selects to be open so I am using the prop menuIsOpen={true} but for some reason this prop causing the page to scroll down to almost the middle of the page.

when I am setting menuIsOpen={false} the page is not scrolled down but it does not solve the problem because I must have the selects open

is anyone familiar with this problem?

      <Select
        styles={filter.name !== "More" ? basicStyles : moreStyles}
        isMulti={filter.name !== "colorType" ? true : false}
        options={options}
        hideSelectedOptions={false}
        closeMenuOnSelect={false}
        placeholder=""
        value={selectedValues ? selectedValues : []}
        isClearable={false}
        isSearchable={false}
        onChange={addSelectFilter}
        components={{ MultiValueLabel: customMultiValueLabel }}
        blurInputOnSelect={false}
        classNamePrefix={filter.name === "More" ? "more" : "basic-drop"}
        className={filter.name === "More" ? "more-select-container" : undefined}
        menuIsOpen={
          filter.name === "More" ? undefined : menuIsOpen ? true : undefined
        }
      />

Share Improve this question asked May 25, 2020 at 9:48 HarelHarel 5811 gold badge9 silver badges23 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 10

This problem is caused by the default value of the menuPosition prop being absolute. On a long dropdown list, the dropdown area overflows the screen and makes the browser scroll down.

Add menuPosition="fixed" as a prop to the Select component and it should work. Then you also do not need to set the z-index manually.

I had the same issue, and it was quite annoying ! I resolved it by setting some custom style and adding the props for the portal like:

    const customSelectProps = {
        menuPortalTarget: document.getElementById('root'),
        customStyles: {
            menuPortal: base => {
                const { zIndex, ...rest } = base;
                return { ...rest, zIndex: 9999 };
            },
        },
    };

Adding menuShouldScrollIntoView={false} to Select component solved that problem for me.

Adding menuPlacement= 'auto' or top (anything beside "bottom") fixed this issue for me.

发布评论

评论列表(0)

  1. 暂无评论