Material UI 自动完成 + 无限滚动在一起?

2023-12-29

问题:获取双滚动条 - 删除纸张滚动条会使自动完成内容不可滚动,因此仅显示下拉列表可见高度中的内容。 如果我隐藏另一个滚动,则不会调用无限滚动 API。我怎样才能让它工作:

描述 -

我正在尝试使用 Material UI Autocomplete 创建一个无限滚动,我正在使用react-infinite-scroll-component附上链接供参考 https://www.npmjs.com/package/react-infinite-scroll-component

我的实现方式是:

因为我们需要将无限滚动附加到呈现列表项的 Popper;因此我编写了自定义 PAPER 组件(根据文档,它负责渲染下拉列表中的项目) PaperComponent={myCustomComponent}

我的 InfiniteScrollAutoComplete 定义附在下面:

<Autocomplete
      options={list.data && list.data !== null ? list.data : []}
      getOptionLabel={(option) => option.name}
      PaperComponent={(param) => (
        <InfiniteScroll
          height={200}
          dataLength={list.total}
          next={this.handleFetchNext.bind(this)}
          hasMore={list.data.length < list.total ? true : false}
          loader={
            <p style={{ textAlign: "center", backgroundColor: "#f9dc01" }}>
              <b>Loading...</b>
            </p>
          }
          endMessage={
            <p style={{ textAlign: "center", backgroundColor: "#f9dc01" }}>
              <b>Yay! You have seen it all</b>
            </p>
          }
        >
          <Paper {...param}  />
        </InfiniteScroll>
      )}
      renderInput={(params) => (
        <TextField {...params} label="" variant="outlined" />
      )}
    />

    const observer = useRef();
     
    const lastOptionElementRef = useCallback((node) => {
        if (observer.current) observer.current.disconnect();
            observer.current = new IntersectionObserver(async (entries) => {
                if (entries[0].isIntersecting && props.hasMore) {
                    setPageNumber((pageNumber) => pageNumber + 1);
                }
            });
        if (node) observer.current.observe(node);
    }, [props.loader]);

您可以使用 render option 属性将此 lastOptionElementRef 添加到选项的最后一个元素。每当最后一个选项在视口中可见时,这将触发一个函数。此外,它还避免了滚动问题

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Material UI 自动完成 + 无限滚动在一起? 的相关文章

随机推荐