zoukankan      html  css  js  c++  java
  • [React] React Virtual

    // Window large lists with react-virtual
    // http://localhost:3000/isolated/final/04.js
    
    import React from 'react'
    import {useVirtual} from 'react-virtual'
    import {useCombobox} from '../use-combobox'
    import {getItems} from '../workerized-filter-cities'
    import {useAsync, useForceRerender} from '../utils'
    
    const getVirtualRowStyles = ({size, start}) => ({
      position: 'absolute',
      top: 0,
      left: 0,
       '100%',
      height: size,
      transform: `translateY(${start}px)`,
    })
    
    function Menu({
      items,
      getMenuProps,
      getItemProps,
      highlightedIndex,
      selectedItem,
      listRef,
      virtualRows,
      totalHeight,
    }) {
      return (
        <ul {...getMenuProps({ref: listRef})}>
          <li style={{height: totalHeight}} /> // The reason here add a li is tell react virtual, the total height of the items, so the scolling bar can display correct size
          {virtualRows.map(({index, size, start}) => {
            const item = items[index]
            if (!item) return null
            return (
              <ListItem
                key={item.id}
                getItemProps={getItemProps}
                item={item}
                index={index}
                isSelected={selectedItem?.id === item.id}
                isHighlighted={highlightedIndex === index}
                style={getVirtualRowStyles({size, start})}
              >
                {item.name}
              </ListItem>
            )
          })}
        </ul>
      )
    }
    
    function ListItem({
      getItemProps,
      item,
      index,
      isHighlighted,
      isSelected,
      style,
      ...props
    }) {
      return (
        <li
          {...getItemProps({
            index,
            item,
            style: {
              backgroundColor: isHighlighted ? 'lightgray' : 'inherit',
              fontWeight: isSelected ? 'bold' : 'normal',
              ...style,
            },
            ...props,
          })}
        />
      )
    }
    
    function App() {
      const forceRerender = useForceRerender()
      const [inputValue, setInputValue] = React.useState('')
    
      const {data: items, run} = useAsync({data: [], status: 'pending'})
      React.useEffect(() => {
        run(getItems(inputValue))
      }, [inputValue, run])
    
      const listRef = React.useRef()
    
      const rowVirtualizer = useVirtual({
        size: items.length,
        parentRef: listRef,
        estimateSize: React.useCallback(() => 20, []),
        overscan: 10,
      })
    
      const {
        selectedItem,
        highlightedIndex,
        getComboboxProps,
        getInputProps,
        getItemProps,
        getLabelProps,
        getMenuProps,
        selectItem,
      } = useCombobox({
        items,
        inputValue,
        onInputValueChange: ({inputValue: newValue}) => setInputValue(newValue),
        onSelectedItemChange: ({selectedItem}) =>
          alert(
            selectedItem
              ? `You selected ${selectedItem.name}`
              : 'Selection Cleared',
          ),
        itemToString: item => (item ? item.name : ''),
        scrollIntoView: () => {},
        onHighlightedIndexChange: ({highlightedIndex}) =>
          highlightedIndex !== -1 && rowVirtualizer.scrollToIndex(highlightedIndex),
      })
    
      return (
        <div className="city-app">
          <button onClick={forceRerender}>force rerender</button>
          <div>
            <label {...getLabelProps()}>Find a city</label>
            <div {...getComboboxProps()}>
              <input {...getInputProps({type: 'text'})} />
              <button onClick={() => selectItem(null)} aria-label="toggle menu">
                &#10005;
              </button>
            </div>
            <Menu
              items={items}
              getMenuProps={getMenuProps}
              getItemProps={getItemProps}
              highlightedIndex={highlightedIndex}
              selectedItem={selectedItem}
              listRef={listRef}
              virtualRows={rowVirtualizer.virtualItems}
              totalHeight={rowVirtualizer.totalSize}
            />
          </div>
        </div>
      )
    }
    
    export default App
  • 相关阅读:
    C#利用System.Net发送邮件(带 抄送、密送、附件、html格式的邮件)
    ASP.NET跨平台实践:无需安装Mono的Jexus“独立版”
    在.NET Core之前,实现.Net跨平台之Mono+CentOS+Jexus初体验
    初识Docker和Windows Server容器
    windows 7 docker oralce安装和使用
    javaweb学习总结(三十)——EL函数库
    javaweb学习总结(二十九)——EL表达式
    javaweb学习总结(二十八)——JSTL标签库之核心标签
    javaweb学习总结(二十七)——jsp简单标签开发案例和打包
    在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13861798.html
Copyright © 2011-2022 走看看