mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-24 17:51:14 +02:00
chore: scroll to the top of table when changing page
Makes it so that if you change the page in a paginated table, you'll scroll to the top of the table. Makes the experience more user friendly. Will only scroll if the top of the table isn't in view already. Finally: should we also scroll when you change the number of result rows per page?
This commit is contained in:
parent
fabf76e12c
commit
b15005895b
@ -1,4 +1,5 @@
|
|||||||
import { TableBody, TableRow, TableHead } from '@mui/material';
|
import { TableBody, TableRow, TableHead } from '@mui/material';
|
||||||
|
import { useRef } from 'react';
|
||||||
import { Table } from 'component/common/Table/Table/Table';
|
import { Table } from 'component/common/Table/Table/Table';
|
||||||
import {
|
import {
|
||||||
type Header,
|
type Header,
|
||||||
@ -59,10 +60,22 @@ export const PaginatedTable = <T extends object>({
|
|||||||
totalItems?: number;
|
totalItems?: number;
|
||||||
}) => {
|
}) => {
|
||||||
const { pagination } = tableInstance.getState();
|
const { pagination } = tableInstance.getState();
|
||||||
|
const tableRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
const scrollToTopIfNeeded = () => {
|
||||||
|
if (!tableRef.current) return;
|
||||||
|
|
||||||
|
const rect = tableRef.current.getBoundingClientRect();
|
||||||
|
const isTableTopVisible = rect.top >= 0;
|
||||||
|
|
||||||
|
if (!isTableTopVisible) {
|
||||||
|
tableRef.current.scrollIntoView({ block: 'start' });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<TableContainer>
|
<TableContainer ref={tableRef}>
|
||||||
<Table>
|
<Table>
|
||||||
<TableHead>
|
<TableHead>
|
||||||
{tableInstance.getHeaderGroups().map((headerGroup) => (
|
{tableInstance.getHeaderGroups().map((headerGroup) => (
|
||||||
@ -110,24 +123,35 @@ export const PaginatedTable = <T extends object>({
|
|||||||
totalItems={totalItems}
|
totalItems={totalItems}
|
||||||
pageIndex={pagination.pageIndex}
|
pageIndex={pagination.pageIndex}
|
||||||
pageSize={pagination.pageSize}
|
pageSize={pagination.pageSize}
|
||||||
fetchNextPage={() =>
|
fetchNextPage={() => {
|
||||||
tableInstance.setPagination({
|
tableInstance.setPagination({
|
||||||
pageIndex: pagination.pageIndex + 1,
|
pageIndex: pagination.pageIndex + 1,
|
||||||
pageSize: pagination.pageSize,
|
pageSize: pagination.pageSize,
|
||||||
})
|
});
|
||||||
}
|
scrollToTopIfNeeded();
|
||||||
fetchPrevPage={() =>
|
}}
|
||||||
|
fetchPrevPage={() => {
|
||||||
tableInstance.setPagination({
|
tableInstance.setPagination({
|
||||||
pageIndex: pagination.pageIndex - 1,
|
pageIndex: pagination.pageIndex - 1,
|
||||||
pageSize: pagination.pageSize,
|
pageSize: pagination.pageSize,
|
||||||
})
|
});
|
||||||
}
|
scrollToTopIfNeeded();
|
||||||
setPageLimit={(pageSize) =>
|
}}
|
||||||
|
setPageLimit={(pageSize) => {
|
||||||
tableInstance.setPagination({
|
tableInstance.setPagination({
|
||||||
pageIndex: 0,
|
pageIndex: 0,
|
||||||
pageSize,
|
pageSize,
|
||||||
})
|
});
|
||||||
}
|
|
||||||
|
if (
|
||||||
|
pagination.pageIndex > 0 ||
|
||||||
|
pagination.pageSize > pageSize
|
||||||
|
) {
|
||||||
|
// scroll to table top unless you are on the
|
||||||
|
// first page and increasing the page size.
|
||||||
|
scrollToTopIfNeeded();
|
||||||
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
Loading…
Reference in New Issue
Block a user