aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/tables/PageTable.tsx
blob: ee4824db6a360d62d1f996ea82ae781aaba2631d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { useEffect } from "react";
import { usePagination, useTable } from "react-table";
import { ScrollToTop } from "@/utilities";
import { usePageSize } from "@/utilities/storage";
import BaseTable from "./BaseTable";
import PageControl from "./PageControl";
import { useDefaultSettings } from "./plugins";
import { SimpleTableProps } from "./SimpleTable";

type Props<T extends object> = SimpleTableProps<T> & {
  autoScroll?: boolean;
};

const tablePlugins = [useDefaultSettings, usePagination];

export default function PageTable<T extends object>(props: Props<T>) {
  const { autoScroll = true, plugins, instanceRef, ...options } = props;

  const instance = useTable(
    options,
    useDefaultSettings,
    ...tablePlugins,
    ...(plugins ?? []),
  );

  // use page size as specified in UI settings
  instance.state.pageSize = usePageSize();

  if (instanceRef) {
    instanceRef.current = instance;
  }

  // Scroll to top when page is changed
  useEffect(() => {
    if (autoScroll) {
      ScrollToTop();
    }
  }, [instance.state.pageIndex, autoScroll]);

  return (
    <>
      <BaseTable
        {...options}
        {...instance}
        plugins={[...tablePlugins, ...(plugins ?? [])]}
      ></BaseTable>
      <PageControl
        count={instance.pageCount}
        index={instance.state.pageIndex}
        size={instance.state.pageSize}
        total={instance.rows.length}
        goto={instance.gotoPage}
      ></PageControl>
    </>
  );
}