UNPKG

4.73 kBTypeScriptView Raw
1import { PureComponent, Validator, Requireable } from 'react';
2import { Grid, GridCoreProps, GridCellProps, OverscanIndicesGetter } from './Grid';
3import { Index, IndexRange, OverscanIndexRange, Alignment } from '../../index';
4import { CellMeasurerCache, CellPosition } from './CellMeasurer';
5import { OnScrollParams } from './ScrollSync';
6
7export type ListRowProps = Pick<GridCellProps, Exclude<keyof GridCellProps, 'rowIndex'>> & {
8 index: GridCellProps['rowIndex'];
9};
10
11export type ListRowRenderer = (props: ListRowProps) => React.ReactNode;
12
13export type RenderedRows = OverscanIndexRange & IndexRange;
14
15export type ListProps = GridCoreProps & {
16 deferredMeasurementCache?: CellMeasurerCache | undefined;
17 /**
18 * Removes fixed height from the scrollingContainer so that the total height
19 * of rows can stretch the window. Intended for use with WindowScroller
20 */
21 autoHeight?: boolean | undefined;
22 /** Optional CSS class name */
23 className?: string | undefined;
24 /**
25 * Used to estimate the total height of a List before all of its rows have actually been measured.
26 * The estimated total height is adjusted as rows are rendered.
27 */
28 estimatedRowSize?: number | undefined;
29 /** Height constraint for list (determines how many actual rows are rendered) */
30 height: number;
31 /** Optional renderer to be used in place of rows when rowCount is 0 */
32 noRowsRenderer?: (() => JSX.Element) | undefined;
33 /**
34 * Callback invoked with information about the slice of rows that were just rendered.
35 * ({ startIndex, stopIndex }): void
36 */
37 onRowsRendered?: ((info: RenderedRows) => void) | undefined;
38 /**
39 * Number of rows to render above/below the visible bounds of the list.
40 * These rows can help for smoother scrolling on touch devices.
41 */
42 overscanRowCount?: number | undefined;
43 /**
44 * Callback invoked whenever the scroll offset changes within the inner scrollable region.
45 * This callback can be used to sync scrolling between lists, tables, or grids.
46 * ({ clientHeight, scrollHeight, scrollTop }): void
47 */
48 onScroll?: ((params: OnScrollParams) => void) | undefined;
49 /** See Grid#overscanIndicesGetter */
50 overscanIndicesGetter?: OverscanIndicesGetter | undefined;
51 /**
52 * Either a fixed row height (number) or a function that returns the height of a row given its index.
53 * ({ index: number }): number
54 */
55 rowHeight: number | ((info: Index) => number);
56 /** Responsible for rendering a row given an index; ({ index: number }): node */
57 rowRenderer: ListRowRenderer;
58 /** Number of rows in list. */
59 rowCount: number;
60 /** See Grid#scrollToAlignment */
61 scrollToAlignment?: string | undefined;
62 /** Row index to ensure visible (by forcefully scrolling if necessary) */
63 scrollToIndex?: number | undefined;
64 /** Vertical offset. */
65 scrollTop?: number | undefined;
66 /** Optional inline style */
67 style?: React.CSSProperties | undefined;
68 /** Tab index for focus */
69 tabIndex?: number | null | undefined;
70 /** Width of list */
71 width: number;
72};
73/**
74 * It is inefficient to create and manage a large list of DOM elements within a scrolling container
75 * if only a few of those elements are visible. The primary purpose of this component is to improve
76 * performance by only rendering the DOM nodes that a user is able to see based on their current
77 * scroll position.
78 *
79 * This component renders a virtualized list of elements with either fixed or dynamic heights.
80 */
81export class List extends PureComponent<ListProps> {
82 static defaultProps: {
83 autoHeight: false;
84 estimatedRowSize: 30;
85 onScroll: () => void;
86 noRowsRenderer: () => null;
87 onRowsRendered: () => void;
88 overscanIndicesGetter: OverscanIndicesGetter;
89 overscanRowCount: 10;
90 scrollToAlignment: 'auto';
91 scrollToIndex: -1;
92 style: {};
93 };
94
95 Grid?: Grid | undefined;
96
97 forceUpdateGrid(): void;
98
99 /** See Grid#getOffsetForCell */
100 getOffsetForRow(params: { alignment?: Alignment | undefined; index?: number | undefined }): number;
101
102 /** CellMeasurer compatibility */
103 invalidateCellSizeAfterRender({ columnIndex, rowIndex }: CellPosition): void;
104
105 /** See Grid#measureAllCells */
106 measureAllRows(): void;
107
108 /** CellMeasurer compatibility */
109 recomputeGridSize(params?: Partial<CellPosition>): void;
110
111 /** See Grid#recomputeGridSize */
112 recomputeRowHeights(index?: number): void;
113
114 /** See Grid#scrollToPosition */
115 scrollToPosition(scrollTop?: number): void;
116
117 /** See Grid#scrollToCell */
118 scrollToRow(index?: number): void;
119}
120
121export default List;