All files / src/components table.tsx

100% Statements 33/33
100% Branches 0/0
100% Functions 8/8
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182          2x   29x             29x             29x                     2x   2x       16x         16x               2x   2x       23x         23x               2x   2x       4x                 4x               2x   2x       52x                 52x       2x   2x       31x                 31x       2x   2x       57x             57x       2x   2x       12x             12x               2x                        
import type { HTMLAttributes, TdHTMLAttributes, ThHTMLAttributes } from 'react';
import { forwardRef } from 'react';
 
import { cn } from '@/lib/utils';
 
const Table = forwardRef<HTMLTableElement, HTMLAttributes<HTMLTableElement>>(
  ({ className, ...props }, ref) => {
    const containerStyles = [
      // Layout
      'relative w-full',
      // Overflow handling
      'overflow-auto',
    ];
 
    const tableStyles = [
      // Layout
      'w-full',
      // Typography
      'caption-bottom text-sm',
    ];
 
    return (
      <div className={containerStyles.join(' ')}>
        <table
          ref={ref}
          className={cn(tableStyles.join(' '), className)}
          {...props}
        />
      </div>
    );
  }
);
Table.displayName = 'Table';
 
const TableHeader = forwardRef<
  HTMLTableSectionElement,
  HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => {
  const headerStyles = [
    // Row styling
    '[&_tr]:border-b',
  ];
 
  return (
    <thead
      ref={ref}
      className={cn(headerStyles.join(' '), className)}
      {...props}
    />
  );
});
TableHeader.displayName = 'TableHeader';
 
const TableBody = forwardRef<
  HTMLTableSectionElement,
  HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => {
  const bodyStyles = [
    // Row styling
    '[&_tr:last-child]:border-0',
  ];
 
  return (
    <tbody
      ref={ref}
      className={cn(bodyStyles.join(' '), className)}
      {...props}
    />
  );
});
TableBody.displayName = 'TableBody';
 
const TableFooter = forwardRef<
  HTMLTableSectionElement,
  HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => {
  const footerStyles = [
    // Appearance
    'border-t bg-muted/50',
    // Typography
    'font-medium',
    // Row styling
    '[&>tr]:last:border-b-0',
  ];
 
  return (
    <tfoot
      ref={ref}
      className={cn(footerStyles.join(' '), className)}
      {...props}
    />
  );
});
TableFooter.displayName = 'TableFooter';
 
const TableRow = forwardRef<
  HTMLTableRowElement,
  HTMLAttributes<HTMLTableRowElement>
>(({ className, ...props }, ref) => {
  const rowStyles = [
    // Appearance
    'border-b',
    // Interactions
    'transition-colors hover:bg-muted/50',
    // States
    'data-[state=selected]:bg-muted',
  ];
 
  return (
    <tr ref={ref} className={cn(rowStyles.join(' '), className)} {...props} />
  );
});
TableRow.displayName = 'TableRow';
 
const TableHead = forwardRef<
  HTMLTableCellElement,
  ThHTMLAttributes<HTMLTableCellElement>
>(({ className, ...props }, ref) => {
  const headStyles = [
    // Layout
    'h-12 px-4',
    // Typography
    'text-left align-middle font-medium text-muted-foreground',
    // Checkbox styling
    '[&:has([role=checkbox])]:pr-0',
  ];
 
  return (
    <th ref={ref} className={cn(headStyles.join(' '), className)} {...props} />
  );
});
TableHead.displayName = 'TableHead';
 
const TableCell = forwardRef<
  HTMLTableCellElement,
  TdHTMLAttributes<HTMLTableCellElement>
>(({ className, ...props }, ref) => {
  const cellStyles = [
    // Layout
    'p-4 align-middle',
    // Checkbox styling
    '[&:has([role=checkbox])]:pr-0',
  ];
 
  return (
    <td ref={ref} className={cn(cellStyles.join(' '), className)} {...props} />
  );
});
TableCell.displayName = 'TableCell';
 
const TableCaption = forwardRef<
  HTMLTableCaptionElement,
  HTMLAttributes<HTMLTableCaptionElement>
>(({ className, ...props }, ref) => {
  const captionStyles = [
    // Spacing
    'mt-4',
    // Typography
    'text-sm text-muted-foreground',
  ];
 
  return (
    <caption
      ref={ref}
      className={cn(captionStyles.join(' '), className)}
      {...props}
    />
  );
});
TableCaption.displayName = 'TableCaption';
 
export {
  Table,
  TableHeader,
  TableBody,
  TableFooter,
  TableHead,
  TableRow,
  TableCell,
  TableCaption,
};