简体   繁体   English

滚动到 material-ui 中的选定列表项

[英]Scroll to selected list item in material-ui

I have a list, build with material-ui.我有一个列表,使用 material-ui 构建。 There are a lot of items in it, so scrollbar is visible.里面有很多项目,所以滚动条是可见的。

What I would like to do is scroll to the selected item.我想做的是滚动到所选项目。 Have any ideas of how to implement this?对如何实现这一点有任何想法吗?

Here is a demo sendbox link这是一个演示发送箱链接

After click on the item list should looks like this (selected item is in the center):单击项目列表后应如下所示(所选项目位于中心):

在此处输入图像描述

I know there is an accepted answer here, but I think using我知道这里有一个公认的答案,但我认为使用

<ListItem autoFocus={true}/>

would scroll that list item into view.将该列表项滚动到视图中。 The same logic to set the list item to be marked as selected could be used for setting the autoFocus attribute as well.设置要标记为选中的列表项的相同逻辑也可用于设置autoFocus属性。

Hold a ref to the List , and upon click on ListItem , calculate how much you need to scroll based on:保持对List的引用,然后单击ListItem ,根据以下内容计算您需要滚动多少:

  1. list item height列表项高度
  2. the index of the selected item所选项目的索引
  3. number of visible list items.可见列表项的数量。

     const scrollableListRef = React.createRef(); function Row(props) { const { index, style } = props; const placeSelectedItemInTheMiddle = (index) => { const LIST_ITEM_HEIGHT = 46; const NUM_OF_VISIBLE_LIST_ITEMS = 9; const amountToScroll = LIST_ITEM_HEIGHT * (index - (NUM_OF_VISIBLE_LIST_ITEMS / 2) + 1); scrollableListRef.current.scrollTo(amountToScroll, 0); } return ( <ListItem button style={style} key={index} onClick={() => {placeSelectedItemInTheMiddle(index)}}> <ListItemText primary={`Item ${index + 1}`} /> </ListItem> ); }

编辑隐形背景

Chandra Hasa's answer should include a bit of extra explanation but the edit queue was full. Chandra Hasa 的回答应该包括一些额外的解释,但编辑队列已满。

Material-ui has a built-in way to do this with the autoFocus prop. Material-ui 具有使用autoFocus执行此操作的内置方法。 See the table below taken from the MUI Docs .请参阅从MUI Docs获取的下表。

Prop支柱 Type类型 Default默认 Description描述
autoFocus自动对焦 boolean boolean false错误的 If true, the list item is focused during the first mount.如果为 true,则列表项在第一次挂载期间获得焦点。 Focus will also be triggered if the value changes from false to true.如果值从 false 变为 true,也会触发焦点。

❗️ The autoFocus prop should only be true for one item in the list. ❗️ autoFocus应该只true于列表中的一项 If you add autoFocus={true} to every ListItem , it will continuously scroll to each element, like OP mentioned in his comment .如果您将autoFocus={true}添加到每个ListItem ,它将不断滚动到每个元素,就像他的评论中提到的 OP 一样。

The browser will scroll to the one ListItem that has autoFocus={true} .浏览器将滚动到具有autoFocus={true}一个ListItem

If you want to always scroll to the same ListItem every time the component renders, you can add autoFocus={true} to that one ListItem component, like this:如果您希望每次组件呈现时始终滚动到同一个ListItem ,您可以将autoFocus={true}添加到ListItem组件,如下所示:

<List>
  <ListItem>item1</ListItem>
  <ListItem autoFocus={true}>The list will automatically scroll to this item</ListItem>
  <ListItem>item3</ListItem>
</List>

In most cases, people want to dynamically determine which ListItem to scroll to.在大多数情况下,人们希望动态确定要滚动到哪个ListItem In this case, the value of the autoFocus prop needs to evaluate to a boolean .在这种情况下, autoFocus的值需要评估为boolean It should still only be true for one item in the list.它仍然应该只true于列表中的一项

One use case is if you're rendering a long list with one option already selected.一个用例是,如果您要呈现一个已经选择了一个选项的长列表。 It's good to automatically scroll so the selected option is visible.最好自动滚动以使所选选项可见。

/* The list will auto-scroll to the **one** item that has an
`autoFocus` prop that evaluates to `true`. 
If `selectedItem === item2`, 
the list will automatically scroll to `item2`.
*/
<List>
  <ListItem autoFocus={ selectedItem === item1 }> item1 </ListItem>
  <ListItem autoFocus={ selectedItem === item2 }> item2 </ListItem>
  <ListItem autoFocus={ selectedItem === item3 }> item3 </ListItem>
</List>

❗️ This question and answers use MUI v4. ❗️ 本问答使用 MUI v4。 MUI v5 deprecated the autoFocus prop for ListItem components. MUI v5弃用ListItem组件的autoFocus Use ListItemButton instead.请改用ListItemButton Don't worry, it doesn't look like an MUI Button component.别担心,它看起来不像 MUI Button组件。

Deprecated - checkout ListItemButton instead (Source: MUI Docs )已弃用 - 改为结帐 ListItemButton(来源: MUI Docs

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

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