Fixed: Restoring scroll position when going back to index page

This commit is contained in:
ta264 2021-05-06 21:49:33 +01:00 committed by Mark McDowall
parent 7c74d19515
commit 99843d2876
5 changed files with 40 additions and 10 deletions

View File

@ -39,7 +39,8 @@ class VirtualTable extends Component {
super(props, context); super(props, context);
this.state = { this.state = {
width: 0 width: 0,
scrollRestored: false
}; };
this._grid = null; this._grid = null;
@ -48,11 +49,13 @@ class VirtualTable extends Component {
componentDidUpdate(prevProps, prevState) { componentDidUpdate(prevProps, prevState) {
const { const {
items, items,
scrollIndex scrollIndex,
scrollTop
} = this.props; } = this.props;
const { const {
width width,
scrollRestored
} = this.state; } = this.state;
if (this._grid && (prevState.width !== width || hasDifferentItemsOrOrder(prevProps.items, items))) { if (this._grid && (prevState.width !== width || hasDifferentItemsOrOrder(prevProps.items, items))) {
@ -60,6 +63,11 @@ class VirtualTable extends Component {
this._grid.recomputeGridSize(); this._grid.recomputeGridSize();
} }
if (this._grid && scrollTop !== undefined && scrollTop !== 0 && !scrollRestored) {
this.setState({ scrollRestored: true });
this._grid.scrollToPosition({ scrollTop });
}
if (scrollIndex != null && scrollIndex !== prevProps.scrollIndex) { if (scrollIndex != null && scrollIndex !== prevProps.scrollIndex) {
this._grid.scrollToCell({ this._grid.scrollToCell({
rowIndex: scrollIndex, rowIndex: scrollIndex,
@ -137,6 +145,7 @@ class VirtualTable extends Component {
{header} {header}
<div ref={registerChild}> <div ref={registerChild}>
<Grid <Grid
{...otherProps}
ref={this.setGridRef} ref={this.setGridRef}
autoContainerWidth={true} autoContainerWidth={true}
autoHeight={true} autoHeight={true}
@ -158,7 +167,6 @@ class VirtualTable extends Component {
className={styles.tableBodyContainer} className={styles.tableBodyContainer}
style={gridStyle} style={gridStyle}
containerStyle={containerStyle} containerStyle={containerStyle}
{...otherProps}
/> />
</div> </div>
</Scroller> </Scroller>
@ -176,6 +184,7 @@ VirtualTable.propTypes = {
className: PropTypes.string.isRequired, className: PropTypes.string.isRequired,
items: PropTypes.arrayOf(PropTypes.object).isRequired, items: PropTypes.arrayOf(PropTypes.object).isRequired,
scrollIndex: PropTypes.number, scrollIndex: PropTypes.number,
scrollTop: PropTypes.number,
scroller: PropTypes.instanceOf(Element).isRequired, scroller: PropTypes.instanceOf(Element).isRequired,
header: PropTypes.node.isRequired, header: PropTypes.node.isRequired,
headerHeight: PropTypes.number.isRequired, headerHeight: PropTypes.number.isRequired,

View File

@ -8,7 +8,7 @@ function withScrollPosition(WrappedComponent, scrollPositionKey) {
history history
} = props; } = props;
const scrollTop = history.action === 'POP' ? const scrollTop = history.action === 'POP' || (history.location.state && history.location.state.restoreScrollPosition) ?
scrollPositions[scrollPositionKey] : scrollPositions[scrollPositionKey] :
0; 0;

View File

@ -60,7 +60,8 @@ class SeriesIndexOverviews extends Component {
columnCount: 1, columnCount: 1,
posterWidth: 162, posterWidth: 162,
posterHeight: 238, posterHeight: 238,
rowHeight: calculateRowHeight(238, null, props.isSmallScreen, {}) rowHeight: calculateRowHeight(238, null, props.isSmallScreen, {}),
scrollRestored: false
}; };
this._grid = null; this._grid = null;
@ -72,12 +73,14 @@ class SeriesIndexOverviews extends Component {
sortKey, sortKey,
overviewOptions, overviewOptions,
jumpToCharacter, jumpToCharacter,
scrollTop,
isSmallScreen isSmallScreen
} = this.props; } = this.props;
const { const {
width, width,
rowHeight rowHeight,
scrollRestored
} = this.state; } = this.state;
if (prevProps.sortKey !== sortKey || if (prevProps.sortKey !== sortKey ||
@ -97,6 +100,11 @@ class SeriesIndexOverviews extends Component {
this._grid.recomputeGridSize(); this._grid.recomputeGridSize();
} }
if (this._grid && scrollTop !== 0 && !scrollRestored) {
this.setState({ scrollRestored: true });
this._grid.scrollToPosition({ scrollTop });
}
if (jumpToCharacter != null && jumpToCharacter !== prevProps.jumpToCharacter) { if (jumpToCharacter != null && jumpToCharacter !== prevProps.jumpToCharacter) {
const index = getIndexOfFirstCharacter(items, jumpToCharacter); const index = getIndexOfFirstCharacter(items, jumpToCharacter);
@ -255,6 +263,7 @@ SeriesIndexOverviews.propTypes = {
items: PropTypes.arrayOf(PropTypes.object).isRequired, items: PropTypes.arrayOf(PropTypes.object).isRequired,
sortKey: PropTypes.string, sortKey: PropTypes.string,
overviewOptions: PropTypes.object.isRequired, overviewOptions: PropTypes.object.isRequired,
scrollTop: PropTypes.number.isRequired,
jumpToCharacter: PropTypes.string, jumpToCharacter: PropTypes.string,
scroller: PropTypes.instanceOf(Element).isRequired, scroller: PropTypes.instanceOf(Element).isRequired,
showRelativeDates: PropTypes.bool.isRequired, showRelativeDates: PropTypes.bool.isRequired,

View File

@ -101,7 +101,8 @@ class SeriesIndexPosters extends Component {
columnCount: 1, columnCount: 1,
posterWidth: 162, posterWidth: 162,
posterHeight: 238, posterHeight: 238,
rowHeight: calculateRowHeight(238, null, props.isSmallScreen, {}) rowHeight: calculateRowHeight(238, null, props.isSmallScreen, {}),
scrollRestored: false
}; };
this._isInitialized = false; this._isInitialized = false;
@ -115,6 +116,7 @@ class SeriesIndexPosters extends Component {
sortKey, sortKey,
posterOptions, posterOptions,
jumpToCharacter, jumpToCharacter,
scrollTop,
isSmallScreen isSmallScreen
} = this.props; } = this.props;
@ -122,7 +124,8 @@ class SeriesIndexPosters extends Component {
width, width,
columnWidth, columnWidth,
columnCount, columnCount,
rowHeight rowHeight,
scrollRestored
} = this.state; } = this.state;
if (prevProps.sortKey !== sortKey || if (prevProps.sortKey !== sortKey ||
@ -140,6 +143,11 @@ class SeriesIndexPosters extends Component {
this._grid.recomputeGridSize(); this._grid.recomputeGridSize();
} }
if (this._grid && scrollTop !== 0 && !scrollRestored) {
this.setState({ scrollRestored: true });
this._grid.scrollToPosition({ scrollTop });
}
if (jumpToCharacter != null && jumpToCharacter !== prevProps.jumpToCharacter) { if (jumpToCharacter != null && jumpToCharacter !== prevProps.jumpToCharacter) {
const index = getIndexOfFirstCharacter(items, jumpToCharacter); const index = getIndexOfFirstCharacter(items, jumpToCharacter);
@ -315,6 +323,7 @@ SeriesIndexPosters.propTypes = {
sortKey: PropTypes.string, sortKey: PropTypes.string,
posterOptions: PropTypes.object.isRequired, posterOptions: PropTypes.object.isRequired,
jumpToCharacter: PropTypes.string, jumpToCharacter: PropTypes.string,
scrollTop: PropTypes.number.isRequired,
scroller: PropTypes.instanceOf(Element).isRequired, scroller: PropTypes.instanceOf(Element).isRequired,
showRelativeDates: PropTypes.bool.isRequired, showRelativeDates: PropTypes.bool.isRequired,
shortDateFormat: PropTypes.string.isRequired, shortDateFormat: PropTypes.string.isRequired,

View File

@ -82,7 +82,8 @@ class SeriesIndexTable extends Component {
showBanners, showBanners,
isSmallScreen, isSmallScreen,
onSortPress, onSortPress,
scroller scroller,
scrollTop
} = this.props; } = this.props;
return ( return (
@ -90,6 +91,7 @@ class SeriesIndexTable extends Component {
className={styles.tableContainer} className={styles.tableContainer}
items={items} items={items}
scrollIndex={this.state.scrollIndex} scrollIndex={this.state.scrollIndex}
scrollTop={scrollTop}
scroller={scroller} scroller={scroller}
isSmallScreen={isSmallScreen} isSmallScreen={isSmallScreen}
rowHeight={showBanners ? 70 : 38} rowHeight={showBanners ? 70 : 38}
@ -117,6 +119,7 @@ SeriesIndexTable.propTypes = {
sortDirection: PropTypes.oneOf(sortDirections.all), sortDirection: PropTypes.oneOf(sortDirections.all),
showBanners: PropTypes.bool.isRequired, showBanners: PropTypes.bool.isRequired,
jumpToCharacter: PropTypes.string, jumpToCharacter: PropTypes.string,
scrollTop: PropTypes.number,
scroller: PropTypes.instanceOf(Element).isRequired, scroller: PropTypes.instanceOf(Element).isRequired,
isSmallScreen: PropTypes.bool.isRequired, isSmallScreen: PropTypes.bool.isRequired,
onSortPress: PropTypes.func.isRequired onSortPress: PropTypes.func.isRequired