diff --git a/Libraries/Lists/VirtualizedList.js b/Libraries/Lists/VirtualizedList.js index 545a04a4127..3b53a2a883a 100644 --- a/Libraries/Lists/VirtualizedList.js +++ b/Libraries/Lists/VirtualizedList.js @@ -304,7 +304,6 @@ type Props = {| |}; type DefaultProps = {| - horizontal: boolean, initialNumToRender: number, keyExtractor: (item: Item, index: number) => string, maxToRenderPerBatch: number, @@ -322,6 +321,10 @@ type State = { last: number, }; +function horizontalOrDefault(horizontal: ?boolean) { + return horizontal ?? false; +} + /** * Base implementation for the more convenient [``](https://reactnative.dev/docs/flatlist.html) * and [``](https://reactnative.dev/docs/sectionlist.html) components, which are also better @@ -380,7 +383,9 @@ class VirtualizedList extends React.PureComponent { } this._scrollRef.scrollTo( - this.props.horizontal ? {x: offset, animated} : {y: offset, animated}, + horizontalOrDefault(this.props.horizontal) + ? {x: offset, animated} + : {y: offset, animated}, ); } @@ -502,7 +507,9 @@ class VirtualizedList extends React.PureComponent { } this._scrollRef.scrollTo( - this.props.horizontal ? {x: offset, animated} : {y: offset, animated}, + horizontalOrDefault(this.props.horizontal) + ? {x: offset, animated} + : {y: offset, animated}, ); } @@ -560,7 +567,6 @@ class VirtualizedList extends React.PureComponent { } static defaultProps: DefaultProps = { - horizontal: false, initialNumToRender: 10, keyExtractor: (item: Item, index: number) => { if (item.key != null) { @@ -594,7 +600,7 @@ class VirtualizedList extends React.PureComponent { return { listKey: this._getListKey(), cellKey: this._getCellKey(), - horizontal: !!this.props.horizontal, + horizontal: horizontalOrDefault(this.props.horizontal), parent: this.context?.debugInfo, }; } @@ -839,7 +845,8 @@ class VirtualizedList extends React.PureComponent { _isNestedWithSameOrientation(): boolean { const nestedContext = this.context; return !!( - nestedContext && !!nestedContext.horizontal === !!this.props.horizontal + nestedContext && + !!nestedContext.horizontal === horizontalOrDefault(this.props.horizontal) ); } @@ -864,7 +871,7 @@ class VirtualizedList extends React.PureComponent { const {data, horizontal} = this.props; const isVirtualizationDisabled = this._isVirtualizationDisabled(); const inversionStyle = this.props.inverted - ? this.props.horizontal + ? horizontalOrDefault(this.props.horizontal) ? styles.horizontallyInverted : styles.verticallyInverted : null; @@ -1070,7 +1077,7 @@ class VirtualizedList extends React.PureComponent { value={{ cellKey: null, getScrollMetrics: this._getScrollMetrics, - horizontal: this.props.horizontal, + horizontal: horizontalOrDefault(this.props.horizontal), getOutermostParentListRef: this._getOutermostParentListRef, getNestedChildState: this._getNestedChildState, registerAsNestedChild: this._registerAsNestedChild, @@ -1096,7 +1103,8 @@ class VirtualizedList extends React.PureComponent { {scrollContext => { if ( scrollContext != null && - !scrollContext.horizontal === !this.props.horizontal && + !scrollContext.horizontal === + !horizontalOrDefault(this.props.horizontal) && !this._hasWarned.nesting && this.context == null ) { @@ -1452,7 +1460,9 @@ class VirtualizedList extends React.PureComponent { ... }>, ): number { - return !this.props.horizontal ? metrics.height : metrics.width; + return !horizontalOrDefault(this.props.horizontal) + ? metrics.height + : metrics.width; } _selectOffset( @@ -1462,7 +1472,7 @@ class VirtualizedList extends React.PureComponent { ... }>, ): number { - return !this.props.horizontal ? metrics.y : metrics.x; + return !horizontalOrDefault(this.props.horizontal) ? metrics.y : metrics.x; } _maybeCallOnEndReached() { diff --git a/Libraries/Lists/__tests__/__snapshots__/FlatList-test.js.snap b/Libraries/Lists/__tests__/__snapshots__/FlatList-test.js.snap index f16138c369e..471f89f48b1 100644 --- a/Libraries/Lists/__tests__/__snapshots__/FlatList-test.js.snap +++ b/Libraries/Lists/__tests__/__snapshots__/FlatList-test.js.snap @@ -28,7 +28,6 @@ exports[`FlatList renders all the bells and whistles 1`] = ` getItem={[Function]} getItemCount={[Function]} getItemLayout={[Function]} - horizontal={false} initialNumToRender={10} keyExtractor={[Function]} maxToRenderPerBatch={10} @@ -130,7 +129,6 @@ exports[`FlatList renders empty list 1`] = ` data={Array []} getItem={[Function]} getItemCount={[Function]} - horizontal={false} initialNumToRender={10} keyExtractor={[Function]} maxToRenderPerBatch={10} @@ -158,7 +156,6 @@ exports[`FlatList renders null list 1`] = `