Skip to content
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
41 changes: 41 additions & 0 deletions src/components/MainTable/MainTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,47 @@ export const Paginated: Story = {
name: "Paginated",
};

/**
* Set `scrollToTop` to scroll to the top of the page when the page of a
* paginated table is changed. Scroll down to the pagination and change the
* page to see this.
*/
export const PaginatedWithScrollToTop: Story = {
args: {
headers: [
{
content: "Machine",
},
{
content: "Status",
},
{
content: "Cores",
className: "u-align--right",
},
],
paginate: 25,
rows: Array.from({ length: 100 }, (_, index) => ({
columns: [
{
content: `machine-${index + 1}`,
role: "rowheader",
},
{
content: ["Ready", "Deploying", "Allocated"][index % 3],
},
{
content: (index % 8) + 1,
className: "u-align--right",
},
],
})),
scrollToTop: true,
},

name: "Paginated with scroll to top",
};

export const Empty: Story = {
render: () => (
<MainTable
Expand Down
24 changes: 24 additions & 0 deletions src/components/MainTable/MainTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,30 @@ describe("MainTable", () => {
expect(screen.getAllByRole("rowheader")[0].textContent).toEqual("Idle");
});

it("can scroll to the top when changing the page", async () => {
const scrollTo = jest
.spyOn(window, "scrollTo")
.mockImplementation(() => undefined);
render(<MainTable paginate={2} rows={rows} scrollToTop />);
await userEvent.click(
screen.getByRole("button", { name: PaginationButtonLabel.Next }),
);
expect(scrollTo).toHaveBeenCalledWith(0, 0);
scrollTo.mockRestore();
});

it("does not scroll to the top when changing the page by default", async () => {
const scrollTo = jest
.spyOn(window, "scrollTo")
.mockImplementation(() => undefined);
render(<MainTable paginate={2} rows={rows} />);
await userEvent.click(
screen.getByRole("button", { name: PaginationButtonLabel.Next }),
);
expect(scrollTo).not.toHaveBeenCalled();
scrollTo.mockRestore();
});

describe("sorting", () => {
beforeEach(() => {
headers[0].sortKey = "status";
Expand Down
7 changes: 7 additions & 0 deletions src/components/MainTable/MainTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ export type Props = PropsWithSpread<
* The rows to display in the table.
*/
rows?: MainTableRow[];
/**
* Whether to scroll to the top of the page when the page of a paginated
* table is changed.
*/
scrollToTop?: boolean;
/**
* Whether this table should be sortable.
*/
Expand Down Expand Up @@ -298,6 +303,7 @@ const MainTable = ({
paginate,
rows,
responsive,
scrollToTop,
sortable,
sortFunction,
hiddenCaption,
Expand Down Expand Up @@ -392,6 +398,7 @@ const MainTable = ({
currentPage={currentPage}
itemsPerPage={paginate}
paginate={setCurrentPage}
scrollToTop={scrollToTop}
style={{ marginTop: "1rem" }}
totalItems={rows.length}
/>
Expand Down
Loading