Skip to content

Commit

Permalink
fix: Add scrollTopOnPagination property to Table (#22115)
Browse files Browse the repository at this point in the history
  • Loading branch information
geido committed Nov 18, 2022
1 parent 17c2bd8 commit 896c832
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ describe('Drill to detail modal', () => {
.then($rows => {
expect($rows).to.contain('Victoria');
});

// verify scroll top on pagination
cy.getBySelLike('Number-modal')
.find('.table-condensed')
.scrollTo(0, 100);

cy.get("[role='rowgroup'] [role='row']")
.contains('Miguel')
.should('not.be.visible');

cy.get(".pagination-container [role='navigation'] [role='button']")
.eq(1)
.click();

cy.get("[role='rowgroup'] [role='row']")
.contains('Aaron')
.should('be.visible');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export default function DrillDetailPane({
margin-bottom: 0;
}
`}
scrollTopOnPagination
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ InteractiveTableView.args = {
showRowCount: true,
withPagination: true,
columnsForWrapText: ['Summary'],
scrollTopOnPagination: false,
};

InteractiveTableView.argTypes = {
Expand Down
48 changes: 29 additions & 19 deletions superset-frontend/src/components/TableView/TableView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import React, { useEffect } from 'react';
import React, { useEffect, useRef } from 'react';
import isEqual from 'lodash/isEqual';
import { styled, t } from '@superset-ui/core';
import { useFilters, usePagination, useSortBy, useTable } from 'react-table';
Expand Down Expand Up @@ -49,6 +49,7 @@ export interface TableViewProps {
isPaginationSticky?: boolean;
showRowCount?: boolean;
scrollTable?: boolean;
scrollTopOnPagination?: boolean;
small?: boolean;
columnsForWrapText?: string[];
}
Expand Down Expand Up @@ -130,6 +131,7 @@ const TableView = ({
serverPagination = false,
columnsForWrapText,
onServerPagination = () => {},
scrollTopOnPagination = false,
...props
}: TableViewProps) => {
const initialState = {
Expand Down Expand Up @@ -161,22 +163,6 @@ const TableView = ({
useSortBy,
usePagination,
);
useEffect(() => {
if (serverPagination && pageIndex !== initialState.pageIndex) {
onServerPagination({
pageIndex,
});
}
}, [pageIndex]);

useEffect(() => {
if (serverPagination && !isEqual(sortBy, initialState.sortBy)) {
onServerPagination({
pageIndex: 0,
sortBy,
});
}
}, [sortBy]);

const content = withPagination ? page : rows;

Expand All @@ -194,10 +180,34 @@ const TableView = ({

const isEmpty = !loading && content.length === 0;
const hasPagination = pageCount > 1 && withPagination;
const tableRef = useRef<HTMLTableElement>(null);
const handleGotoPage = (p: number) => {
if (scrollTopOnPagination) {
tableRef?.current?.scroll(0, 0);
}
gotoPage(p);
};

useEffect(() => {
if (serverPagination && pageIndex !== initialState.pageIndex) {
onServerPagination({
pageIndex,
});
}
}, [pageIndex]);

useEffect(() => {
if (serverPagination && !isEqual(sortBy, initialState.sortBy)) {
onServerPagination({
pageIndex: 0,
sortBy,
});
}
}, [sortBy]);

return (
<>
<TableViewStyles {...props}>
<TableViewStyles {...props} ref={tableRef}>
<TableCollection
getTableProps={getTableProps}
getTableBodyProps={getTableBodyProps}
Expand Down Expand Up @@ -229,7 +239,7 @@ const TableView = ({
<Pagination
totalPages={pageCount || 0}
currentPage={pageCount ? pageIndex + 1 : 0}
onChange={(p: number) => gotoPage(p - 1)}
onChange={(p: number) => handleGotoPage(p - 1)}
hideFirstAndLastPageLinks
/>
{showRowCount && (
Expand Down

0 comments on commit 896c832

Please sign in to comment.