From add03ecc7f512d94c394847eb3797122a362404c Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 5 Dec 2024 17:54:25 +0800 Subject: [PATCH] [General] Fixes scrolling to section not work when index is 0 and stickySectionHeadersEnabled --- .../Libraries/Components/ScrollView/ScrollView.js | 6 +++++- packages/virtualized-lists/Lists/VirtualizedList.js | 3 +++ .../virtualized-lists/Lists/VirtualizedListCellRenderer.js | 5 +++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/react-native/Libraries/Components/ScrollView/ScrollView.js b/packages/react-native/Libraries/Components/ScrollView/ScrollView.js index f2dbf2646fbe44..4193d3b0d0ad60 100644 --- a/packages/react-native/Libraries/Components/ScrollView/ScrollView.js +++ b/packages/react-native/Libraries/Components/ScrollView/ScrollView.js @@ -1668,7 +1668,11 @@ class ScrollView extends React.Component { nextHeaderLayoutY={this._headerLayoutYs.get( this._getKeyForIndex(nextIndex, children), )} - onLayout={event => this._onStickyHeaderLayout(index, event, key)} + onLayout={event => { + this._onStickyHeaderLayout(index, event, key); + const {onCellLayout, cellKey, index: cellIndex} = child.props; + onCellLayout?.(event, cellKey, cellIndex); + }} scrollAnimatedValue={this._scrollAnimatedValue} inverted={this.props.invertStickyHeaders} hiddenOnScroll={this.props.stickyHeaderHiddenOnScroll} diff --git a/packages/virtualized-lists/Lists/VirtualizedList.js b/packages/virtualized-lists/Lists/VirtualizedList.js index 37b70ad74fb061..77e52ba7154a04 100644 --- a/packages/virtualized-lists/Lists/VirtualizedList.js +++ b/packages/virtualized-lists/Lists/VirtualizedList.js @@ -789,8 +789,10 @@ class VirtualizedList extends StateSafePureComponent { const key = VirtualizedList._keyExtractor(item, ii, this.props); this._indicesToKeys.set(ii, key); + let isStickyHeader = false; if (stickyIndicesFromProps.has(ii + stickyOffset)) { stickyHeaderIndices.push(cells.length); + isStickyHeader = true; } const shouldListenForLayout = @@ -811,6 +813,7 @@ class VirtualizedList extends StateSafePureComponent { onUpdateSeparators={this._onUpdateSeparators} onCellFocusCapture={this._onCellFocusCapture} onUnmount={this._onCellUnmount} + isStickyHeader={isStickyHeader} ref={ref => { this._cellRefs[key] = ref; }} diff --git a/packages/virtualized-lists/Lists/VirtualizedListCellRenderer.js b/packages/virtualized-lists/Lists/VirtualizedListCellRenderer.js index 830c8b72feb349..4b16de17f17e34 100644 --- a/packages/virtualized-lists/Lists/VirtualizedListCellRenderer.js +++ b/packages/virtualized-lists/Lists/VirtualizedListCellRenderer.js @@ -40,6 +40,7 @@ export type Props = { ) => void, prevCellKey: ?string, renderItem?: ?RenderItemType, + isStickyHeader?: boolean, ... }; @@ -118,6 +119,10 @@ export default class CellRenderer extends React.PureComponent< } _onLayout = (nativeEvent: LayoutEvent): void => { + const {isStickyHeader} = this.props; + if (isStickyHeader) { + return; + } this.props.onCellLayout?.( nativeEvent, this.props.cellKey,