简体   繁体   English

在溢出的React-Select Multi-select输入中滚动到底部

[英]Scroll to bottom in overflowing React-Select Multi-select Input

I am using React-Select v2.4.4. 我正在使用React-Select v2.4.4。 I was trying various things for bringing scrollbar at bottom of multi-select input but none of them seems to work. 我正在尝试各种使滚动条位于多选输入底部的方法,但是似乎都没有用。

Sandbox Link: https://codesandbox.io/s/react-codesandboxer-example-89cry 沙盒链接: https//codesandbox.io/s/react-codesandboxer-example-89cry

In above link, when we select Select All option from drop down, scrollbar should automatically move to bottom and show below inputs: 在上面的链接中,当我们从下拉列表中Select All选”选项时,滚动条应自动移至底部并在输入下方显示: 在此处输入图片说明

I have tried 2 things but none of them is working. 我已经尝试了2件事,但是它们都没有起作用。

  1. I tried using Ref and below code: 我尝试使用Ref和以下代码:

     const scrollHeight = this.node.scrollHeight; const height = this.node.clientHeight; const maxScrollTop = scrollHeight - height; this.node.scrollTop = maxScrollTop > 0 ? maxScrollTop : 0; 
  2. Got element by using document.getElementById() , and then used scrollIntoView . 通过使用document.getElementById()获得元素,然后使用scrollIntoView

     const element = document.getElementById("select"); element.scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" }); 

Both of above methods are present in scrollToBottom function. 上述两种方法都存在于scrollToBottom函数中。

Is there something i am missing or doing wrong. 有什么我想念或做错的事吗?

Your first approach can´t work because ref on the Select component is the component itself, not a DOM element. 您的第一种方法行不通,因为对Select组件的ref是组件本身,而不是DOM元素。

The second approach can´t work either because the element with the id select (in your case) is not the element which has the overflow. 第二种方法也不起作用,因为具有id select的元素(在您的情况下)不是发生溢出的元素。


Thanks to the classNamePrefix prop you can easily access the DOM element of the ValueContainer component, which contains all the selected options and therefore has the overflow. 多亏了classNamePrefix ValueContainer ,您可以轻松访问ValueContainer组件的DOM元素,该组件包含所有选定的选项,因此会产生溢出。

classNamePrefix classNamePrefix

If provided, all inner components will be given a prefixed className attribute. 如果提供的话,所有内部组件将被赋予一个前缀的className属性。

This is useful when styling via CSS classes instead of the Styles API approach. 通过CSS类而不是Styles API方法进行样式设置时,这很有用。

--- react-select documentation --- react-select文件

Just get the element with the id == "select" and find in it the element with the css-class react-select__value-container . 只需获取id == "select"的元素,然后在其中找到具有CSS类的元素react-select__value-container You can then manipulate the scroll values on that element to scroll to the bottom. 然后,您可以操纵该元素上的滚动值以滚动到底部。

scrollToBottom = () => {
    // element with `id` from `id` prop
    const container = document.getElementById('select');

    // `classNamePrefix` + component name
    const valueContainer = container.querySelector('.react-select__value-container');

    // Set scroll to maximum
    valueContainer.scrollTop = valueContainer.scrollHeight;
}

I solved it without using any scrollHeight or document.getElementById . 我没有使用任何scrollHeightdocument.getElementById解决了它。

I did passed a ref to Select . 我确实将一个ref传递给Select

ref={component => {
          this.node = component;
    }}

and then i used this.node.blur() to bring the focus out of Input and then this.node.focus() to again bring focus to it. 然后我使用this.node.blur()将焦点this.node.focus() Input ,然后使用this.node.focus()再次将焦点移到Input This way, when focus is brought back to Input , scroll will automatically move to bottom. 这样,当focus重新回到Input ,滚动将自动移至底部。

Updated code is in same code sandbox: https://codesandbox.io/s/react-codesandboxer-example-89cry 更新的代码在同一代码沙箱中: https : //codesandbox.io/s/react-codesandboxer-example-89cry

You can try by giving following input: 您可以尝试通过输入以下内容:

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100

and then press tab or enter . 然后按tab键或enter You will notice that scrollbar is at bottom of Input . 您会注意到滚动条在Input底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM