You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bazarr/frontend/src/components/tables/PageTable.tsx

75 lines
1.7 KiB

import { ScrollToTop } from "@/utilities";
import { useEffect } from "react";
import { PluginHook, TableOptions, usePagination, useTable } from "react-table";
import BaseTable, { TableStyleProps, useStyleAndOptions } from "./BaseTable";
import PageControl from "./PageControl";
import { useDefaultSettings } from "./plugins";
type Props<T extends object> = TableOptions<T> &
TableStyleProps<T> & {
autoScroll?: boolean;
plugins?: PluginHook<T>[];
};
export default function PageTable<T extends object>(props: Props<T>) {
const { autoScroll, plugins, ...remain } = props;
const { style, options } = useStyleAndOptions(remain);
const allPlugins: PluginHook<T>[] = [useDefaultSettings, usePagination];
if (plugins) {
allPlugins.push(...plugins);
}
const instance = useTable(options, ...allPlugins);
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
// page
page,
canNextPage,
canPreviousPage,
pageCount,
gotoPage,
nextPage,
previousPage,
state: { pageIndex, pageSize },
} = instance;
// Scroll to top when page is changed
useEffect(() => {
if (autoScroll) {
ScrollToTop();
}
}, [pageIndex, autoScroll]);
return (
<>
<BaseTable
{...style}
headers={headerGroups}
rows={page}
prepareRow={prepareRow}
tableProps={getTableProps()}
tableBodyProps={getTableBodyProps()}
></BaseTable>
<PageControl
count={pageCount}
index={pageIndex}
size={pageSize}
total={rows.length}
canPrevious={canPreviousPage}
canNext={canNextPage}
previous={previousPage}
next={nextPage}
goto={gotoPage}
></PageControl>
</>
);
}