UNPKG

2.73 kBTypeScriptView Raw
1import { PureComponent } from 'react';
2import { Grid, GridCoreProps, GridCellProps, OverscanIndicesGetter } from './Grid';
3import { IndexRange, OverscanIndexRange, Alignment } from '../../index';
4import { CellPosition } from './CellMeasurer';
5
6export type ListRowProps = Pick<GridCellProps, Exclude<keyof GridCellProps, 'rowIndex'>> & {
7 index: GridCellProps['rowIndex'];
8};
9
10export type ListRowRenderer = (props: ListRowProps) => React.ReactNode;
11
12export type RenderedRows = OverscanIndexRange & IndexRange;
13
14export type ListProps = GridCoreProps & {
15 /** Optional renderer to be used in place of rows when rowCount is 0 */
16 noRowsRenderer?: (() => JSX.Element) | undefined;
17 /**
18 * Callback invoked with information about the slice of rows that were just rendered.
19 * ({ startIndex, stopIndex }): void
20 */
21 onRowsRendered?: ((info: RenderedRows) => void) | undefined;
22 /** Responsible for rendering a row given an index; ({ index: number }): node */
23 rowRenderer: ListRowRenderer;
24 /** Row index to ensure visible (by forcefully scrolling if necessary) */
25 scrollToIndex?: number | undefined;
26};
27/**
28 * It is inefficient to create and manage a large list of DOM elements within a scrolling container
29 * if only a few of those elements are visible. The primary purpose of this component is to improve
30 * performance by only rendering the DOM nodes that a user is able to see based on their current
31 * scroll position.
32 *
33 * This component renders a virtualized list of elements with either fixed or dynamic heights.
34 */
35export class List extends PureComponent<ListProps> {
36 static defaultProps: {
37 autoHeight: false;
38 estimatedRowSize: 30;
39 onScroll: () => void;
40 noRowsRenderer: () => null;
41 onRowsRendered: () => void;
42 overscanIndicesGetter: OverscanIndicesGetter;
43 overscanRowCount: 10;
44 scrollToAlignment: 'auto';
45 scrollToIndex: -1;
46 style: {};
47 };
48
49 Grid?: Grid | undefined;
50
51 forceUpdateGrid(): void;
52
53 /** See Grid#getOffsetForCell */
54 getOffsetForRow(params: { alignment?: Alignment | undefined; index?: number | undefined }): number;
55
56 /** CellMeasurer compatibility */
57 invalidateCellSizeAfterRender({ columnIndex, rowIndex }: CellPosition): void;
58
59 /** See Grid#measureAllCells */
60 measureAllRows(): void;
61
62 /** CellMeasurer compatibility */
63 recomputeGridSize(params?: Partial<CellPosition>): void;
64
65 /** See Grid#recomputeGridSize */
66 recomputeRowHeights(index?: number): void;
67
68 /** See Grid#scrollToPosition */
69 scrollToPosition(scrollTop?: number): void;
70
71 /** See Grid#scrollToCell */
72 scrollToRow(index?: number): void;
73}
74
75export default List;