data-table-column-header.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { Button } from '@/components/ui/button.js';
  2. import {
  3. DropdownMenu,
  4. DropdownMenuContent,
  5. DropdownMenuItem,
  6. DropdownMenuTrigger,
  7. } from '@/components/ui/dropdown-menu.js';
  8. import {
  9. Dialog,
  10. DialogContent,
  11. DialogDescription,
  12. DialogHeader,
  13. DialogTitle,
  14. DialogTrigger,
  15. } from '@/components/ui/dialog.js';
  16. import { DataTableFilterDialog } from '@/framework/internal/data-table/data-table-filter-dialog.js';
  17. import { FieldInfo } from '@/framework/internal/document-introspection/get-document-structure.js';
  18. import { Trans } from '@lingui/react/macro';
  19. import { ColumnDef, HeaderContext } from '@tanstack/table-core';
  20. import { ArrowDown, ArrowUp, ArrowUpDown, EllipsisVertical, Filter } from 'lucide-react';
  21. import React from 'react';
  22. export interface DataTableColumnHeaderProps {
  23. customConfig: Partial<ColumnDef<any>>;
  24. headerContext: HeaderContext<any, any>;
  25. }
  26. export function DataTableColumnHeader({ headerContext, customConfig }: DataTableColumnHeaderProps) {
  27. const { column } = headerContext;
  28. const isSortable = column.getCanSort();
  29. const isFilterable = column.getCanFilter();
  30. const customHeader = customConfig.header;
  31. let display = column.id;
  32. if (typeof customHeader === 'function') {
  33. display = customHeader(headerContext);
  34. } else if (typeof customHeader === 'string') {
  35. display = customHeader;
  36. }
  37. const columSort = column.getIsSorted();
  38. const columnFilter = column.getFilterValue();
  39. const nextSort = columSort === 'asc' ? true : columSort === 'desc' ? undefined : false;
  40. return (
  41. <div className="flex items-center">
  42. <div>{display}</div>
  43. {isSortable && (
  44. <Button size="icon" variant="ghost" onClick={() => column.toggleSorting(nextSort)}>
  45. {columSort === 'desc' ? (
  46. <ArrowUp />
  47. ) : columSort === 'asc' ? (
  48. <ArrowDown />
  49. ) : (
  50. <ArrowUpDown className="opacity-50" />
  51. )}
  52. </Button>
  53. )}
  54. {isFilterable && (
  55. <Dialog>
  56. <DialogTrigger asChild>
  57. <Button size="icon" variant="ghost">
  58. <Filter className={columnFilter ? '' : 'opacity-50'} />
  59. </Button>
  60. </DialogTrigger>
  61. <DataTableFilterDialog column={column} />
  62. </Dialog>
  63. )}
  64. </div>
  65. );
  66. }