|
| 1 | +import './App.css' |
| 2 | +import * as React from 'react' |
| 3 | +import faker from 'faker' |
| 4 | +import { findIndex, groupBy } from 'lodash' |
| 5 | +import { useVirtual, defaultRangeExtractor } from 'react-virtual' |
| 6 | + |
| 7 | +const groupedNames = groupBy( |
| 8 | + Array.from({ length: 1000 }) |
| 9 | + .map(() => faker.name.firstName()) |
| 10 | + .sort(), |
| 11 | + name => name[0] |
| 12 | +) |
| 13 | +const groups = Object.keys(groupedNames) |
| 14 | +const rows = groups.reduce((acc, k) => [...acc, k, ...groupedNames[k]], []) |
| 15 | + |
| 16 | +const App = () => { |
| 17 | + const parentRef = React.useRef() |
| 18 | + |
| 19 | + const activeStickyIndexRef = React.useRef(0) |
| 20 | + |
| 21 | + const stickyIndexes = React.useMemo( |
| 22 | + () => groups.map(gn => findIndex(rows, n => n === gn)), |
| 23 | + [] |
| 24 | + ) |
| 25 | + |
| 26 | + const isSticky = index => stickyIndexes.includes(index) |
| 27 | + |
| 28 | + const isActiveSticky = index => activeStickyIndexRef.current === index |
| 29 | + |
| 30 | + const rowVirtualizer = useVirtual({ |
| 31 | + estimateSize: React.useCallback(() => 50, []), |
| 32 | + size: rows.length, |
| 33 | + parentRef, |
| 34 | + rangeExtractor: React.useCallback( |
| 35 | + range => { |
| 36 | + activeStickyIndexRef.current = [...stickyIndexes] |
| 37 | + .reverse() |
| 38 | + .find(index => range.start >= index) |
| 39 | + |
| 40 | + const next = new Set([ |
| 41 | + activeStickyIndexRef.current, |
| 42 | + ...defaultRangeExtractor(range), |
| 43 | + ]) |
| 44 | + |
| 45 | + return [...next].sort((a, b) => a - b) |
| 46 | + }, |
| 47 | + [stickyIndexes] |
| 48 | + ), |
| 49 | + }) |
| 50 | + |
| 51 | + return ( |
| 52 | + <div> |
| 53 | + <div |
| 54 | + ref={parentRef} |
| 55 | + className="List" |
| 56 | + style={{ |
| 57 | + height: `300px`, |
| 58 | + width: `400px`, |
| 59 | + overflow: 'auto', |
| 60 | + }} |
| 61 | + > |
| 62 | + <div |
| 63 | + style={{ |
| 64 | + height: `${rowVirtualizer.totalSize}px`, |
| 65 | + width: '100%', |
| 66 | + position: 'relative', |
| 67 | + }} |
| 68 | + > |
| 69 | + {rowVirtualizer.virtualItems.map(virtualRow => ( |
| 70 | + <div |
| 71 | + key={virtualRow.index} |
| 72 | + className={'ListItem'} |
| 73 | + style={{ |
| 74 | + ...(isSticky(virtualRow.index) |
| 75 | + ? { |
| 76 | + background: '#fff', |
| 77 | + borderBottom: '1px solid #ddd', |
| 78 | + zIndex: 1, |
| 79 | + } |
| 80 | + : {}), |
| 81 | + ...(isActiveSticky(virtualRow.index) |
| 82 | + ? { |
| 83 | + position: 'sticky', |
| 84 | + } |
| 85 | + : { |
| 86 | + position: 'absolute', |
| 87 | + transform: `translateY(${virtualRow.start}px)`, |
| 88 | + }), |
| 89 | + top: 0, |
| 90 | + left: 0, |
| 91 | + width: '100%', |
| 92 | + height: `${virtualRow.size}px`, |
| 93 | + }} |
| 94 | + > |
| 95 | + {rows[virtualRow.index]} |
| 96 | + </div> |
| 97 | + ))} |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + {process.env.NODE_ENV === 'development' ? ( |
| 101 | + <p> |
| 102 | + <strong>Notice:</strong> You are currently running React in |
| 103 | + development mode. Rendering performance will be slightly degraded |
| 104 | + until this application is build for production. |
| 105 | + </p> |
| 106 | + ) : null} |
| 107 | + </div> |
| 108 | + ) |
| 109 | +} |
| 110 | + |
| 111 | +export default App |
0 commit comments