@react-native-macos/virtualized-lists 0.76.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,401 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow
8
+ * @format
9
+ */
10
+
11
+ import type {
12
+ ViewabilityConfig,
13
+ ViewabilityConfigCallbackPair,
14
+ ViewToken,
15
+ } from './ViewabilityHelper';
16
+ import type {ViewStyleProp} from 'react-native/Libraries/StyleSheet/StyleSheet';
17
+ import type {
18
+ FocusEvent,
19
+ LayoutEvent,
20
+ } from 'react-native/Libraries/Types/CoreEventTypes';
21
+
22
+ import * as React from 'react';
23
+ import {typeof ScrollView} from 'react-native';
24
+
25
+ export type Item = any;
26
+
27
+ export type Separators = {
28
+ highlight: () => void,
29
+ unhighlight: () => void,
30
+ updateProps: (select: 'leading' | 'trailing', newProps: Object) => void,
31
+ ...
32
+ };
33
+
34
+ export type RenderItemProps<ItemT> = {
35
+ item: ItemT,
36
+ index: number,
37
+ isSelected: ?boolean, // [macOS]
38
+ separators: Separators,
39
+ ...
40
+ };
41
+
42
+ export type CellRendererProps<ItemT> = $ReadOnly<{
43
+ cellKey: string,
44
+ children: React.Node,
45
+ index: number,
46
+ item: ItemT,
47
+ onFocusCapture?: (event: FocusEvent) => void,
48
+ onLayout?: (event: LayoutEvent) => void,
49
+ style: ViewStyleProp,
50
+ }>;
51
+
52
+ export type RenderItemType<ItemT> = (
53
+ info: RenderItemProps<ItemT>,
54
+ ) => React.Node;
55
+
56
+ // [macOS
57
+ export type SelectedRowIndexPathType = {
58
+ sectionIndex: number,
59
+ rowIndex: number,
60
+ }; // macOS]
61
+
62
+ type RequiredProps = {|
63
+ /**
64
+ * The default accessor functions assume this is an Array<{key: string} | {id: string}> but you can override
65
+ * getItem, getItemCount, and keyExtractor to handle any type of index-based data.
66
+ */
67
+ data?: any,
68
+ /**
69
+ * A generic accessor for extracting an item from any sort of data blob.
70
+ */
71
+ getItem: (data: any, index: number) => ?Item,
72
+ /**
73
+ * Determines how many items are in the data blob.
74
+ */
75
+ getItemCount: (data: any) => number,
76
+ |};
77
+ type OptionalProps = {|
78
+ renderItem?: ?RenderItemType<Item>,
79
+ /**
80
+ * `debug` will turn on extra logging and visual overlays to aid with debugging both usage and
81
+ * implementation, but with a significant perf hit.
82
+ */
83
+ debug?: ?boolean,
84
+ /**
85
+ * DEPRECATED: Virtualization provides significant performance and memory optimizations, but fully
86
+ * unmounts react instances that are outside of the render window. You should only need to disable
87
+ * this for debugging purposes. Defaults to false.
88
+ */
89
+ disableVirtualization?: ?boolean,
90
+ /**
91
+ * A marker property for telling the list to re-render (since it implements `PureComponent`). If
92
+ * any of your `renderItem`, Header, Footer, etc. functions depend on anything outside of the
93
+ * `data` prop, stick it here and treat it immutably.
94
+ */
95
+ extraData?: any,
96
+ // e.g. height, y
97
+ getItemLayout?: (
98
+ data: any,
99
+ index: number,
100
+ ) => {
101
+ length: number,
102
+ offset: number,
103
+ index: number,
104
+ ...
105
+ },
106
+ horizontal?: ?boolean,
107
+ /**
108
+ * How many items to render in the initial batch. This should be enough to fill the screen but not
109
+ * much more. Note these items will never be unmounted as part of the windowed rendering in order
110
+ * to improve perceived performance of scroll-to-top actions.
111
+ */
112
+ initialNumToRender?: ?number,
113
+ /**
114
+ * Instead of starting at the top with the first item, start at `initialScrollIndex`. This
115
+ * disables the "scroll to top" optimization that keeps the first `initialNumToRender` items
116
+ * always rendered and immediately renders the items starting at this initial index. Requires
117
+ * `getItemLayout` to be implemented.
118
+ */
119
+ initialScrollIndex?: ?number,
120
+ /**
121
+ * Reverses the direction of scroll. Uses scale transforms of -1.
122
+ */
123
+ inverted?: ?boolean,
124
+ keyExtractor?: ?(item: Item, index: number) => string,
125
+ /**
126
+ * CellRendererComponent allows customizing how cells rendered by
127
+ * `renderItem`/`ListItemComponent` are wrapped when placed into the
128
+ * underlying ScrollView. This component must accept event handlers which
129
+ * notify VirtualizedList of changes within the cell.
130
+ */
131
+ CellRendererComponent?: ?React.ComponentType<CellRendererProps<Item>>,
132
+ /**
133
+ * Rendered in between each item, but not at the top or bottom. By default, `highlighted` and
134
+ * `leadingItem` props are provided. `renderItem` provides `separators.highlight`/`unhighlight`
135
+ * which will update the `highlighted` prop, but you can also add custom props with
136
+ * `separators.updateProps`.
137
+ */
138
+ ItemSeparatorComponent?: ?React.ComponentType<any>,
139
+ /**
140
+ * Takes an item from `data` and renders it into the list. Example usage:
141
+ *
142
+ * <FlatList
143
+ * ItemSeparatorComponent={Platform.OS !== 'android' && ({highlighted}) => (
144
+ * <View style={[style.separator, highlighted && {marginLeft: 0}]} />
145
+ * )}
146
+ * data={[{title: 'Title Text', key: 'item1'}]}
147
+ * ListItemComponent={({item, separators}) => (
148
+ * <TouchableHighlight
149
+ * onPress={() => this._onPress(item)}
150
+ * onShowUnderlay={separators.highlight}
151
+ * onHideUnderlay={separators.unhighlight}>
152
+ * <View style={{backgroundColor: 'white'}}>
153
+ * <Text>{item.title}</Text>
154
+ * </View>
155
+ * </TouchableHighlight>
156
+ * )}
157
+ * />
158
+ *
159
+ * Provides additional metadata like `index` if you need it, as well as a more generic
160
+ * `separators.updateProps` function which let's you set whatever props you want to change the
161
+ * rendering of either the leading separator or trailing separator in case the more common
162
+ * `highlight` and `unhighlight` (which set the `highlighted: boolean` prop) are insufficient for
163
+ * your use-case.
164
+ */
165
+ ListItemComponent?: ?(
166
+ | React.ComponentType<any>
167
+ | ExactReactElement_DEPRECATED<any>
168
+ ),
169
+ /**
170
+ * Rendered when the list is empty. Can be a React Component Class, a render function, or
171
+ * a rendered element.
172
+ */
173
+ ListEmptyComponent?: ?(
174
+ | React.ComponentType<any>
175
+ | ExactReactElement_DEPRECATED<any>
176
+ ),
177
+ /**
178
+ * Rendered at the bottom of all the items. Can be a React Component Class, a render function, or
179
+ * a rendered element.
180
+ */
181
+ ListFooterComponent?: ?(
182
+ | React.ComponentType<any>
183
+ | ExactReactElement_DEPRECATED<any>
184
+ ),
185
+ /**
186
+ * Styling for internal View for ListFooterComponent
187
+ */
188
+ ListFooterComponentStyle?: ViewStyleProp,
189
+ /**
190
+ * Rendered at the top of all the items. Can be a React Component Class, a render function, or
191
+ * a rendered element.
192
+ */
193
+ ListHeaderComponent?: ?(
194
+ | React.ComponentType<any>
195
+ | ExactReactElement_DEPRECATED<any>
196
+ ),
197
+ /**
198
+ * Styling for internal View for ListHeaderComponent
199
+ */
200
+ ListHeaderComponentStyle?: ViewStyleProp,
201
+ /**
202
+ * The maximum number of items to render in each incremental render batch. The more rendered at
203
+ * once, the better the fill rate, but responsiveness may suffer because rendering content may
204
+ * interfere with responding to button taps or other interactions.
205
+ */
206
+ maxToRenderPerBatch?: ?number,
207
+ /**
208
+ * Called once when the scroll position gets within within `onEndReachedThreshold`
209
+ * from the logical end of the list.
210
+ */
211
+ onEndReached?: ?(info: {distanceFromEnd: number, ...}) => void,
212
+ /**
213
+ * How far from the end (in units of visible length of the list) the trailing edge of the
214
+ * list must be from the end of the content to trigger the `onEndReached` callback.
215
+ * Thus, a value of 0.5 will trigger `onEndReached` when the end of the content is
216
+ * within half the visible length of the list.
217
+ */
218
+ onEndReachedThreshold?: ?number,
219
+ /**
220
+ * If provided, a standard RefreshControl will be added for "Pull to Refresh" functionality. Make
221
+ * sure to also set the `refreshing` prop correctly.
222
+ */
223
+ onRefresh?: ?() => void,
224
+ /**
225
+ * Used to handle failures when scrolling to an index that has not been measured yet. Recommended
226
+ * action is to either compute your own offset and `scrollTo` it, or scroll as far as possible and
227
+ * then try again after more items have been rendered.
228
+ */
229
+ onScrollToIndexFailed?: ?(info: {
230
+ index: number,
231
+ highestMeasuredFrameIndex: number,
232
+ averageItemLength: number,
233
+ ...
234
+ }) => void,
235
+ /**
236
+ * Called once when the scroll position gets within within `onStartReachedThreshold`
237
+ * from the logical start of the list.
238
+ */
239
+ onStartReached?: ?(info: {distanceFromStart: number, ...}) => void,
240
+ /**
241
+ * How far from the start (in units of visible length of the list) the leading edge of the
242
+ * list must be from the start of the content to trigger the `onStartReached` callback.
243
+ * Thus, a value of 0.5 will trigger `onStartReached` when the start of the content is
244
+ * within half the visible length of the list.
245
+ */
246
+ onStartReachedThreshold?: ?number,
247
+ /**
248
+ * Called when the viewability of rows changes, as defined by the
249
+ * `viewabilityConfig` prop.
250
+ */
251
+ onViewableItemsChanged?: ?(info: {
252
+ viewableItems: Array<ViewToken>,
253
+ changed: Array<ViewToken>,
254
+ ...
255
+ }) => void,
256
+ persistentScrollbar?: ?boolean,
257
+ /**
258
+ * Set this when offset is needed for the loading indicator to show correctly.
259
+ */
260
+ progressViewOffset?: number,
261
+ /**
262
+ * A custom refresh control element. When set, it overrides the default
263
+ * <RefreshControl> component built internally. The onRefresh and refreshing
264
+ * props are also ignored. Only works for vertical VirtualizedList.
265
+ */
266
+ refreshControl?: ?ExactReactElement_DEPRECATED<any>,
267
+ /**
268
+ * Set this true while waiting for new data from a refresh.
269
+ */
270
+ refreshing?: ?boolean,
271
+ /**
272
+ * Note: may have bugs (missing content) in some circumstances - use at your own risk.
273
+ *
274
+ * This may improve scroll performance for large lists.
275
+ */
276
+ removeClippedSubviews?: boolean,
277
+ /**
278
+ * Render a custom scroll component, e.g. with a differently styled `RefreshControl`.
279
+ */
280
+ renderScrollComponent?: (props: Object) => ExactReactElement_DEPRECATED<any>,
281
+ /**
282
+ * Amount of time between low-pri item render batches, e.g. for rendering items quite a ways off
283
+ * screen. Similar fill rate/responsiveness tradeoff as `maxToRenderPerBatch`.
284
+ */
285
+ updateCellsBatchingPeriod?: ?number,
286
+ /**
287
+ * See `ViewabilityHelper` for flow type and further documentation.
288
+ */
289
+ viewabilityConfig?: ViewabilityConfig,
290
+ /**
291
+ * List of ViewabilityConfig/onViewableItemsChanged pairs. A specific onViewableItemsChanged
292
+ * will be called when its corresponding ViewabilityConfig's conditions are met.
293
+ */
294
+ viewabilityConfigCallbackPairs?: Array<ViewabilityConfigCallbackPair>,
295
+ /**
296
+ * Determines the maximum number of items rendered outside of the visible area, in units of
297
+ * visible lengths. So if your list fills the screen, then `windowSize={21}` (the default) will
298
+ * render the visible screen area plus up to 10 screens above and 10 below the viewport. Reducing
299
+ * this number will reduce memory consumption and may improve performance, but will increase the
300
+ * chance that fast scrolling may reveal momentary blank areas of unrendered content.
301
+ */
302
+ windowSize?: ?number,
303
+ /**
304
+ * The legacy implementation is no longer supported.
305
+ */
306
+ legacyImplementation?: empty,
307
+ |};
308
+ // [macOS
309
+ type MacOSProps = {|
310
+ /**
311
+ * Allows you to 'select' a row using arrow keys. The selected row will have the prop `isSelected`
312
+ * passed in as true to it's renderItem / ListItemComponent. You can also imperatively select a row
313
+ * using the `selectRowAtIndex` method. You can set the initially selected row using the
314
+ * `initialSelectedIndex` prop.
315
+ * Keyboard Behavior:
316
+ * - ArrowUp: Select row above current selected row
317
+ * - ArrowDown: Select row below current selected row
318
+ * - Option+ArrowUp: Select the first row
319
+ * - Opton+ArrowDown: Select the last 'realized' row
320
+ * - Home: Scroll to top of list
321
+ * - End: Scroll to end of list
322
+ *
323
+ * @platform macos
324
+ */
325
+ enableSelectionOnKeyPress?: ?boolean,
326
+ /**
327
+ * The initially selected row, if `enableSelectionOnKeyPress` is set.
328
+ */
329
+ initialSelectedIndex?: ?number,
330
+ /**
331
+ * If provided, will be invoked whenever the selection on the list changes. Make sure to set
332
+ * the property enableSelectionOnKeyPress to true to change selection via keyboard (macOS).
333
+ *
334
+ * @platform macos
335
+ */
336
+ onSelectionChanged?: ?(info: {
337
+ previousSelection: number,
338
+ newSelection: number,
339
+ item: ?Item,
340
+ }) => void,
341
+ /**
342
+ * If provided, called when 'Enter' key is pressed on an item.
343
+ *
344
+ * @platform macos
345
+ */
346
+ onSelectionEntered?: ?(item: ?Item) => void,
347
+
348
+ sectionIndex?: number,
349
+ rowIndex?: number,
350
+ |};
351
+ // macOS]
352
+
353
+ export type Props = {|
354
+ ...React.ElementConfig<ScrollView>,
355
+ ...RequiredProps,
356
+ ...OptionalProps,
357
+ ...MacOSProps, // [macOS]
358
+ |};
359
+
360
+ /**
361
+ * Default Props Helper Functions
362
+ * Use the following helper functions for default values
363
+ */
364
+
365
+ // horizontalOrDefault(this.props.horizontal)
366
+ export function horizontalOrDefault(horizontal: ?boolean): boolean {
367
+ return horizontal ?? false;
368
+ }
369
+
370
+ // initialNumToRenderOrDefault(this.props.initialNumToRender)
371
+ export function initialNumToRenderOrDefault(
372
+ initialNumToRender: ?number,
373
+ ): number {
374
+ return initialNumToRender ?? 10;
375
+ }
376
+
377
+ // maxToRenderPerBatchOrDefault(this.props.maxToRenderPerBatch)
378
+ export function maxToRenderPerBatchOrDefault(
379
+ maxToRenderPerBatch: ?number,
380
+ ): number {
381
+ return maxToRenderPerBatch ?? 10;
382
+ }
383
+
384
+ // onStartReachedThresholdOrDefault(this.props.onStartReachedThreshold)
385
+ export function onStartReachedThresholdOrDefault(
386
+ onStartReachedThreshold: ?number,
387
+ ): number {
388
+ return onStartReachedThreshold ?? 2;
389
+ }
390
+
391
+ // onEndReachedThresholdOrDefault(this.props.onEndReachedThreshold)
392
+ export function onEndReachedThresholdOrDefault(
393
+ onEndReachedThreshold: ?number,
394
+ ): number {
395
+ return onEndReachedThreshold ?? 2;
396
+ }
397
+
398
+ // windowSizeOrDefault(this.props.windowSize)
399
+ export function windowSizeOrDefault(windowSize: ?number): number {
400
+ return windowSize ?? 21;
401
+ }