|
|
@ -1,9 +1,10 @@
|
|
|
|
import { ScrollToTop } from "@/utilities";
|
|
|
|
import { ScrollToTop } from "@/utilities";
|
|
|
|
import { useEffect, useRef } from "react";
|
|
|
|
import { useEffect } from "react";
|
|
|
|
import { TableInstance, usePagination } from "react-table";
|
|
|
|
import { usePagination, useTable } from "react-table";
|
|
|
|
|
|
|
|
import BaseTable from "./BaseTable";
|
|
|
|
import PageControl from "./PageControl";
|
|
|
|
import PageControl from "./PageControl";
|
|
|
|
import { useDefaultSettings } from "./plugins";
|
|
|
|
import { useDefaultSettings } from "./plugins";
|
|
|
|
import SimpleTable, { SimpleTableProps } from "./SimpleTable";
|
|
|
|
import { SimpleTableProps } from "./SimpleTable";
|
|
|
|
|
|
|
|
|
|
|
|
type Props<T extends object> = SimpleTableProps<T> & {
|
|
|
|
type Props<T extends object> = SimpleTableProps<T> & {
|
|
|
|
autoScroll?: boolean;
|
|
|
|
autoScroll?: boolean;
|
|
|
@ -12,33 +13,40 @@ type Props<T extends object> = SimpleTableProps<T> & {
|
|
|
|
const tablePlugins = [useDefaultSettings, usePagination];
|
|
|
|
const tablePlugins = [useDefaultSettings, usePagination];
|
|
|
|
|
|
|
|
|
|
|
|
export default function PageTable<T extends object>(props: Props<T>) {
|
|
|
|
export default function PageTable<T extends object>(props: Props<T>) {
|
|
|
|
const { autoScroll, plugins, ...remain } = props;
|
|
|
|
const { autoScroll = true, plugins, instanceRef, ...options } = props;
|
|
|
|
|
|
|
|
|
|
|
|
const instance = useRef<TableInstance<T> | null>(null);
|
|
|
|
const instance = useTable(
|
|
|
|
|
|
|
|
options,
|
|
|
|
|
|
|
|
useDefaultSettings,
|
|
|
|
|
|
|
|
...tablePlugins,
|
|
|
|
|
|
|
|
...(plugins ?? [])
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (instanceRef) {
|
|
|
|
|
|
|
|
instanceRef.current = instance;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Scroll to top when page is changed
|
|
|
|
// Scroll to top when page is changed
|
|
|
|
useEffect(() => {
|
|
|
|
useEffect(() => {
|
|
|
|
if (autoScroll) {
|
|
|
|
if (autoScroll) {
|
|
|
|
ScrollToTop();
|
|
|
|
ScrollToTop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [instance.current?.state.pageIndex, autoScroll]);
|
|
|
|
}, [instance.state.pageIndex, autoScroll]);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<>
|
|
|
|
<SimpleTable
|
|
|
|
<BaseTable
|
|
|
|
{...remain}
|
|
|
|
{...options}
|
|
|
|
instanceRef={instance}
|
|
|
|
{...instance}
|
|
|
|
plugins={[...tablePlugins, ...(plugins ?? [])]}
|
|
|
|
plugins={[...tablePlugins, ...(plugins ?? [])]}
|
|
|
|
></SimpleTable>
|
|
|
|
></BaseTable>
|
|
|
|
{instance.current && (
|
|
|
|
<PageControl
|
|
|
|
<PageControl
|
|
|
|
count={instance.pageCount}
|
|
|
|
count={instance.current.pageCount}
|
|
|
|
index={instance.state.pageIndex}
|
|
|
|
index={instance.current.state.pageIndex}
|
|
|
|
size={instance.state.pageSize}
|
|
|
|
size={instance.current.state.pageSize}
|
|
|
|
total={instance.rows.length}
|
|
|
|
total={instance.current.rows.length}
|
|
|
|
goto={instance.gotoPage}
|
|
|
|
goto={instance.current.gotoPage}
|
|
|
|
></PageControl>
|
|
|
|
></PageControl>
|
|
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
</>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|