Table.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { ComponentPropsWithRef, forwardRef } from 'react'
  2. import { cn } from 'ui'
  3. import {
  4. Table as ShadcnTable,
  5. TableBody as ShadcnTableBody,
  6. TableCaption as ShadcnTableCaption,
  7. TableCell as ShadcnTableCell,
  8. TableFooter as ShadcnTableFooter,
  9. TableHead as ShadcnTableHead,
  10. TableHeader as ShadcnTableHeader,
  11. TableRow as ShadcnTableRow,
  12. } from 'ui/src/components/shadcn/ui/table'
  13. // Only create a custom component for Table with the added props
  14. export const Table = forwardRef<HTMLTableElement, ComponentPropsWithRef<typeof ShadcnTable>>(
  15. ({ className, onScroll, ...props }, ref) => (
  16. <ShadcnTable
  17. ref={ref}
  18. {...props}
  19. className={cn(className)}
  20. containerProps={{
  21. onScroll,
  22. className: 'h-full w-full overflow-auto caption-bottom text-sm [&>table]:table-fixed',
  23. }}
  24. />
  25. )
  26. )
  27. Table.displayName = 'Table'
  28. export const TableHeader = forwardRef<
  29. HTMLTableSectionElement,
  30. ComponentPropsWithRef<typeof ShadcnTableHeader>
  31. >(({ className, ...props }, ref) => (
  32. <ShadcnTableHeader ref={ref} className={cn('sticky top-0 z-1', className)} {...props} />
  33. ))
  34. TableHeader.displayName = 'TableHeader'
  35. export const TableBody = forwardRef<
  36. HTMLTableSectionElement,
  37. ComponentPropsWithRef<typeof ShadcnTableBody>
  38. >(({ className, ...props }, ref) => (
  39. <ShadcnTableBody
  40. ref={ref}
  41. {...props}
  42. className={cn(
  43. 'transition-colors outline-none focus-visible:outline-1 focus-visible:-outline-offset-1 focus-visible:outline-primary',
  44. className
  45. )}
  46. />
  47. ))
  48. TableBody.displayName = 'TableBody'
  49. export const TableFooter = forwardRef<
  50. HTMLTableSectionElement,
  51. ComponentPropsWithRef<typeof ShadcnTableFooter>
  52. >(({ className, ...props }, ref) => (
  53. <ShadcnTableFooter ref={ref} className={cn('text-primary-foreground', className)} {...props} />
  54. ))
  55. TableFooter.displayName = 'TableFooter'
  56. export const TableRow = forwardRef<
  57. HTMLTableRowElement,
  58. ComponentPropsWithRef<typeof ShadcnTableRow>
  59. >(({ className, ...props }, ref) => (
  60. <ShadcnTableRow
  61. ref={ref}
  62. className={cn('bg-background hover:bg-surface-100 border-b-0', className)}
  63. {...props}
  64. />
  65. ))
  66. TableRow.displayName = 'TableRow'
  67. export const TableHead = forwardRef<
  68. HTMLTableCellElement,
  69. ComponentPropsWithRef<typeof ShadcnTableHead>
  70. >(({ className, ...props }, ref) => (
  71. <ShadcnTableHead
  72. ref={ref}
  73. className={cn(
  74. 'text-xs! font-normal! text-foreground-lighter font-mono',
  75. 'relative select-none truncate [&>.cursor-col-resize]:last:opacity-0',
  76. 'text-muted-foreground h-9 px-2 text-left align-middle [&:has([role=checkbox])]:pr-0 *:[[role=checkbox]]:translate-y-[2px]',
  77. className
  78. )}
  79. {...props}
  80. />
  81. ))
  82. TableHead.displayName = 'TableHead'
  83. export const TableCell = forwardRef<
  84. HTMLTableCellElement,
  85. ComponentPropsWithRef<typeof ShadcnTableCell>
  86. >(({ className, ...props }, ref) => (
  87. <ShadcnTableCell
  88. ref={ref}
  89. className={cn('text-xs py-1! p-2 *:[[role=checkbox]]:translate-y-[2px] truncate', className)}
  90. {...props}
  91. />
  92. ))
  93. TableCell.displayName = 'TableCell'
  94. export const TableCaption = forwardRef<
  95. HTMLTableCaptionElement,
  96. ComponentPropsWithRef<typeof ShadcnTableCaption>
  97. >(({ className, ...props }, ref) => (
  98. <ShadcnTableCaption ref={ref} className={cn('text-sm', className)} {...props} />
  99. ))
  100. TableCaption.displayName = 'TableCaption'