@react-native-macos/virtualized-lists 0.78.4
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/CHANGELOG.json +20 -0
- package/CHANGELOG.md +13 -0
- package/Interaction/Batchinator.js +85 -0
- package/Lists/CellRenderMask.js +155 -0
- package/Lists/ChildListCollection.js +72 -0
- package/Lists/FillRateHelper.js +247 -0
- package/Lists/ListMetricsAggregator.js +327 -0
- package/Lists/StateSafePureComponent.js +85 -0
- package/Lists/ViewabilityHelper.js +349 -0
- package/Lists/VirtualizeUtils.js +257 -0
- package/Lists/VirtualizedList.d.ts +393 -0
- package/Lists/VirtualizedList.js +2192 -0
- package/Lists/VirtualizedListCellRenderer.js +256 -0
- package/Lists/VirtualizedListContext.js +114 -0
- package/Lists/VirtualizedListProps.js +401 -0
- package/Lists/VirtualizedSectionList.js +609 -0
- package/README.md +21 -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 +39 -0
|
@@ -0,0 +1,256 @@
|
|
|
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 {CellRendererProps, RenderItemType} from './VirtualizedListProps';
|
|
12
|
+
import type {ViewStyleProp} from 'react-native/Libraries/StyleSheet/StyleSheet';
|
|
13
|
+
import type {
|
|
14
|
+
FocusEvent,
|
|
15
|
+
LayoutEvent,
|
|
16
|
+
} from 'react-native/Libraries/Types/CoreEventTypes';
|
|
17
|
+
|
|
18
|
+
import {VirtualizedListCellContextProvider} from './VirtualizedListContext.js';
|
|
19
|
+
import invariant from 'invariant';
|
|
20
|
+
import * as React from 'react';
|
|
21
|
+
import {Platform, StyleSheet, View} from 'react-native'; // [macOS]
|
|
22
|
+
|
|
23
|
+
export type Props<ItemT> = {
|
|
24
|
+
CellRendererComponent?: ?React.ComponentType<CellRendererProps<ItemT>>,
|
|
25
|
+
ItemSeparatorComponent: ?React.ComponentType<
|
|
26
|
+
any | {highlighted: boolean, leadingItem: ?ItemT},
|
|
27
|
+
>,
|
|
28
|
+
ListItemComponent?: ?(React.ComponentType<any> | React.MixedElement),
|
|
29
|
+
cellKey: string,
|
|
30
|
+
horizontal: ?boolean,
|
|
31
|
+
index: number,
|
|
32
|
+
inversionStyle: ViewStyleProp,
|
|
33
|
+
isSelected: ?boolean, // [macOS]
|
|
34
|
+
item: ItemT,
|
|
35
|
+
onCellLayout?: (event: LayoutEvent, cellKey: string, index: number) => void,
|
|
36
|
+
onCellFocusCapture?: (cellKey: string) => void,
|
|
37
|
+
onUnmount: (cellKey: string) => void,
|
|
38
|
+
onUpdateSeparators: (
|
|
39
|
+
cellKeys: Array<?string>,
|
|
40
|
+
props: Partial<SeparatorProps<ItemT>>,
|
|
41
|
+
) => void,
|
|
42
|
+
prevCellKey: ?string,
|
|
43
|
+
renderItem?: ?RenderItemType<ItemT>,
|
|
44
|
+
...
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type SeparatorProps<ItemT> = $ReadOnly<{|
|
|
48
|
+
highlighted: boolean,
|
|
49
|
+
leadingItem: ?ItemT,
|
|
50
|
+
|}>;
|
|
51
|
+
|
|
52
|
+
type State<ItemT> = {
|
|
53
|
+
separatorProps: SeparatorProps<ItemT>,
|
|
54
|
+
...
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export default class CellRenderer<ItemT> extends React.PureComponent<
|
|
58
|
+
Props<ItemT>,
|
|
59
|
+
State<ItemT>,
|
|
60
|
+
> {
|
|
61
|
+
state: State<ItemT> = {
|
|
62
|
+
separatorProps: {
|
|
63
|
+
highlighted: false,
|
|
64
|
+
leadingItem: this.props.item,
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
static getDerivedStateFromProps(
|
|
69
|
+
props: Props<ItemT>,
|
|
70
|
+
prevState: State<ItemT>,
|
|
71
|
+
): ?State<ItemT> {
|
|
72
|
+
if (props.item !== prevState.separatorProps.leadingItem) {
|
|
73
|
+
return {
|
|
74
|
+
separatorProps: {
|
|
75
|
+
...prevState.separatorProps,
|
|
76
|
+
leadingItem: props.item,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// TODO: consider factoring separator stuff out of VirtualizedList into FlatList since it's not
|
|
84
|
+
// reused by SectionList and we can keep VirtualizedList simpler.
|
|
85
|
+
// $FlowFixMe[missing-local-annot]
|
|
86
|
+
_separators = {
|
|
87
|
+
highlight: () => {
|
|
88
|
+
const {cellKey, prevCellKey} = this.props;
|
|
89
|
+
this.props.onUpdateSeparators([cellKey, prevCellKey], {
|
|
90
|
+
highlighted: true,
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
unhighlight: () => {
|
|
94
|
+
const {cellKey, prevCellKey} = this.props;
|
|
95
|
+
this.props.onUpdateSeparators([cellKey, prevCellKey], {
|
|
96
|
+
highlighted: false,
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
updateProps: (
|
|
100
|
+
select: 'leading' | 'trailing',
|
|
101
|
+
newProps: SeparatorProps<ItemT>,
|
|
102
|
+
) => {
|
|
103
|
+
const {cellKey, prevCellKey} = this.props;
|
|
104
|
+
this.props.onUpdateSeparators(
|
|
105
|
+
[select === 'leading' ? prevCellKey : cellKey],
|
|
106
|
+
newProps,
|
|
107
|
+
);
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
updateSeparatorProps(newProps: SeparatorProps<ItemT>) {
|
|
112
|
+
this.setState(state => ({
|
|
113
|
+
separatorProps: {...state.separatorProps, ...newProps},
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
componentWillUnmount() {
|
|
118
|
+
this.props.onUnmount(this.props.cellKey);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
_onLayout = (nativeEvent: LayoutEvent): void => {
|
|
122
|
+
this.props.onCellLayout?.(
|
|
123
|
+
nativeEvent,
|
|
124
|
+
this.props.cellKey,
|
|
125
|
+
this.props.index,
|
|
126
|
+
);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
_onCellFocusCapture = (e: FocusEvent): void => {
|
|
130
|
+
this.props.onCellFocusCapture?.(this.props.cellKey);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
_renderElement(
|
|
134
|
+
renderItem: ?RenderItemType<ItemT>,
|
|
135
|
+
ListItemComponent: any,
|
|
136
|
+
item: ItemT,
|
|
137
|
+
index: number,
|
|
138
|
+
isSelected: ?boolean, // [macOS]
|
|
139
|
+
): React.Node {
|
|
140
|
+
if (renderItem && ListItemComponent) {
|
|
141
|
+
console.warn(
|
|
142
|
+
'VirtualizedList: Both ListItemComponent and renderItem props are present. ListItemComponent will take' +
|
|
143
|
+
' precedence over renderItem.',
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (ListItemComponent) {
|
|
148
|
+
return (
|
|
149
|
+
<ListItemComponent
|
|
150
|
+
item={item}
|
|
151
|
+
index={index}
|
|
152
|
+
separators={this._separators}
|
|
153
|
+
/>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (renderItem) {
|
|
158
|
+
return renderItem({
|
|
159
|
+
item,
|
|
160
|
+
index,
|
|
161
|
+
isSelected, // [macOS]
|
|
162
|
+
separators: this._separators,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
invariant(
|
|
167
|
+
false,
|
|
168
|
+
'VirtualizedList: Either ListItemComponent or renderItem props are required but none were found.',
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
render(): React.Node {
|
|
173
|
+
const {
|
|
174
|
+
CellRendererComponent,
|
|
175
|
+
ItemSeparatorComponent,
|
|
176
|
+
ListItemComponent,
|
|
177
|
+
cellKey,
|
|
178
|
+
horizontal,
|
|
179
|
+
item,
|
|
180
|
+
index,
|
|
181
|
+
inversionStyle,
|
|
182
|
+
isSelected, // [macOS]
|
|
183
|
+
onCellLayout,
|
|
184
|
+
renderItem,
|
|
185
|
+
} = this.props;
|
|
186
|
+
const element = this._renderElement(
|
|
187
|
+
renderItem,
|
|
188
|
+
ListItemComponent,
|
|
189
|
+
item,
|
|
190
|
+
index,
|
|
191
|
+
isSelected, // [macOS]
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
// NOTE: that when this is a sticky header, `onLayout` will get automatically extracted and
|
|
195
|
+
// called explicitly by `ScrollViewStickyHeader`.
|
|
196
|
+
const itemSeparator: React.Node = React.isValidElement(
|
|
197
|
+
ItemSeparatorComponent,
|
|
198
|
+
)
|
|
199
|
+
? // $FlowFixMe[incompatible-type]
|
|
200
|
+
ItemSeparatorComponent
|
|
201
|
+
: // $FlowFixMe[incompatible-type]
|
|
202
|
+
ItemSeparatorComponent && (
|
|
203
|
+
<ItemSeparatorComponent {...this.state.separatorProps} />
|
|
204
|
+
);
|
|
205
|
+
const cellStyle = inversionStyle
|
|
206
|
+
? horizontal
|
|
207
|
+
? [styles.rowReverse, inversionStyle]
|
|
208
|
+
: [styles.columnReverse, inversionStyle]
|
|
209
|
+
: horizontal
|
|
210
|
+
? [styles.row, inversionStyle]
|
|
211
|
+
: inversionStyle;
|
|
212
|
+
let result = !CellRendererComponent ? ( // [macOS]
|
|
213
|
+
<View
|
|
214
|
+
style={cellStyle}
|
|
215
|
+
onFocusCapture={this._onCellFocusCapture}
|
|
216
|
+
{...(onCellLayout && {onLayout: this._onLayout})}>
|
|
217
|
+
{element}
|
|
218
|
+
{itemSeparator}
|
|
219
|
+
</View>
|
|
220
|
+
) : (
|
|
221
|
+
<CellRendererComponent
|
|
222
|
+
cellKey={cellKey}
|
|
223
|
+
index={index}
|
|
224
|
+
item={item}
|
|
225
|
+
style={cellStyle}
|
|
226
|
+
onFocusCapture={this._onCellFocusCapture}
|
|
227
|
+
{...(onCellLayout && {onLayout: this._onLayout})}>
|
|
228
|
+
{element}
|
|
229
|
+
{itemSeparator}
|
|
230
|
+
</CellRendererComponent>
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
if (Platform.OS === 'macos') {
|
|
234
|
+
// [macOS
|
|
235
|
+
result = React.cloneElement(result, {collapsable: false});
|
|
236
|
+
} // macOS]
|
|
237
|
+
|
|
238
|
+
return (
|
|
239
|
+
<VirtualizedListCellContextProvider cellKey={this.props.cellKey}>
|
|
240
|
+
{result}
|
|
241
|
+
</VirtualizedListCellContextProvider>
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const styles = StyleSheet.create({
|
|
247
|
+
row: {
|
|
248
|
+
flexDirection: 'row',
|
|
249
|
+
},
|
|
250
|
+
rowReverse: {
|
|
251
|
+
flexDirection: 'row-reverse',
|
|
252
|
+
},
|
|
253
|
+
columnReverse: {
|
|
254
|
+
flexDirection: 'column-reverse',
|
|
255
|
+
},
|
|
256
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
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 strict-local
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import typeof VirtualizedList from './VirtualizedList';
|
|
12
|
+
|
|
13
|
+
import * as React from 'react';
|
|
14
|
+
import {useContext, useMemo} from 'react';
|
|
15
|
+
|
|
16
|
+
type Context = $ReadOnly<{
|
|
17
|
+
cellKey: ?string,
|
|
18
|
+
getScrollMetrics: () => {
|
|
19
|
+
contentLength: number,
|
|
20
|
+
dOffset: number,
|
|
21
|
+
dt: number,
|
|
22
|
+
offset: number,
|
|
23
|
+
timestamp: number,
|
|
24
|
+
velocity: number,
|
|
25
|
+
visibleLength: number,
|
|
26
|
+
zoomScale: number,
|
|
27
|
+
},
|
|
28
|
+
horizontal: ?boolean,
|
|
29
|
+
getOutermostParentListRef: () => React.ElementRef<VirtualizedList>,
|
|
30
|
+
registerAsNestedChild: ({
|
|
31
|
+
cellKey: string,
|
|
32
|
+
ref: React.ElementRef<VirtualizedList>,
|
|
33
|
+
}) => void,
|
|
34
|
+
unregisterAsNestedChild: ({ref: React.ElementRef<VirtualizedList>}) => void,
|
|
35
|
+
}>;
|
|
36
|
+
|
|
37
|
+
export const VirtualizedListContext: React.Context<?Context> =
|
|
38
|
+
React.createContext(null);
|
|
39
|
+
if (__DEV__) {
|
|
40
|
+
VirtualizedListContext.displayName = 'VirtualizedListContext';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Resets the context. Intended for use by portal-like components (e.g. Modal).
|
|
45
|
+
*/
|
|
46
|
+
export function VirtualizedListContextResetter({
|
|
47
|
+
children,
|
|
48
|
+
}: {
|
|
49
|
+
children: React.Node,
|
|
50
|
+
}): React.Node {
|
|
51
|
+
return (
|
|
52
|
+
<VirtualizedListContext.Provider value={null}>
|
|
53
|
+
{children}
|
|
54
|
+
</VirtualizedListContext.Provider>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Sets the context with memoization. Intended to be used by `VirtualizedList`.
|
|
60
|
+
*/
|
|
61
|
+
export function VirtualizedListContextProvider({
|
|
62
|
+
children,
|
|
63
|
+
value,
|
|
64
|
+
}: {
|
|
65
|
+
children: React.Node,
|
|
66
|
+
value: Context,
|
|
67
|
+
}): React.Node {
|
|
68
|
+
// Avoid setting a newly created context object if the values are identical.
|
|
69
|
+
const context = useMemo(
|
|
70
|
+
() => ({
|
|
71
|
+
cellKey: null,
|
|
72
|
+
getScrollMetrics: value.getScrollMetrics,
|
|
73
|
+
horizontal: value.horizontal,
|
|
74
|
+
getOutermostParentListRef: value.getOutermostParentListRef,
|
|
75
|
+
registerAsNestedChild: value.registerAsNestedChild,
|
|
76
|
+
unregisterAsNestedChild: value.unregisterAsNestedChild,
|
|
77
|
+
}),
|
|
78
|
+
[
|
|
79
|
+
value.getScrollMetrics,
|
|
80
|
+
value.horizontal,
|
|
81
|
+
value.getOutermostParentListRef,
|
|
82
|
+
value.registerAsNestedChild,
|
|
83
|
+
value.unregisterAsNestedChild,
|
|
84
|
+
],
|
|
85
|
+
);
|
|
86
|
+
return (
|
|
87
|
+
<VirtualizedListContext.Provider value={context}>
|
|
88
|
+
{children}
|
|
89
|
+
</VirtualizedListContext.Provider>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Sets the `cellKey`. Intended to be used by `VirtualizedList` for each cell.
|
|
95
|
+
*/
|
|
96
|
+
export function VirtualizedListCellContextProvider({
|
|
97
|
+
cellKey,
|
|
98
|
+
children,
|
|
99
|
+
}: {
|
|
100
|
+
cellKey: string,
|
|
101
|
+
children: React.Node,
|
|
102
|
+
}): React.Node {
|
|
103
|
+
// Avoid setting a newly created context object if the values are identical.
|
|
104
|
+
const currContext = useContext(VirtualizedListContext);
|
|
105
|
+
const context = useMemo(
|
|
106
|
+
() => (currContext == null ? null : {...currContext, cellKey}),
|
|
107
|
+
[currContext, cellKey],
|
|
108
|
+
);
|
|
109
|
+
return (
|
|
110
|
+
<VirtualizedListContext.Provider value={context}>
|
|
111
|
+
{children}
|
|
112
|
+
</VirtualizedListContext.Provider>
|
|
113
|
+
);
|
|
114
|
+
}
|