table.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import * as React from "react"
  2. import { cn } from "@/lib/utils"
  3. const Table = React.forwardRef<
  4. HTMLTableElement,
  5. React.HTMLAttributes<HTMLTableElement>
  6. >(({ className, ...props }, ref) => (
  7. <div className="relative w-full overflow-auto">
  8. <table
  9. ref={ref}
  10. className={cn("w-full caption-bottom text-sm", className)}
  11. {...props}
  12. />
  13. </div>
  14. ))
  15. Table.displayName = "Table"
  16. const TableHeader = React.forwardRef<
  17. HTMLTableSectionElement,
  18. React.HTMLAttributes<HTMLTableSectionElement>
  19. >(({ className, ...props }, ref) => (
  20. <thead ref={ref} className={cn("[&_tr]:border-b", className)} {...props} />
  21. ))
  22. TableHeader.displayName = "TableHeader"
  23. const TableBody = React.forwardRef<
  24. HTMLTableSectionElement,
  25. React.HTMLAttributes<HTMLTableSectionElement>
  26. >(({ className, ...props }, ref) => (
  27. <tbody
  28. ref={ref}
  29. className={cn("[&_tr:last-child]:border-0", className)}
  30. {...props}
  31. />
  32. ))
  33. TableBody.displayName = "TableBody"
  34. const TableFooter = React.forwardRef<
  35. HTMLTableSectionElement,
  36. React.HTMLAttributes<HTMLTableSectionElement>
  37. >(({ className, ...props }, ref) => (
  38. <tfoot
  39. ref={ref}
  40. className={cn(
  41. "border-t bg-muted/50 font-medium [&>tr]:last:border-b-0",
  42. className
  43. )}
  44. {...props}
  45. />
  46. ))
  47. TableFooter.displayName = "TableFooter"
  48. const TableRow = React.forwardRef<
  49. HTMLTableRowElement,
  50. React.HTMLAttributes<HTMLTableRowElement>
  51. >(({ className, ...props }, ref) => (
  52. <tr
  53. ref={ref}
  54. className={cn(
  55. "border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted",
  56. className
  57. )}
  58. {...props}
  59. />
  60. ))
  61. TableRow.displayName = "TableRow"
  62. const TableHead = React.forwardRef<
  63. HTMLTableCellElement,
  64. React.ThHTMLAttributes<HTMLTableCellElement>
  65. >(({ className, ...props }, ref) => (
  66. <th
  67. ref={ref}
  68. className={cn(
  69. "h-12 px-4 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0",
  70. className
  71. )}
  72. {...props}
  73. />
  74. ))
  75. TableHead.displayName = "TableHead"
  76. const TableCell = React.forwardRef<
  77. HTMLTableCellElement,
  78. React.TdHTMLAttributes<HTMLTableCellElement>
  79. >(({ className, ...props }, ref) => (
  80. <td
  81. ref={ref}
  82. className={cn("p-4 align-middle [&:has([role=checkbox])]:pr-0", className)}
  83. {...props}
  84. />
  85. ))
  86. TableCell.displayName = "TableCell"
  87. const TableCaption = React.forwardRef<
  88. HTMLTableCaptionElement,
  89. React.HTMLAttributes<HTMLTableCaptionElement>
  90. >(({ className, ...props }, ref) => (
  91. <caption
  92. ref={ref}
  93. className={cn("mt-4 text-sm text-muted-foreground", className)}
  94. {...props}
  95. />
  96. ))
  97. TableCaption.displayName = "TableCaption"
  98. export {
  99. Table,
  100. TableHeader,
  101. TableBody,
  102. TableFooter,
  103. TableHead,
  104. TableRow,
  105. TableCell,
  106. TableCaption,
  107. }