UNPKG

23.3 kBTypeScriptView Raw
1// Type definitions for react-window 1.8
2// Project: https://github.com/bvaughn/react-window/, http://react-window.now.sh
3// Definitions by: Martynas Kadiša <https://github.com/martynaskadisa>
4// Alex Guerra <https://github.com/heyimalex>
5// John Gozde <https://github.com/jgoz>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7// TypeScript Version: 2.8
8
9import {
10 Component,
11 ComponentType,
12 CSSProperties,
13 Ref,
14 Key,
15 FunctionComponent,
16 ComponentClass
17} from "react";
18
19export type CSSDirection = "ltr" | "rtl";
20export type Direction = "vertical" | "horizontal"; // TODO: deprecate in favour of Layout
21export type Layout = "vertical" | "horizontal";
22export type ScrollDirection = "forward" | "backward";
23export type Align = "auto" | "smart" | "center" | "end" | "start";
24
25export interface ListChildComponentProps<T = any> {
26 index: number;
27 style: CSSProperties;
28 data: T;
29 isScrolling?: boolean | undefined;
30}
31
32export interface GridChildComponentProps<T = any> {
33 columnIndex: number;
34 rowIndex: number;
35 style: CSSProperties;
36 data: T;
37 isScrolling?: boolean | undefined;
38}
39
40// This is supposed to represent the type of the first parameter to
41// React.createElement.
42export type ReactElementType =
43 | FunctionComponent<any>
44 | ComponentClass<any>
45 | string;
46
47export interface CommonProps<T = any> {
48 /**
49 * Optional CSS class to attach to outermost <div> element.
50 */
51 className?: string | undefined;
52 /**
53 * Tag name passed to document.createElement to create the inner container element. This is an advanced property; in most cases, the default ("div") should be used.
54 */
55 innerElementType?: ReactElementType | undefined;
56 /**
57 * Ref to attach to the inner container element. This is an advanced property.
58 */
59 innerRef?: Ref<any> | undefined;
60 /**
61 * Tag name passed to document.createElement to create the inner container element. This is an advanced property; in most cases, the default ("div") should be used.
62 *
63 * @deprecated since 1.4.0
64 */
65 innerTagName?: string | undefined;
66 /**
67 * Contextual data to be passed to the item renderer as a data prop. This is a light-weight alternative to React's built-in context API.
68 *
69 * Item data is useful for item renderers that are class components.
70 */
71 itemData?: T | undefined;
72 /**
73 * Tag name passed to document.createElement to create the outer container element. This is an advanced property; in most cases, the default ("div") should be used.
74 */
75 outerElementType?: ReactElementType | undefined;
76 /**
77 * Ref to attach to the outer container element. This is an advanced property.
78 */
79 outerRef?: Ref<any> | undefined;
80 /**
81 * Tag name passed to document.createElement to create the outer container element. This is an advanced property; in most cases, the default ("div") should be used.
82 *
83 * @deprecated since 1.4.0
84 */
85 outerTagName?: string | undefined;
86 /**
87 * Optional inline style to attach to outermost <div> element.
88 */
89 style?: CSSProperties | undefined;
90 /**
91 * Adds an additional isScrolling parameter to the children render function. This parameter can be used to show a placeholder row or column while the list is being scrolled.
92 *
93 * Note that using this parameter will result in an additional render call after scrolling has stopped (when isScrolling changes from true to false).
94 */
95 useIsScrolling?: boolean | undefined;
96}
97
98export type ListItemKeySelector<T = any> = (index: number, data: T) => Key;
99
100export interface ListOnItemsRenderedProps {
101 overscanStartIndex: number;
102 overscanStopIndex: number;
103 visibleStartIndex: number;
104 visibleStopIndex: number;
105}
106
107export interface ListOnScrollProps {
108 scrollDirection: ScrollDirection;
109 scrollOffset: number;
110 scrollUpdateWasRequested: boolean;
111}
112
113export interface ListProps<T = any> extends CommonProps<T> {
114 /**
115 * React component responsible for rendering the individual item specified by an index prop. This component also receives a style prop (used for positioning).
116 *
117 * If useIsScrolling is enabled for the list, the component also receives an additional isScrolling boolean prop.
118 */
119 children: ComponentType<ListChildComponentProps<T>>;
120 /**
121 * Height of the list.
122 *
123 * For vertical lists, this must be a number. It affects the number of rows that will be rendered (and displayed) at any given time.
124 *
125 * For horizontal lists, this can be a number or a string (e.g. "50%").
126 */
127 height: number | string;
128 /**
129 * Total number of items in the list. Note that only a few items will be rendered and displayed at a time.
130 */
131 itemCount: number;
132 /**
133 * Width of the list.
134 *
135 * For horizontal lists, this must be a number. It affects the number of columns that will be rendered (and displayed) at any given time.
136 *
137 * For vertical lists, this can be a number or a string (e.g. "50%").
138 */
139 width: number | string;
140 /**
141 * Determines the direction of text and horizontal scrolling.
142 *
143 * This property also automatically sets the CSS direction style for the list component.
144 *
145 * Specifying "horizontal" or "vertical" for this value is deprecated. Use "layout" prop instead.
146 *
147 * @default "ltr"
148 */
149 direction?: CSSDirection | Direction | undefined;
150 /**
151 * Layout/orientation of the list.
152 *
153 * Acceptable values are:
154 * - "vertical" (default) - Up/down scrolling.
155 * - "horizontal" - Left/right scrolling.
156 *
157 * Note that lists may scroll in both directions (depending on CSS) but content will only be windowed in the layout direction specified.
158 */
159 layout?: Layout | undefined;
160 /**
161 * Scroll offset for initial render.
162 *
163 * For vertical lists, this affects scrollTop. For horizontal lists, this affects scrollLeft.
164 */
165 initialScrollOffset?: number | undefined;
166 /**
167 * By default, lists will use an item's index as its key. This is okay if:
168 *
169 * - Your collections of items is never sorted or modified
170 * - Your item renderer is not stateful and does not extend PureComponent
171 *
172 * If your list does not satisfy the above constraints, use the itemKey property to specify your own keys for items
173 */
174 itemKey?: ListItemKeySelector<T> | undefined;
175 /**
176 * The number of items (rows or columns) to render outside of the visible area. This property can be important for two reasons:
177 *
178 * - Overscanning by one row or column allows the tab key to focus on the next (not yet visible) item.
179 * - Overscanning slightly can reduce or prevent a flash of empty space when a user first starts scrolling.
180 *
181 * Note that overscanning too much can negatively impact performance. By default, List overscans by one item.
182 */
183 overscanCount?: number | undefined;
184 /**
185 * Called when the items rendered by the list change.
186 */
187 onItemsRendered?: ((props: ListOnItemsRenderedProps) => any) | undefined;
188 /**
189 * Called when the list scroll positions changes, as a result of user scrolling or scroll-to method calls.
190 */
191 onScroll?: ((props: ListOnScrollProps) => any) | undefined;
192}
193
194export type GridItemKeySelector<T = any> = (params: {
195 columnIndex: number;
196 rowIndex: number;
197 data: T;
198}) => Key;
199
200export interface GridOnItemsRenderedProps {
201 overscanColumnStartIndex: number;
202 overscanColumnStopIndex: number;
203 overscanRowStartIndex: number;
204 overscanRowStopIndex: number;
205 visibleColumnStartIndex: number;
206 visibleColumnStopIndex: number;
207 visibleRowStartIndex: number;
208 visibleRowStopIndex: number;
209}
210
211export interface GridOnScrollProps {
212 horizontalScrollDirection: ScrollDirection;
213 scrollLeft: number;
214 scrollTop: number;
215 scrollUpdateWasRequested: boolean;
216 verticalScrollDirection: ScrollDirection;
217}
218
219export interface GridProps<T = any> extends CommonProps<T> {
220 /**
221 * React component responsible for rendering the individual item specified by an index prop. This component also receives a style prop (used for positioning).
222 *
223 * If useIsScrolling is enabled for the list, the component also receives an additional isScrolling boolean prop.
224 */
225 children: ComponentType<GridChildComponentProps<T>>;
226 /**
227 * Number of columns in the grid. Note that only a few columns will be rendered and displayed at a time.
228 */
229 columnCount: number;
230 /**
231 * Determines the direction of text and horizontal scrolling.
232 *
233 * This property also automatically sets the CSS direction style for the grid component.
234 *
235 * @default "ltr"
236 */
237 direction?: CSSDirection | undefined;
238 /**
239 * Height of the grid. This affects the number of rows that will be rendered (and displayed) at any given time.
240 */
241 height: number;
242 /**
243 * Horizontal scroll offset for initial render.
244 */
245 initialScrollLeft?: number | undefined;
246 /**
247 * Vertical scroll offset for initial render.
248 */
249 initialScrollTop?: number | undefined;
250 /**
251 * By default, grids will use an item's indices as its key. This is okay if:
252 *
253 * - Your collections of items is never sorted or modified
254 * - Your item renderer is not stateful and does not extend PureComponent
255 *
256 * If your grid does not satisfy the above constraints, use the itemKey property to specify your own keys for items.
257 */
258 itemKey?: GridItemKeySelector<T> | undefined;
259 /**
260 * Called when the items rendered by the grid change.
261 */
262 onItemsRendered?: ((props: GridOnItemsRenderedProps) => any) | undefined;
263 /**
264 * Called when the grid scroll positions changes, as a result of user scrolling or scroll-to method calls.
265 */
266 onScroll?: ((props: GridOnScrollProps) => any) | undefined;
267 /**
268 * @deprecated since version 1.8.2, please use overscanColumnCount
269 */
270 overscanColumnsCount?: number | undefined;
271 /**
272 * The number of columns to render outside of the visible area. This property can be important for two reasons:
273 *
274 * - Overscanning by one row or column allows the tab key to focus on the next (not yet visible) item.
275 * - Overscanning slightly can reduce or prevent a flash of empty space when a user first starts scrolling.
276 *
277 * Note that overscanning too much can negatively impact performance. By default, grid overscans by one item.
278 */
279 overscanColumnCount?: number | undefined;
280 /**
281 * @deprecated since version 1.8.2, please use overscanRowCount
282 */
283 overscanRowsCount?: number | undefined;
284 /**
285 * The number of rows to render outside of the visible area. This property can be important for two reasons:
286 *
287 * - Overscanning by one row or column allows the tab key to focus on the next (not yet visible) item.
288 * - Overscanning slightly can reduce or prevent a flash of empty space when a user first starts scrolling.
289 *
290 * Note that overscanning too much can negatively impact performance. By default, grid overscans by one item.
291 */
292 overscanRowCount?: number | undefined;
293 /**
294 * The number of items (rows or columns) to render outside of the visible area. This property can be important for two reasons:
295 *
296 * - Overscanning by one row or column allows the tab key to focus on the next (not yet visible) item.
297 * - Overscanning slightly can reduce or prevent a flash of empty space when a user first starts scrolling.
298 *
299 * Note that overscanning too much can negatively impact performance. By default, grid overscans by one item.
300 *
301 * @deprecated since version 1.4.0
302 */
303 overscanCount?: number | undefined;
304 /**
305 * Number of rows in the grid. Note that only a few rows will be rendered and displayed at a time.
306 */
307 rowCount: number;
308 /**
309 * Width of the grid. This affects the number of columns that will be rendered (and displayed) at any given time.
310 */
311 width: number;
312}
313
314export interface FixedSizeListProps<T = any> extends ListProps<T> {
315 /**
316 * Size of a item in the direction being windowed. For vertical lists, this is the row height. For horizontal lists, this is the column width.
317 */
318 itemSize: number;
319}
320
321export interface VariableSizeListProps<T = any> extends ListProps<T> {
322 /**
323 * Estimated size of a item in the direction being windowed. For vertical lists, this is the row height. For horizontal lists, this is the column width.
324 *
325 * This value is used to calculated the estimated total size of a list before its items have all been measured. The total size impacts user scrolling behavior.
326 * It is updated whenever new items are measured.
327 */
328 estimatedItemSize?: number | undefined;
329 /**
330 * Returns the size of a item in the direction being windowed. For vertical lists, this is the row height. For horizontal lists, this is the column width.
331 */
332 itemSize: (index: number) => number;
333}
334
335export interface FixedSizeGridProps<T = any> extends GridProps<T> {
336 /**
337 * Width of an individual column within the grid.
338 */
339 columnWidth: number;
340 /**
341 * Height of an individual row within the grid.
342 */
343 rowHeight: number;
344}
345
346export interface VariableSizeGridProps<T = any> extends GridProps<T> {
347 /**
348 * Returns the width of the specified column.
349 */
350 columnWidth: (index: number) => number;
351 /**
352 * Average (or estimated) column width for unrendered columns.
353 *
354 * This value is used to calculated the estimated total width of a Grid before its columns have all been measured.
355 * The estimated width impacts user scrolling behavior. It is updated whenever new columns are measured.
356 */
357 estimatedColumnWidth?: number | undefined;
358 /**
359 * Average (or estimated) row height for unrendered rows.
360 *
361 * This value is used to calculated the estimated total height of a Grid before its rows have all been measured.
362 * The estimated height impacts user scrolling behavior. It is updated whenever new rows are measured.
363 */
364 estimatedRowHeight?: number | undefined;
365 /**
366 * Returns the height of the specified row.
367 */
368 rowHeight: (index: number) => number;
369}
370
371export class FixedSizeList<T = any> extends Component<FixedSizeListProps<T>> {
372 /**
373 * Scroll to the specified offset (scrollTop or scrollLeft, depending on the direction prop).
374 */
375 scrollTo(scrollOffset: number): void;
376 /**
377 * Scroll to the specified item.
378 *
379 * By default, the List will scroll as little as possible to ensure the item is visible.
380 * You can control the alignment of the item though by specifying a second alignment parameter. Acceptable values are:
381 *
382 * - auto (default) - Scroll as little as possible to ensure the item is visible. (If the item is already visible, it won't scroll at all.)
383 * - smart
384 * - If the item is already visible, don't scroll at all.
385 * - If it is less than one viewport away, scroll as little as possible so that it becomes visible.
386 * - If it is more than one viewport away, scroll so that it is centered within the list.
387 * - center - Center align the item within the list.
388 * - end - Align the item to the end of the list (the bottom for vertical lists or the right for horizontal lists).
389 * - start - Align the item to the beginning of the list (the top for vertical lists or the left for horizontal lists).
390 */
391 scrollToItem(index: number, align?: Align): void;
392}
393
394export class VariableSizeList<T = any> extends Component<VariableSizeListProps<T>> {
395 /**
396 * Scroll to the specified offset (scrollTop or scrollLeft, depending on the direction prop).
397 */
398 scrollTo(scrollOffset: number): void;
399 /**
400 * Scroll to the specified item.
401 *
402 * By default, the List will scroll as little as possible to ensure the item is visible.
403 * You can control the alignment of the item though by specifying a second alignment parameter. Acceptable values are:
404 *
405 * - auto (default) - Scroll as little as possible to ensure the item is visible. (If the item is already visible, it won't scroll at all.)
406 * - smart
407 * - If the item is already visible, don't scroll at all.
408 * - If it is less than one viewport away, scroll as little as possible so that it becomes visible.
409 * - If it is more than one viewport away, scroll so that it is centered within the list.
410 * - center - Center align the item within the list.
411 * - end - Align the item to the end of the list (the bottom for vertical lists or the right for horizontal lists).
412 * - start - Align the item to the beginning of the list (the top for vertical lists or the left for horizontal lists).
413 */
414 scrollToItem(index: number, align?: Align): void;
415 /**
416 * VariableSizeList caches offsets and measurements for each index for performance purposes.
417 * This method clears that cached data for all items after (and including) the specified index.
418 * It should be called whenever a item's size changes. (Note that this is not a typical occurrence.)
419 *
420 * By default the list will automatically re-render after the index is reset.
421 * If you would like to delay this re-render until e.g. a state update has completed in the parent component,
422 * specify a value of false for the second, optional parameter.
423 */
424 resetAfterIndex(index: number, shouldForceUpdate?: boolean): void;
425}
426
427export class FixedSizeGrid<T = any> extends Component<FixedSizeGridProps<T>> {
428 /**
429 * Scroll to the specified offsets.
430 */
431 scrollTo(params: { scrollLeft?: number; scrollTop?: number }): void;
432 /**
433 * Scroll to the specified item.
434 *
435 * By default, the Grid will scroll as little as possible to ensure the item is visible.
436 * You can control the alignment of the item though by specifying an `align` property. Acceptable values are:
437 *
438 * - auto (default) - Scroll as little as possible to ensure the item is visible. (If the item is already visible, it won't scroll at all.)
439 * - smart
440 * - If the item is already visible, don't scroll at all.
441 * - If it is less than one viewport away, scroll as little as possible so that it becomes visible.
442 * - If it is more than one viewport away, scroll so that it is centered within the grid.
443 * - center - Center align the item within the grid.
444 * - end - Align the item to the bottom, right hand side of the grid.
445 * - start - Align the item to the top, left hand of the grid.
446 *
447 * If either `columnIndex` or `rowIndex` are omitted, `scrollLeft` or `scrollTop` will be unchanged (respectively).
448 */
449 scrollToItem(params: {
450 align?: Align | undefined;
451 columnIndex?: number | undefined;
452 rowIndex?: number | undefined;
453 }): void;
454}
455
456export class VariableSizeGrid<T = any> extends Component<VariableSizeGridProps<T>> {
457 /**
458 * Scroll to the specified offsets.
459 */
460 scrollTo(params: { scrollLeft?: number; scrollTop?: number }): void;
461 /**
462 * Scroll to the specified item.
463 *
464 * By default, the Grid will scroll as little as possible to ensure the item is visible.
465 * You can control the alignment of the item though by specifying an `align` property. Acceptable values are:
466 *
467 * - auto (default) - Scroll as little as possible to ensure the item is visible. (If the item is already visible, it won't scroll at all.)
468 * - smart
469 * - If the item is already visible, don't scroll at all.
470 * - If it is less than one viewport away, scroll as little as possible so that it becomes visible.
471 * - If it is more than one viewport away, scroll so that it is centered within the grid.
472 * - center - Center align the item within the grid.
473 * - end - Align the item to the bottom, right hand side of the grid.
474 * - start - Align the item to the top, left hand of the grid.
475 *
476 * If either `columnIndex` or `rowIndex` are omitted, `scrollLeft` or `scrollTop` will be unchanged (respectively).
477 */
478 scrollToItem(params: {
479 align?: Align | undefined;
480 columnIndex?: number | undefined;
481 rowIndex?: number | undefined;
482 }): void;
483 /**
484 * VariableSizeGrid caches offsets and measurements for each column index for performance purposes.
485 * This method clears that cached data for all columns after (and including) the specified index.
486 * It should be called whenever a column's width changes. (Note that this is not a typical occurrence.)
487 *
488 * By default the grid will automatically re-render after the index is reset.
489 * If you would like to delay this re-render until e.g. a state update has completed in the parent component,
490 * specify a value of false for the second, optional parameter.
491 */
492 resetAfterColumnIndex(index: number, shouldForceUpdate?: boolean): void;
493 /**
494 * VariableSizeGrid caches offsets and measurements for each item for performance purposes.
495 * This method clears that cached data for all items after (and including) the specified indices.
496 * It should be called whenever an items size changes. (Note that this is not a typical occurrence.)
497 *
498 * By default the grid will automatically re-render after the index is reset.
499 * If you would like to delay this re-render until e.g. a state update has completed in the parent component,
500 * specify a value of false for the optional shouldForceUpdate parameter.
501 */
502 resetAfterIndices(params: {
503 columnIndex: number;
504 rowIndex: number;
505 shouldForceUpdate?: boolean | undefined;
506 }): void;
507 /**
508 * VariableSizeGrid caches offsets and measurements for each row index for performance purposes.
509 * This method clears that cached data for all rows after (and including) the specified index.
510 * It should be called whenever a row's height changes. (Note that this is not a typical occurrence.)
511 *
512 * By default the grid will automatically re-render after the index is reset.
513 * If you would like to delay this re-render until e.g. a state update has completed in the parent component,
514 * specify a value of false for the second, optional parameter.
515 */
516 resetAfterRowIndex(index: number, shouldForceUpdate?: boolean): void;
517}
518
519/**
520 * Custom comparison function for React.memo().
521 * It knows to compare individual style props and ignore the wrapper object.
522 *
523 * @see https://reactjs.org/docs/react-api.html#reactmemo
524 */
525export function areEqual(
526 prevProps: Readonly<object>,
527 nextProps: Readonly<object>
528): boolean;
529
530/**
531 * Custom shouldComponentUpdate for class components.
532 * It knows to compare individual style props and ignore the wrapper object.
533 *
534 * @see https://reactjs.org/docs/react-component.html#shouldcomponentupdate
535 */
536export function shouldComponentUpdate<P = {}, S = {}>(
537 this: { props: P; state: S },
538 nextProps: Readonly<P>,
539 nextState: Readonly<S>
540): boolean;