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