data-table-column-header.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { Button } from '@/vdb/components/ui/button.js';
  2. import { useDynamicTranslations } from '@/vdb/hooks/use-dynamic-translations.js';
  3. import { ColumnDef, HeaderContext } from '@tanstack/table-core';
  4. import { ArrowDown, ArrowUp, ArrowUpDown } from 'lucide-react';
  5. import { useMemo } from 'react';
  6. import { ColumnHeaderWrapper } from './column-header-wrapper.js';
  7. export interface DataTableColumnHeaderProps {
  8. customConfig: Partial<ColumnDef<any>>;
  9. headerContext: HeaderContext<any, any>;
  10. }
  11. export function DataTableColumnHeader({ headerContext, customConfig }: Readonly<DataTableColumnHeaderProps>) {
  12. const { column } = headerContext;
  13. const isSortable = column.getCanSort();
  14. const { getTranslatedFieldName } = useDynamicTranslations();
  15. const display = useMemo(() => {
  16. const customHeader = customConfig.header;
  17. let result = getTranslatedFieldName(column.id);
  18. if (typeof customHeader === 'function') {
  19. result = customHeader(headerContext);
  20. } else if (typeof customHeader === 'string') {
  21. result = customHeader;
  22. }
  23. return result;
  24. }, [customConfig.header, column.id, getTranslatedFieldName]);
  25. const columSort = column.getIsSorted();
  26. const nextSort = columSort === 'asc' ? true : columSort === 'desc' ? undefined : false;
  27. return (
  28. <ColumnHeaderWrapper columnId={column.id}>
  29. <div className="flex items-center">
  30. {isSortable && (
  31. <Button size="icon-sm" variant="ghost" onClick={() => column.toggleSorting(nextSort)}>
  32. {columSort === 'desc' ? (
  33. <ArrowUp />
  34. ) : columSort === 'asc' ? (
  35. <ArrowDown />
  36. ) : (
  37. <ArrowUpDown className="opacity-50" />
  38. )}
  39. </Button>
  40. )}
  41. <div>{display}</div>
  42. </div>
  43. </ColumnHeaderWrapper>
  44. );
  45. }