@react-native-tvos/virtualized-lists 0.73.4-0
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.
- package/Interaction/Batchinator.js +76 -0
- package/Lists/CellRenderMask.js +155 -0
- package/Lists/ChildListCollection.js +72 -0
- package/Lists/FillRateHelper.js +245 -0
- package/Lists/ListMetricsAggregator.js +303 -0
- package/Lists/StateSafePureComponent.js +85 -0
- package/Lists/ViewabilityHelper.js +348 -0
- package/Lists/VirtualizeUtils.js +245 -0
- package/Lists/VirtualizedList.d.ts +393 -0
- package/Lists/VirtualizedList.js +2019 -0
- package/Lists/VirtualizedListCellRenderer.js +245 -0
- package/Lists/VirtualizedListContext.js +114 -0
- package/Lists/VirtualizedListProps.js +337 -0
- package/Lists/VirtualizedSectionList.js +617 -0
- package/README.md +23 -0
- package/Utilities/clamp.js +23 -0
- package/Utilities/infoLog.js +20 -0
- package/index.d.ts +10 -0
- package/index.js +58 -0
- package/package.json +32 -0
|
@@ -0,0 +1,617 @@
|
|
|
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 {ViewToken} from './ViewabilityHelper';
|
|
12
|
+
|
|
13
|
+
import {View} from 'react-native';
|
|
14
|
+
import VirtualizedList from './VirtualizedList';
|
|
15
|
+
import {keyExtractor as defaultKeyExtractor} from './VirtualizeUtils';
|
|
16
|
+
import invariant from 'invariant';
|
|
17
|
+
import * as React from 'react';
|
|
18
|
+
|
|
19
|
+
type Item = any;
|
|
20
|
+
|
|
21
|
+
export type SectionBase<SectionItemT> = {
|
|
22
|
+
/**
|
|
23
|
+
* The data for rendering items in this section.
|
|
24
|
+
*/
|
|
25
|
+
data: $ReadOnlyArray<SectionItemT>,
|
|
26
|
+
/**
|
|
27
|
+
* Optional key to keep track of section re-ordering. If you don't plan on re-ordering sections,
|
|
28
|
+
* the array index will be used by default.
|
|
29
|
+
*/
|
|
30
|
+
key?: string,
|
|
31
|
+
// Optional props will override list-wide props just for this section.
|
|
32
|
+
renderItem?: ?(info: {
|
|
33
|
+
item: SectionItemT,
|
|
34
|
+
index: number,
|
|
35
|
+
section: SectionBase<SectionItemT>,
|
|
36
|
+
separators: {
|
|
37
|
+
highlight: () => void,
|
|
38
|
+
unhighlight: () => void,
|
|
39
|
+
updateProps: (select: 'leading' | 'trailing', newProps: Object) => void,
|
|
40
|
+
...
|
|
41
|
+
},
|
|
42
|
+
...
|
|
43
|
+
}) => null | React.Element<any>,
|
|
44
|
+
ItemSeparatorComponent?: ?React.ComponentType<any>,
|
|
45
|
+
keyExtractor?: (item: SectionItemT, index?: ?number) => string,
|
|
46
|
+
...
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
type RequiredProps<SectionT: SectionBase<any>> = {|
|
|
50
|
+
sections: $ReadOnlyArray<SectionT>,
|
|
51
|
+
|};
|
|
52
|
+
|
|
53
|
+
type OptionalProps<SectionT: SectionBase<any>> = {|
|
|
54
|
+
/**
|
|
55
|
+
* Default renderer for every item in every section.
|
|
56
|
+
*/
|
|
57
|
+
renderItem?: (info: {
|
|
58
|
+
item: Item,
|
|
59
|
+
index: number,
|
|
60
|
+
section: SectionT,
|
|
61
|
+
separators: {
|
|
62
|
+
highlight: () => void,
|
|
63
|
+
unhighlight: () => void,
|
|
64
|
+
updateProps: (select: 'leading' | 'trailing', newProps: Object) => void,
|
|
65
|
+
...
|
|
66
|
+
},
|
|
67
|
+
...
|
|
68
|
+
}) => null | React.Element<any>,
|
|
69
|
+
/**
|
|
70
|
+
* Rendered at the top of each section. These stick to the top of the `ScrollView` by default on
|
|
71
|
+
* iOS. See `stickySectionHeadersEnabled`.
|
|
72
|
+
*/
|
|
73
|
+
renderSectionHeader?: ?(info: {
|
|
74
|
+
section: SectionT,
|
|
75
|
+
...
|
|
76
|
+
}) => null | React.Element<any>,
|
|
77
|
+
/**
|
|
78
|
+
* Rendered at the bottom of each section.
|
|
79
|
+
*/
|
|
80
|
+
renderSectionFooter?: ?(info: {
|
|
81
|
+
section: SectionT,
|
|
82
|
+
...
|
|
83
|
+
}) => null | React.Element<any>,
|
|
84
|
+
/**
|
|
85
|
+
* Rendered at the top and bottom of each section (note this is different from
|
|
86
|
+
* `ItemSeparatorComponent` which is only rendered between items). These are intended to separate
|
|
87
|
+
* sections from the headers above and below and typically have the same highlight response as
|
|
88
|
+
* `ItemSeparatorComponent`. Also receives `highlighted`, `[leading/trailing][Item/Separator]`,
|
|
89
|
+
* and any custom props from `separators.updateProps`.
|
|
90
|
+
*/
|
|
91
|
+
SectionSeparatorComponent?: ?React.ComponentType<any>,
|
|
92
|
+
/**
|
|
93
|
+
* Makes section headers stick to the top of the screen until the next one pushes it off. Only
|
|
94
|
+
* enabled by default on iOS because that is the platform standard there.
|
|
95
|
+
*/
|
|
96
|
+
stickySectionHeadersEnabled?: boolean,
|
|
97
|
+
onEndReached?: ?({distanceFromEnd: number, ...}) => void,
|
|
98
|
+
|};
|
|
99
|
+
|
|
100
|
+
type VirtualizedListProps = React.ElementConfig<typeof VirtualizedList>;
|
|
101
|
+
|
|
102
|
+
export type Props<SectionT> = {|
|
|
103
|
+
...RequiredProps<SectionT>,
|
|
104
|
+
...OptionalProps<SectionT>,
|
|
105
|
+
...$Diff<
|
|
106
|
+
VirtualizedListProps,
|
|
107
|
+
{
|
|
108
|
+
renderItem: $PropertyType<VirtualizedListProps, 'renderItem'>,
|
|
109
|
+
data: $PropertyType<VirtualizedListProps, 'data'>,
|
|
110
|
+
...
|
|
111
|
+
},
|
|
112
|
+
>,
|
|
113
|
+
|};
|
|
114
|
+
export type ScrollToLocationParamsType = {|
|
|
115
|
+
animated?: ?boolean,
|
|
116
|
+
itemIndex: number,
|
|
117
|
+
sectionIndex: number,
|
|
118
|
+
viewOffset?: number,
|
|
119
|
+
viewPosition?: number,
|
|
120
|
+
|};
|
|
121
|
+
|
|
122
|
+
type State = {childProps: VirtualizedListProps, ...};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Right now this just flattens everything into one list and uses VirtualizedList under the
|
|
126
|
+
* hood. The only operation that might not scale well is concatting the data arrays of all the
|
|
127
|
+
* sections when new props are received, which should be plenty fast for up to ~10,000 items.
|
|
128
|
+
*/
|
|
129
|
+
class VirtualizedSectionList<
|
|
130
|
+
SectionT: SectionBase<any>,
|
|
131
|
+
> extends React.PureComponent<Props<SectionT>, State> {
|
|
132
|
+
scrollToLocation(params: ScrollToLocationParamsType) {
|
|
133
|
+
let index = params.itemIndex;
|
|
134
|
+
for (let i = 0; i < params.sectionIndex; i++) {
|
|
135
|
+
index += this.props.getItemCount(this.props.sections[i].data) + 2;
|
|
136
|
+
}
|
|
137
|
+
let viewOffset = params.viewOffset || 0;
|
|
138
|
+
if (this._listRef == null) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const listRef = this._listRef;
|
|
142
|
+
if (params.itemIndex > 0 && this.props.stickySectionHeadersEnabled) {
|
|
143
|
+
const frame = listRef
|
|
144
|
+
.__getListMetrics()
|
|
145
|
+
.getCellMetricsApprox(index - params.itemIndex, listRef.props);
|
|
146
|
+
viewOffset += frame.length;
|
|
147
|
+
}
|
|
148
|
+
const toIndexParams = {
|
|
149
|
+
...params,
|
|
150
|
+
viewOffset,
|
|
151
|
+
index,
|
|
152
|
+
};
|
|
153
|
+
// $FlowFixMe[incompatible-use]
|
|
154
|
+
this._listRef.scrollToIndex(toIndexParams);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
getListRef(): ?React.ElementRef<typeof VirtualizedList> {
|
|
158
|
+
return this._listRef;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
render(): React.Node {
|
|
162
|
+
const {
|
|
163
|
+
ItemSeparatorComponent, // don't pass through, rendered with renderItem
|
|
164
|
+
SectionSeparatorComponent,
|
|
165
|
+
renderItem: _renderItem,
|
|
166
|
+
renderSectionFooter,
|
|
167
|
+
renderSectionHeader,
|
|
168
|
+
sections: _sections,
|
|
169
|
+
stickySectionHeadersEnabled,
|
|
170
|
+
...passThroughProps
|
|
171
|
+
} = this.props;
|
|
172
|
+
|
|
173
|
+
const listHeaderOffset = this.props.ListHeaderComponent ? 1 : 0;
|
|
174
|
+
|
|
175
|
+
const stickyHeaderIndices = this.props.stickySectionHeadersEnabled
|
|
176
|
+
? ([]: Array<number>)
|
|
177
|
+
: undefined;
|
|
178
|
+
|
|
179
|
+
let itemCount = 0;
|
|
180
|
+
for (const section of this.props.sections) {
|
|
181
|
+
// Track the section header indices
|
|
182
|
+
if (stickyHeaderIndices != null) {
|
|
183
|
+
stickyHeaderIndices.push(itemCount + listHeaderOffset);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Add two for the section header and footer.
|
|
187
|
+
itemCount += 2;
|
|
188
|
+
itemCount += this.props.getItemCount(section.data);
|
|
189
|
+
}
|
|
190
|
+
const renderItem = this._renderItem(itemCount);
|
|
191
|
+
|
|
192
|
+
return (
|
|
193
|
+
<VirtualizedList
|
|
194
|
+
{...passThroughProps}
|
|
195
|
+
keyExtractor={this._keyExtractor}
|
|
196
|
+
stickyHeaderIndices={stickyHeaderIndices}
|
|
197
|
+
renderItem={renderItem}
|
|
198
|
+
data={this.props.sections}
|
|
199
|
+
getItem={(sections, index) =>
|
|
200
|
+
this._getItem(this.props, sections, index)
|
|
201
|
+
}
|
|
202
|
+
getItemCount={() => itemCount}
|
|
203
|
+
onViewableItemsChanged={
|
|
204
|
+
this.props.onViewableItemsChanged
|
|
205
|
+
? this._onViewableItemsChanged
|
|
206
|
+
: undefined
|
|
207
|
+
}
|
|
208
|
+
ref={this._captureRef}
|
|
209
|
+
/>
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
_getItem(
|
|
214
|
+
props: Props<SectionT>,
|
|
215
|
+
sections: ?$ReadOnlyArray<Item>,
|
|
216
|
+
index: number,
|
|
217
|
+
): ?Item {
|
|
218
|
+
if (!sections) {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
let itemIdx = index - 1;
|
|
222
|
+
for (let i = 0; i < sections.length; i++) {
|
|
223
|
+
const section = sections[i];
|
|
224
|
+
const sectionData = section.data;
|
|
225
|
+
const itemCount = props.getItemCount(sectionData);
|
|
226
|
+
if (itemIdx === -1 || itemIdx === itemCount) {
|
|
227
|
+
// We intend for there to be overflow by one on both ends of the list.
|
|
228
|
+
// This will be for headers and footers. When returning a header or footer
|
|
229
|
+
// item the section itself is the item.
|
|
230
|
+
return section;
|
|
231
|
+
} else if (itemIdx < itemCount) {
|
|
232
|
+
// If we are in the bounds of the list's data then return the item.
|
|
233
|
+
return props.getItem(sectionData, itemIdx);
|
|
234
|
+
} else {
|
|
235
|
+
itemIdx -= itemCount + 2; // Add two for the header and footer
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// $FlowFixMe[missing-local-annot]
|
|
242
|
+
_keyExtractor = (item: Item, index: number) => {
|
|
243
|
+
const info = this._subExtractor(index);
|
|
244
|
+
return (info && info.key) || String(index);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
_subExtractor(index: number): ?{
|
|
248
|
+
section: SectionT,
|
|
249
|
+
// Key of the section or combined key for section + item
|
|
250
|
+
key: string,
|
|
251
|
+
// Relative index within the section
|
|
252
|
+
index: ?number,
|
|
253
|
+
// True if this is the section header
|
|
254
|
+
header?: ?boolean,
|
|
255
|
+
leadingItem?: ?Item,
|
|
256
|
+
leadingSection?: ?SectionT,
|
|
257
|
+
trailingItem?: ?Item,
|
|
258
|
+
trailingSection?: ?SectionT,
|
|
259
|
+
...
|
|
260
|
+
} {
|
|
261
|
+
let itemIndex = index;
|
|
262
|
+
const {getItem, getItemCount, keyExtractor, sections} = this.props;
|
|
263
|
+
for (let i = 0; i < sections.length; i++) {
|
|
264
|
+
const section = sections[i];
|
|
265
|
+
const sectionData = section.data;
|
|
266
|
+
const key = section.key || String(i);
|
|
267
|
+
itemIndex -= 1; // The section adds an item for the header
|
|
268
|
+
if (itemIndex >= getItemCount(sectionData) + 1) {
|
|
269
|
+
itemIndex -= getItemCount(sectionData) + 1; // The section adds an item for the footer.
|
|
270
|
+
} else if (itemIndex === -1) {
|
|
271
|
+
return {
|
|
272
|
+
section,
|
|
273
|
+
key: key + ':header',
|
|
274
|
+
index: null,
|
|
275
|
+
header: true,
|
|
276
|
+
trailingSection: sections[i + 1],
|
|
277
|
+
};
|
|
278
|
+
} else if (itemIndex === getItemCount(sectionData)) {
|
|
279
|
+
return {
|
|
280
|
+
section,
|
|
281
|
+
key: key + ':footer',
|
|
282
|
+
index: null,
|
|
283
|
+
header: false,
|
|
284
|
+
trailingSection: sections[i + 1],
|
|
285
|
+
};
|
|
286
|
+
} else {
|
|
287
|
+
const extractor =
|
|
288
|
+
section.keyExtractor || keyExtractor || defaultKeyExtractor;
|
|
289
|
+
return {
|
|
290
|
+
section,
|
|
291
|
+
key:
|
|
292
|
+
key + ':' + extractor(getItem(sectionData, itemIndex), itemIndex),
|
|
293
|
+
index: itemIndex,
|
|
294
|
+
leadingItem: getItem(sectionData, itemIndex - 1),
|
|
295
|
+
leadingSection: sections[i - 1],
|
|
296
|
+
trailingItem: getItem(sectionData, itemIndex + 1),
|
|
297
|
+
trailingSection: sections[i + 1],
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
_convertViewable = (viewable: ViewToken): ?ViewToken => {
|
|
304
|
+
invariant(viewable.index != null, 'Received a broken ViewToken');
|
|
305
|
+
const info = this._subExtractor(viewable.index);
|
|
306
|
+
if (!info) {
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
const keyExtractorWithNullableIndex = info.section.keyExtractor;
|
|
310
|
+
const keyExtractorWithNonNullableIndex =
|
|
311
|
+
this.props.keyExtractor || defaultKeyExtractor;
|
|
312
|
+
const key =
|
|
313
|
+
keyExtractorWithNullableIndex != null
|
|
314
|
+
? keyExtractorWithNullableIndex(viewable.item, info.index)
|
|
315
|
+
: keyExtractorWithNonNullableIndex(viewable.item, info.index ?? 0);
|
|
316
|
+
|
|
317
|
+
return {
|
|
318
|
+
...viewable,
|
|
319
|
+
index: info.index,
|
|
320
|
+
key,
|
|
321
|
+
section: info.section,
|
|
322
|
+
};
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
_onViewableItemsChanged = ({
|
|
326
|
+
viewableItems,
|
|
327
|
+
changed,
|
|
328
|
+
}: {
|
|
329
|
+
viewableItems: Array<ViewToken>,
|
|
330
|
+
changed: Array<ViewToken>,
|
|
331
|
+
...
|
|
332
|
+
}) => {
|
|
333
|
+
const onViewableItemsChanged = this.props.onViewableItemsChanged;
|
|
334
|
+
if (onViewableItemsChanged != null) {
|
|
335
|
+
onViewableItemsChanged({
|
|
336
|
+
viewableItems: viewableItems
|
|
337
|
+
.map(this._convertViewable, this)
|
|
338
|
+
.filter(Boolean),
|
|
339
|
+
changed: changed.map(this._convertViewable, this).filter(Boolean),
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
_renderItem =
|
|
345
|
+
(listItemCount: number): $FlowFixMe =>
|
|
346
|
+
// eslint-disable-next-line react/no-unstable-nested-components
|
|
347
|
+
({item, index}: {item: Item, index: number, ...}) => {
|
|
348
|
+
const info = this._subExtractor(index);
|
|
349
|
+
if (!info) {
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
const infoIndex = info.index;
|
|
353
|
+
if (infoIndex == null) {
|
|
354
|
+
const {section} = info;
|
|
355
|
+
if (info.header === true) {
|
|
356
|
+
const {renderSectionHeader} = this.props;
|
|
357
|
+
return renderSectionHeader ? renderSectionHeader({section}) : null;
|
|
358
|
+
} else {
|
|
359
|
+
const {renderSectionFooter} = this.props;
|
|
360
|
+
return renderSectionFooter ? renderSectionFooter({section}) : null;
|
|
361
|
+
}
|
|
362
|
+
} else {
|
|
363
|
+
const renderItem = info.section.renderItem || this.props.renderItem;
|
|
364
|
+
const SeparatorComponent = this._getSeparatorComponent(
|
|
365
|
+
index,
|
|
366
|
+
info,
|
|
367
|
+
listItemCount,
|
|
368
|
+
);
|
|
369
|
+
invariant(renderItem, 'no renderItem!');
|
|
370
|
+
return (
|
|
371
|
+
<ItemWithSeparator
|
|
372
|
+
SeparatorComponent={SeparatorComponent}
|
|
373
|
+
LeadingSeparatorComponent={
|
|
374
|
+
infoIndex === 0 ? this.props.SectionSeparatorComponent : undefined
|
|
375
|
+
}
|
|
376
|
+
cellKey={info.key}
|
|
377
|
+
index={infoIndex}
|
|
378
|
+
item={item}
|
|
379
|
+
leadingItem={info.leadingItem}
|
|
380
|
+
leadingSection={info.leadingSection}
|
|
381
|
+
prevCellKey={(this._subExtractor(index - 1) || {}).key}
|
|
382
|
+
// Callback to provide updateHighlight for this item
|
|
383
|
+
setSelfHighlightCallback={this._setUpdateHighlightFor}
|
|
384
|
+
setSelfUpdatePropsCallback={this._setUpdatePropsFor}
|
|
385
|
+
// Provide child ability to set highlight/updateProps for previous item using prevCellKey
|
|
386
|
+
updateHighlightFor={this._updateHighlightFor}
|
|
387
|
+
updatePropsFor={this._updatePropsFor}
|
|
388
|
+
renderItem={renderItem}
|
|
389
|
+
section={info.section}
|
|
390
|
+
trailingItem={info.trailingItem}
|
|
391
|
+
trailingSection={info.trailingSection}
|
|
392
|
+
inverted={!!this.props.inverted}
|
|
393
|
+
/>
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
_updatePropsFor = (cellKey: string, value: any) => {
|
|
399
|
+
const updateProps = this._updatePropsMap[cellKey];
|
|
400
|
+
if (updateProps != null) {
|
|
401
|
+
updateProps(value);
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
_updateHighlightFor = (cellKey: string, value: boolean) => {
|
|
406
|
+
const updateHighlight = this._updateHighlightMap[cellKey];
|
|
407
|
+
if (updateHighlight != null) {
|
|
408
|
+
updateHighlight(value);
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
_setUpdateHighlightFor = (
|
|
413
|
+
cellKey: string,
|
|
414
|
+
updateHighlightFn: ?(boolean) => void,
|
|
415
|
+
) => {
|
|
416
|
+
if (updateHighlightFn != null) {
|
|
417
|
+
this._updateHighlightMap[cellKey] = updateHighlightFn;
|
|
418
|
+
} else {
|
|
419
|
+
// $FlowFixMe[prop-missing]
|
|
420
|
+
delete this._updateHighlightFor[cellKey];
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
_setUpdatePropsFor = (cellKey: string, updatePropsFn: ?(boolean) => void) => {
|
|
425
|
+
if (updatePropsFn != null) {
|
|
426
|
+
this._updatePropsMap[cellKey] = updatePropsFn;
|
|
427
|
+
} else {
|
|
428
|
+
delete this._updatePropsMap[cellKey];
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
_getSeparatorComponent(
|
|
433
|
+
index: number,
|
|
434
|
+
info?: ?Object,
|
|
435
|
+
listItemCount: number,
|
|
436
|
+
): ?React.ComponentType<any> {
|
|
437
|
+
info = info || this._subExtractor(index);
|
|
438
|
+
if (!info) {
|
|
439
|
+
return null;
|
|
440
|
+
}
|
|
441
|
+
const ItemSeparatorComponent =
|
|
442
|
+
info.section.ItemSeparatorComponent || this.props.ItemSeparatorComponent;
|
|
443
|
+
const {SectionSeparatorComponent} = this.props;
|
|
444
|
+
const isLastItemInList = index === listItemCount - 1;
|
|
445
|
+
const isLastItemInSection =
|
|
446
|
+
info.index === this.props.getItemCount(info.section.data) - 1;
|
|
447
|
+
if (SectionSeparatorComponent && isLastItemInSection) {
|
|
448
|
+
return SectionSeparatorComponent;
|
|
449
|
+
}
|
|
450
|
+
if (ItemSeparatorComponent && !isLastItemInSection && !isLastItemInList) {
|
|
451
|
+
return ItemSeparatorComponent;
|
|
452
|
+
}
|
|
453
|
+
return null;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
_updateHighlightMap: {[string]: (boolean) => void} = {};
|
|
457
|
+
_updatePropsMap: {[string]: void | (boolean => void)} = {};
|
|
458
|
+
_listRef: ?React.ElementRef<typeof VirtualizedList>;
|
|
459
|
+
_captureRef = (ref: null | React$ElementRef<Class<VirtualizedList>>) => {
|
|
460
|
+
this._listRef = ref;
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
type ItemWithSeparatorCommonProps = $ReadOnly<{|
|
|
465
|
+
leadingItem: ?Item,
|
|
466
|
+
leadingSection: ?Object,
|
|
467
|
+
section: Object,
|
|
468
|
+
trailingItem: ?Item,
|
|
469
|
+
trailingSection: ?Object,
|
|
470
|
+
|}>;
|
|
471
|
+
|
|
472
|
+
type ItemWithSeparatorProps = $ReadOnly<{|
|
|
473
|
+
...ItemWithSeparatorCommonProps,
|
|
474
|
+
LeadingSeparatorComponent: ?React.ComponentType<any>,
|
|
475
|
+
SeparatorComponent: ?React.ComponentType<any>,
|
|
476
|
+
cellKey: string,
|
|
477
|
+
index: number,
|
|
478
|
+
item: Item,
|
|
479
|
+
setSelfHighlightCallback: (
|
|
480
|
+
cellKey: string,
|
|
481
|
+
updateFn: ?(boolean) => void,
|
|
482
|
+
) => void,
|
|
483
|
+
setSelfUpdatePropsCallback: (
|
|
484
|
+
cellKey: string,
|
|
485
|
+
updateFn: ?(boolean) => void,
|
|
486
|
+
) => void,
|
|
487
|
+
prevCellKey?: ?string,
|
|
488
|
+
updateHighlightFor: (prevCellKey: string, value: boolean) => void,
|
|
489
|
+
updatePropsFor: (prevCellKey: string, value: Object) => void,
|
|
490
|
+
renderItem: Function,
|
|
491
|
+
inverted: boolean,
|
|
492
|
+
|}>;
|
|
493
|
+
|
|
494
|
+
function ItemWithSeparator(props: ItemWithSeparatorProps): React.Node {
|
|
495
|
+
const {
|
|
496
|
+
LeadingSeparatorComponent,
|
|
497
|
+
// this is the trailing separator and is associated with this item
|
|
498
|
+
SeparatorComponent,
|
|
499
|
+
cellKey,
|
|
500
|
+
prevCellKey,
|
|
501
|
+
setSelfHighlightCallback,
|
|
502
|
+
updateHighlightFor,
|
|
503
|
+
setSelfUpdatePropsCallback,
|
|
504
|
+
updatePropsFor,
|
|
505
|
+
item,
|
|
506
|
+
index,
|
|
507
|
+
section,
|
|
508
|
+
inverted,
|
|
509
|
+
} = props;
|
|
510
|
+
|
|
511
|
+
const [leadingSeparatorHiglighted, setLeadingSeparatorHighlighted] =
|
|
512
|
+
React.useState(false);
|
|
513
|
+
|
|
514
|
+
const [separatorHighlighted, setSeparatorHighlighted] = React.useState(false);
|
|
515
|
+
|
|
516
|
+
const [leadingSeparatorProps, setLeadingSeparatorProps] = React.useState({
|
|
517
|
+
leadingItem: props.leadingItem,
|
|
518
|
+
leadingSection: props.leadingSection,
|
|
519
|
+
section: props.section,
|
|
520
|
+
trailingItem: props.item,
|
|
521
|
+
trailingSection: props.trailingSection,
|
|
522
|
+
});
|
|
523
|
+
const [separatorProps, setSeparatorProps] = React.useState({
|
|
524
|
+
leadingItem: props.item,
|
|
525
|
+
leadingSection: props.leadingSection,
|
|
526
|
+
section: props.section,
|
|
527
|
+
trailingItem: props.trailingItem,
|
|
528
|
+
trailingSection: props.trailingSection,
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
React.useEffect(() => {
|
|
532
|
+
setSelfHighlightCallback(cellKey, setSeparatorHighlighted);
|
|
533
|
+
// $FlowFixMe[incompatible-call]
|
|
534
|
+
setSelfUpdatePropsCallback(cellKey, setSeparatorProps);
|
|
535
|
+
|
|
536
|
+
return () => {
|
|
537
|
+
setSelfUpdatePropsCallback(cellKey, null);
|
|
538
|
+
setSelfHighlightCallback(cellKey, null);
|
|
539
|
+
};
|
|
540
|
+
}, [
|
|
541
|
+
cellKey,
|
|
542
|
+
setSelfHighlightCallback,
|
|
543
|
+
setSeparatorProps,
|
|
544
|
+
setSelfUpdatePropsCallback,
|
|
545
|
+
]);
|
|
546
|
+
|
|
547
|
+
const separators = {
|
|
548
|
+
highlight: () => {
|
|
549
|
+
setLeadingSeparatorHighlighted(true);
|
|
550
|
+
setSeparatorHighlighted(true);
|
|
551
|
+
if (prevCellKey != null) {
|
|
552
|
+
updateHighlightFor(prevCellKey, true);
|
|
553
|
+
}
|
|
554
|
+
},
|
|
555
|
+
unhighlight: () => {
|
|
556
|
+
setLeadingSeparatorHighlighted(false);
|
|
557
|
+
setSeparatorHighlighted(false);
|
|
558
|
+
if (prevCellKey != null) {
|
|
559
|
+
updateHighlightFor(prevCellKey, false);
|
|
560
|
+
}
|
|
561
|
+
},
|
|
562
|
+
updateProps: (
|
|
563
|
+
select: 'leading' | 'trailing',
|
|
564
|
+
newProps: Partial<ItemWithSeparatorCommonProps>,
|
|
565
|
+
) => {
|
|
566
|
+
if (select === 'leading') {
|
|
567
|
+
if (LeadingSeparatorComponent != null) {
|
|
568
|
+
setLeadingSeparatorProps({...leadingSeparatorProps, ...newProps});
|
|
569
|
+
} else if (prevCellKey != null) {
|
|
570
|
+
// update the previous item's separator
|
|
571
|
+
updatePropsFor(prevCellKey, {...leadingSeparatorProps, ...newProps});
|
|
572
|
+
}
|
|
573
|
+
} else if (select === 'trailing' && SeparatorComponent != null) {
|
|
574
|
+
setSeparatorProps({...separatorProps, ...newProps});
|
|
575
|
+
}
|
|
576
|
+
},
|
|
577
|
+
};
|
|
578
|
+
const element = props.renderItem({
|
|
579
|
+
item,
|
|
580
|
+
index,
|
|
581
|
+
section,
|
|
582
|
+
separators,
|
|
583
|
+
});
|
|
584
|
+
const leadingSeparator = LeadingSeparatorComponent != null && (
|
|
585
|
+
<LeadingSeparatorComponent
|
|
586
|
+
highlighted={leadingSeparatorHiglighted}
|
|
587
|
+
{...leadingSeparatorProps}
|
|
588
|
+
/>
|
|
589
|
+
);
|
|
590
|
+
const separator = SeparatorComponent != null && (
|
|
591
|
+
<SeparatorComponent
|
|
592
|
+
highlighted={separatorHighlighted}
|
|
593
|
+
{...separatorProps}
|
|
594
|
+
/>
|
|
595
|
+
);
|
|
596
|
+
return leadingSeparator || separator ? (
|
|
597
|
+
<View>
|
|
598
|
+
{inverted === false ? leadingSeparator : separator}
|
|
599
|
+
{element}
|
|
600
|
+
{inverted === false ? separator : leadingSeparator}
|
|
601
|
+
</View>
|
|
602
|
+
) : (
|
|
603
|
+
element
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/* $FlowFixMe[class-object-subtyping] added when improving typing for this
|
|
608
|
+
* parameters */
|
|
609
|
+
// $FlowFixMe[method-unbinding]
|
|
610
|
+
module.exports = (VirtualizedSectionList: React.AbstractComponent<
|
|
611
|
+
React.ElementConfig<typeof VirtualizedSectionList>,
|
|
612
|
+
$ReadOnly<{
|
|
613
|
+
getListRef: () => ?React.ElementRef<typeof VirtualizedList>,
|
|
614
|
+
scrollToLocation: (params: ScrollToLocationParamsType) => void,
|
|
615
|
+
...
|
|
616
|
+
}>,
|
|
617
|
+
>);
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @react-native-tvos/virtualized-lists
|
|
2
|
+
|
|
3
|
+
[![Version][version-badge]][package]
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
yarn add @react-native-tvos/virtualized-lists
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
*Note: We're using `yarn` to install deps. Feel free to change commands to use `npm` 3+ and `npx` if you like*
|
|
12
|
+
|
|
13
|
+
*Note: this package is modified from the original at `@react-native/virtualized-lists`, adding some specific changes needed for correct focus handling on TV platforms.*
|
|
14
|
+
|
|
15
|
+
[version-badge]: https://img.shields.io/npm/v/@react-native-tvos/virtualized-lists?style=flat-square
|
|
16
|
+
[package]: https://www.npmjs.com/package/@react-native-tvos/virtualized-lists
|
|
17
|
+
|
|
18
|
+
## Testing
|
|
19
|
+
|
|
20
|
+
To run the tests in this package, run the following commands from the React Native root folder:
|
|
21
|
+
|
|
22
|
+
1. `yarn` to install the dependencies. You just need to run this once
|
|
23
|
+
2. `yarn jest packages/virtualized-lists`.
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
* @format
|
|
8
|
+
* @flow strict
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
function clamp(min: number, value: number, max: number): number {
|
|
14
|
+
if (value < min) {
|
|
15
|
+
return min;
|
|
16
|
+
}
|
|
17
|
+
if (value > max) {
|
|
18
|
+
return max;
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = clamp;
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
* @format
|
|
8
|
+
* @flow strict
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Intentional info-level logging for clear separation from ad-hoc console debug logging.
|
|
15
|
+
*/
|
|
16
|
+
function infoLog(...args: Array<mixed>): void {
|
|
17
|
+
return console.log(...args);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = infoLog;
|
package/index.d.ts
ADDED