DataTablePagination.tsx 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {
  2. ChevronLeftIcon,
  3. ChevronRightIcon,
  4. DoubleArrowLeftIcon,
  5. DoubleArrowRightIcon
  6. } from "@radix-ui/react-icons";
  7. import { Table } from "@tanstack/react-table";
  8. import { Button } from "@app/components/ui/button";
  9. import {
  10. Select,
  11. SelectContent,
  12. SelectItem,
  13. SelectTrigger,
  14. SelectValue
  15. } from "@app/components/ui/select";
  16. interface DataTablePaginationProps<TData> {
  17. table: Table<TData>;
  18. }
  19. export function DataTablePagination<TData>({
  20. table
  21. }: DataTablePaginationProps<TData>) {
  22. return (
  23. <div className="flex items-center justify-between text-muted-foreground">
  24. <div className="flex items-center space-x-2">
  25. <Select
  26. value={`${table.getState().pagination.pageSize}`}
  27. onValueChange={(value) => {
  28. table.setPageSize(Number(value));
  29. }}
  30. >
  31. <SelectTrigger className="h-8 w-[70px]">
  32. <SelectValue
  33. placeholder={table.getState().pagination.pageSize}
  34. />
  35. </SelectTrigger>
  36. <SelectContent side="top">
  37. {[10, 20, 30, 40, 50, 100].map((pageSize) => (
  38. <SelectItem key={pageSize} value={`${pageSize}`}>
  39. {pageSize}
  40. </SelectItem>
  41. ))}
  42. </SelectContent>
  43. </Select>
  44. </div>
  45. <div className="flex items-center space-x-3 lg:space-x-8">
  46. <div className="flex items-center justify-center text-sm font-medium">
  47. Page {table.getState().pagination.pageIndex + 1} of{" "}
  48. {table.getPageCount()}
  49. </div>
  50. <div className="flex items-center space-x-2">
  51. <Button
  52. variant="outline"
  53. className="hidden h-8 w-8 p-0 lg:flex"
  54. onClick={() => table.setPageIndex(0)}
  55. disabled={!table.getCanPreviousPage()}
  56. >
  57. <span className="sr-only">Go to first page</span>
  58. <DoubleArrowLeftIcon className="h-4 w-4" />
  59. </Button>
  60. <Button
  61. variant="outline"
  62. className="h-8 w-8 p-0"
  63. onClick={() => table.previousPage()}
  64. disabled={!table.getCanPreviousPage()}
  65. >
  66. <span className="sr-only">Go to previous page</span>
  67. <ChevronLeftIcon className="h-4 w-4" />
  68. </Button>
  69. <Button
  70. variant="outline"
  71. className="h-8 w-8 p-0"
  72. onClick={() => table.nextPage()}
  73. disabled={!table.getCanNextPage()}
  74. >
  75. <span className="sr-only">Go to next page</span>
  76. <ChevronRightIcon className="h-4 w-4" />
  77. </Button>
  78. <Button
  79. variant="outline"
  80. className="hidden h-8 w-8 p-0 lg:flex"
  81. onClick={() =>
  82. table.setPageIndex(table.getPageCount() - 1)
  83. }
  84. disabled={!table.getCanNextPage()}
  85. >
  86. <span className="sr-only">Go to last page</span>
  87. <DoubleArrowRightIcon className="h-4 w-4" />
  88. </Button>
  89. </div>
  90. </div>
  91. </div>
  92. );
  93. }