Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve inverted VirtualizedList implementation #23632

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 25 additions & 34 deletions Libraries/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,6 @@ class VirtualizedList extends React.PureComponent<Props, State> {
stickyIndicesFromProps: Set<number>,
first: number,
last: number,
inversionStyle: ViewStyleProp,
) {
const {
CellRendererComponent,
Expand All @@ -662,6 +661,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
getItem,
getItemCount,
horizontal,
inverted,
keyExtractor,
} = this.props;
const stickyOffset = this.props.ListHeaderComponent ? 1 : 0;
Expand All @@ -682,8 +682,8 @@ class VirtualizedList extends React.PureComponent<Props, State> {
cellKey={key}
fillRateHelper={this._fillRateHelper}
horizontal={horizontal}
inverted={inverted}
index={ii}
inversionStyle={inversionStyle}
item={item}
key={key}
prevCellKey={prevCellKey}
Expand Down Expand Up @@ -758,10 +758,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
key="$header">
<View
onLayout={this._onLayoutHeader}
style={StyleSheet.compose(
inversionStyle,
this.props.ListHeaderComponentStyle,
)}>
style={this.props.ListHeaderComponentStyle}>
{
// $FlowFixMe - Typing ReactNativeComponent revealed errors
element
Expand All @@ -785,7 +782,6 @@ class VirtualizedList extends React.PureComponent<Props, State> {
stickyIndicesFromProps,
0,
lastInitialIndex,
inversionStyle,
);
const firstAfterInitial = Math.max(lastInitialIndex + 1, first);
if (!isVirtualizationDisabled && first > lastInitialIndex + 1) {
Expand All @@ -810,7 +806,6 @@ class VirtualizedList extends React.PureComponent<Props, State> {
stickyIndicesFromProps,
ii,
ii,
inversionStyle,
);
const trailSpace =
this._getFrameMetricsApprox(first).offset -
Expand Down Expand Up @@ -839,7 +834,6 @@ class VirtualizedList extends React.PureComponent<Props, State> {
stickyIndicesFromProps,
firstAfterInitial,
last,
inversionStyle,
);
if (!this._hasWarned.keys && _usedIndexForKey) {
console.warn(
Expand Down Expand Up @@ -884,7 +878,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
element.props.onLayout(event);
}
},
style: [element.props.style, inversionStyle],
style: element.props.style,
}),
);
}
Expand All @@ -901,10 +895,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
key="$footer">
<View
onLayout={this._onLayoutFooter}
style={StyleSheet.compose(
inversionStyle,
this.props.ListFooterComponentStyle,
)}>
style={this.props.ListFooterComponentStyle}>
{
// $FlowFixMe - Typing ReactNativeComponent revealed errors
element
Expand All @@ -921,19 +912,14 @@ class VirtualizedList extends React.PureComponent<Props, State> {
onScrollBeginDrag: this._onScrollBeginDrag,
onScrollEndDrag: this._onScrollEndDrag,
onMomentumScrollEnd: this._onMomentumScrollEnd,
contentContainerStyle: inversionStyle,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.props.contentContainerStyle is lost, please use array of styles or maybe StyleSheet.compose (I am not sure what is the difference).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably got wrong what inversionStyle was supposed to be for. Ok, I should fix that indeed if it actually is the contentContainerStyle.

scrollEventThrottle: this.props.scrollEventThrottle, // TODO: Android support
invertStickyHeaders:
this.props.invertStickyHeaders !== undefined
? this.props.invertStickyHeaders
: this.props.inverted,
stickyHeaderIndices,
};
if (inversionStyle && itemCount !== 0) {
/* $FlowFixMe(>=0.70.0 site=react_native_fb) This comment suppresses an
* error found when Flow v0.70 was deployed. To see the error delete
* this comment and run Flow. */
scrollProps.style = [inversionStyle, this.props.style];
}

this._hasMore =
this.state.last < this.props.getItemCount(this.props.data) - 1;
Expand Down Expand Up @@ -980,6 +966,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
_fillRateHelper: FillRateHelper;
_frames = {};
_footerLength = 0;
_distanceFromEnd = 0;
_hasDataChangedSinceEndReached = true;
_hasInteracted = false;
_hasMore = false;
Expand Down Expand Up @@ -1243,22 +1230,28 @@ class VirtualizedList extends React.PureComponent<Props, State> {
getItemCount,
onEndReached,
onEndReachedThreshold,
inverted,
} = this.props;
const {contentLength, visibleLength, offset} = this._scrollMetrics;
const distanceFromEnd = contentLength - visibleLength - offset;
const distanceFromEnd = inverted
? offset
: contentLength - visibleLength - offset;
if (
onEndReached &&
this.state.last === getItemCount(data) - 1 &&
/* $FlowFixMe(>=0.63.0 site=react_native_fb) This comment suppresses an
* error found when Flow v0.63 was deployed. To see the error delete this
* comment and run Flow. */
distanceFromEnd < onEndReachedThreshold * visibleLength &&
distanceFromEnd !== this._distanceFromEnd &&
(this._hasDataChangedSinceEndReached ||
this._scrollMetrics.contentLength !== this._sentEndForContentLength)
) {
// Only call onEndReached once for a given dataset + content length.
this._hasDataChangedSinceEndReached = false;
this._sentEndForContentLength = this._scrollMetrics.contentLength;
//We record the value to prevent from calling twice, especially when distanceFromEnd===0 as the scroll will stick to the top
this._distanceFromEnd = distanceFromEnd;
onEndReached({distanceFromEnd});
}
}
Expand Down Expand Up @@ -1447,7 +1440,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
};

_updateCellsToRender = () => {
const {data, getItemCount, onEndReachedThreshold} = this.props;
const {data, getItemCount, onEndReachedThreshold, inverted} = this.props;
const isVirtualizationDisabled = this._isVirtualizationDisabled();
this._updateViewableItems(data);
if (!data) {
Expand Down Expand Up @@ -1476,7 +1469,9 @@ class VirtualizedList extends React.PureComponent<Props, State> {
}
} else {
const {contentLength, offset, visibleLength} = this._scrollMetrics;
const distanceFromEnd = contentLength - visibleLength - offset;
const distanceFromEnd = inverted
? offset
: contentLength - visibleLength - offset;
const renderAhead =
/* $FlowFixMe(>=0.63.0 site=react_native_fb) This comment suppresses
* an error found when Flow v0.63 was deployed. To see the error
Expand Down Expand Up @@ -1618,8 +1613,8 @@ class CellRenderer extends React.Component<
cellKey: string,
fillRateHelper: FillRateHelper,
horizontal: ?boolean,
inverted: ?boolean,
index: number,
inversionStyle: ViewStyleProp,
item: Item,
onLayout: (event: Object) => void, // This is extracted by ScrollViewStickyHeader
onUnmount: (cellKey: string) => void,
Expand Down Expand Up @@ -1693,9 +1688,9 @@ class CellRenderer extends React.Component<
ItemSeparatorComponent,
fillRateHelper,
horizontal,
inverted,
item,
index,
inversionStyle,
parentProps,
} = this.props;
const {renderItem, getItemLayout} = parentProps;
Expand All @@ -1717,13 +1712,9 @@ class CellRenderer extends React.Component<
const itemSeparator = ItemSeparatorComponent && (
<ItemSeparatorComponent {...this.state.separatorProps} />
);
const cellStyle = inversionStyle
? horizontal
? [{flexDirection: 'row-reverse'}, inversionStyle]
: [{flexDirection: 'column-reverse'}, inversionStyle]
: horizontal
? [{flexDirection: 'row'}, inversionStyle]
: inversionStyle;
const cellStyle = horizontal
? {flexDirection: inverted ? 'row-reverse' : 'row'}
: {flexDirection: inverted ? 'column-reverse' : 'column'};
if (!CellRendererComponent) {
return (
/* $FlowFixMe(>=0.89.0 site=react_native_fb) This comment suppresses an
Expand Down Expand Up @@ -1772,10 +1763,10 @@ class VirtualizedCellWrapper extends React.Component<{

const styles = StyleSheet.create({
verticallyInverted: {
transform: [{scaleY: -1}],
flexDirection: 'column-reverse',
},
horizontallyInverted: {
transform: [{scaleX: -1}],
flexDirection: 'row-reverse',
},
debug: {
flex: 1,
Expand Down
40 changes: 34 additions & 6 deletions Libraries/Lists/__tests__/__snapshots__/FlatList-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports[`FlatList renders all the bells and whistles 1`] = `
ListEmptyComponent={[Function]}
ListFooterComponent={[Function]}
ListHeaderComponent={[Function]}
contentContainerStyle={null}
data={
Array [
Object {
Expand Down Expand Up @@ -65,7 +66,11 @@ exports[`FlatList renders all the bells and whistles 1`] = `
<header />
</View>
<View
style={null}
style={
Object {
"flexDirection": "column",
}
}
>
<View
style={
Expand All @@ -84,7 +89,11 @@ exports[`FlatList renders all the bells and whistles 1`] = `
<separator />
</View>
<View
style={null}
style={
Object {
"flexDirection": "column",
}
}
>
<View
style={
Expand All @@ -103,7 +112,11 @@ exports[`FlatList renders all the bells and whistles 1`] = `
<separator />
</View>
<View
style={null}
style={
Object {
"flexDirection": "column",
}
}
>
<View
style={
Expand All @@ -128,6 +141,7 @@ exports[`FlatList renders all the bells and whistles 1`] = `

exports[`FlatList renders empty list 1`] = `
<RCTScrollView
contentContainerStyle={null}
data={Array []}
disableVirtualization={false}
getItem={[Function]}
Expand Down Expand Up @@ -158,6 +172,7 @@ exports[`FlatList renders empty list 1`] = `

exports[`FlatList renders null list 1`] = `
<RCTScrollView
contentContainerStyle={null}
disableVirtualization={false}
getItem={[Function]}
getItemCount={[Function]}
Expand Down Expand Up @@ -187,6 +202,7 @@ exports[`FlatList renders null list 1`] = `

exports[`FlatList renders simple list 1`] = `
<RCTScrollView
contentContainerStyle={null}
data={
Array [
Object {
Expand Down Expand Up @@ -226,23 +242,35 @@ exports[`FlatList renders simple list 1`] = `
<View>
<View
onLayout={[Function]}
style={null}
style={
Object {
"flexDirection": "column",
}
}
>
<item
value="i1"
/>
</View>
<View
onLayout={[Function]}
style={null}
style={
Object {
"flexDirection": "column",
}
}
>
<item
value="i2"
/>
</View>
<View
onLayout={[Function]}
style={null}
style={
Object {
"flexDirection": "column",
}
}
>
<item
value="i3"
Expand Down
Loading