|
| 1 | +import * as React from 'react'; |
| 2 | +import { findDOMNode } from 'react-dom'; |
| 3 | +import Filler from './Filler'; |
| 4 | +import { getLocationItem, getScrollPercentage } from './util'; |
| 5 | + |
| 6 | +type RenderFunc<T> = (item: T) => React.ReactNode; |
| 7 | + |
| 8 | +export interface ListProps<T> extends React.HTMLAttributes<any> { |
| 9 | + children: RenderFunc<T>; |
| 10 | + dataSource: T[]; |
| 11 | + height?: number; |
| 12 | + itemHeight?: number; |
| 13 | + component?: string | React.FC<any> | React.ComponentClass<any>; |
| 14 | +} |
| 15 | + |
| 16 | +interface ListState { |
| 17 | + status: 'NONE' | 'MEASURE_START' | 'MEASURE_DONE'; |
| 18 | + |
| 19 | + itemIndex: number; |
| 20 | + itemOffsetPtg: number; |
| 21 | + startIndex: number; |
| 22 | + endIndex: number; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * We use class component here since typescript can not support generic in function component |
| 27 | + * |
| 28 | + * Virtual list display logic: |
| 29 | + * 1. scroll / initialize trigger measure |
| 30 | + * 2. Get location item of current `scrollTop` |
| 31 | + * 3. [Render] Render visible items |
| 32 | + * 4. Get all the visible items height |
| 33 | + * 5. [Render] Update top item `margin-top` to fit the position |
| 34 | + */ |
| 35 | +class List<T> extends React.Component<ListProps<T>, ListState> { |
| 36 | + static defaultProps = { |
| 37 | + itemHeight: 15, |
| 38 | + dataSource: [], |
| 39 | + }; |
| 40 | + |
| 41 | + state: ListState = { |
| 42 | + status: 'NONE', |
| 43 | + itemIndex: 0, |
| 44 | + itemOffsetPtg: 0, |
| 45 | + startIndex: 0, |
| 46 | + endIndex: 0, |
| 47 | + }; |
| 48 | + |
| 49 | + listRef = React.createRef<HTMLElement>(); |
| 50 | + |
| 51 | + itemElements: { [index: number]: HTMLElement } = {}; |
| 52 | + |
| 53 | + /** |
| 54 | + * Initial should sync with default scroll top |
| 55 | + */ |
| 56 | + public componentDidMount() { |
| 57 | + this.listRef.current.scrollTop = 0; |
| 58 | + this.onScroll(); |
| 59 | + } |
| 60 | + |
| 61 | + public componentDidUpdate() { |
| 62 | + const { status, startIndex, endIndex } = this.state; |
| 63 | + if (status === 'MEASURE_START') { |
| 64 | + const heightList: number[] = []; |
| 65 | + for (let index = startIndex; index <= endIndex; index += 1) { |
| 66 | + const element: HTMLElement = this.itemElements[index]; |
| 67 | + heightList[index] = |
| 68 | + 'offsetHeight' in element |
| 69 | + ? element.offsetHeight |
| 70 | + : (findDOMNode(element) as HTMLElement).offsetHeight; |
| 71 | + } |
| 72 | + this.setState({ status: 'MEASURE_DONE' }); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Phase 2: Trigger render since we should re-calculate current position. |
| 78 | + */ |
| 79 | + public onScroll = () => { |
| 80 | + const { dataSource, height, itemHeight } = this.props; |
| 81 | + |
| 82 | + const scrollTopPtg = getScrollPercentage(this.listRef.current); |
| 83 | + const { index, offsetPtg } = getLocationItem(scrollTopPtg, dataSource.length); |
| 84 | + const visibleCount = Math.ceil(height / itemHeight); |
| 85 | + |
| 86 | + const beforeCount = Math.ceil(scrollTopPtg * visibleCount); |
| 87 | + const afterCount = Math.ceil((1 - scrollTopPtg) * visibleCount); |
| 88 | + |
| 89 | + this.setState({ |
| 90 | + status: 'MEASURE_START', |
| 91 | + itemIndex: index, |
| 92 | + itemOffsetPtg: offsetPtg, |
| 93 | + startIndex: Math.max(0, index - beforeCount), |
| 94 | + endIndex: Math.min(dataSource.length - 1, index + afterCount), |
| 95 | + }); |
| 96 | + }; |
| 97 | + |
| 98 | + public renderChildren = (list: T[], renderFunc: RenderFunc<T>) => |
| 99 | + // We should measure rendered item height |
| 100 | + list.map((item, index) => { |
| 101 | + const node = renderFunc(item) as React.ReactElement; |
| 102 | + |
| 103 | + // Pass `key` and `ref` for internal measure |
| 104 | + return React.cloneElement(node, { |
| 105 | + key: index, |
| 106 | + ref: (ele: HTMLElement) => { |
| 107 | + this.itemElements[index] = ele; |
| 108 | + }, |
| 109 | + }); |
| 110 | + }) |
| 111 | + ; |
| 112 | + |
| 113 | + public render() { |
| 114 | + const { |
| 115 | + style, |
| 116 | + component: Component = 'div', |
| 117 | + height, |
| 118 | + itemHeight, |
| 119 | + dataSource, |
| 120 | + children, |
| 121 | + ...restProps |
| 122 | + } = this.props; |
| 123 | + |
| 124 | + // Render pure list if not set height |
| 125 | + if (height === undefined) { |
| 126 | + return ( |
| 127 | + <Component style={style} {...restProps}> |
| 128 | + {this.renderChildren(dataSource, children)} |
| 129 | + </Component> |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + const { itemIndex, startIndex, endIndex } = this.state; |
| 134 | + |
| 135 | + const contentHeight = dataSource.length * itemHeight; |
| 136 | + |
| 137 | + return ( |
| 138 | + <Component |
| 139 | + style={{ |
| 140 | + ...style, |
| 141 | + height, |
| 142 | + overflowY: 'auto', |
| 143 | + }} |
| 144 | + {...restProps} |
| 145 | + onScroll={this.onScroll} |
| 146 | + ref={this.listRef} |
| 147 | + > |
| 148 | + <Filler height={contentHeight}> |
| 149 | + {this.renderChildren(dataSource.slice(startIndex, endIndex + 1), children)} |
| 150 | + </Filler> |
| 151 | + </Component> |
| 152 | + ); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +export default List; |
0 commit comments