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

fix: Add scrollTopOnPagination property to Table #22115

Merged
merged 1 commit into from
Nov 18, 2022
Merged
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
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;
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should even expose a property to configure this behavior. My fear is that we would allow different behaviors throughout the app. We could follow the same approach we did with the Select component and not expose this property until a valid use case appears. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree @michael-s-molina that this should be a standard behaviour. However, I believe it makes more sense to validate that for the new component being built by @eric-briscoe rather than changing all the tables right now which may cause unwanted behaviours.

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