|
| 1 | +import React from 'react'; |
| 2 | +import cn from '@ably/ui/core/utils/cn'; |
| 3 | + |
| 4 | +// Table Root Component |
| 5 | +interface TableRootProps extends React.HTMLAttributes<HTMLDivElement> { |
| 6 | + children: React.ReactNode; |
| 7 | +} |
| 8 | + |
| 9 | +const TableRoot: React.FC<TableRootProps> = ({ children, className, ...props }) => { |
| 10 | + return ( |
| 11 | + <div className={cn('overflow-x-auto my-4 rounded-lg overflow-hidden', className)} {...props}> |
| 12 | + <table className="w-full border-separate border-spacing-0 text-left text-sm">{children}</table> |
| 13 | + </div> |
| 14 | + ); |
| 15 | +}; |
| 16 | + |
| 17 | +// Table Header Component |
| 18 | +interface TableHeaderProps extends React.HTMLAttributes<HTMLTableSectionElement> { |
| 19 | + children: React.ReactNode; |
| 20 | +} |
| 21 | + |
| 22 | +const TableHeader: React.FC<TableHeaderProps> = ({ children, className, ...props }) => { |
| 23 | + return ( |
| 24 | + <thead className={cn('bg-neutral-100 dark:bg-neutral-1200', className)} {...props}> |
| 25 | + {children} |
| 26 | + </thead> |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +// Table Body Component |
| 31 | +interface TableBodyProps extends React.HTMLAttributes<HTMLTableSectionElement> { |
| 32 | + children: React.ReactNode; |
| 33 | +} |
| 34 | + |
| 35 | +const TableBody: React.FC<TableBodyProps> = ({ children, className, ...props }) => { |
| 36 | + return ( |
| 37 | + <tbody className={className} {...props}> |
| 38 | + {children} |
| 39 | + </tbody> |
| 40 | + ); |
| 41 | +}; |
| 42 | + |
| 43 | +// Table Row Component |
| 44 | +interface TableRowProps extends React.HTMLAttributes<HTMLTableRowElement> { |
| 45 | + children: React.ReactNode; |
| 46 | +} |
| 47 | + |
| 48 | +const TableRow: React.FC<TableRowProps> = ({ children, className, ...props }) => { |
| 49 | + return ( |
| 50 | + <tr className={cn('first:border-t', className)} {...props}> |
| 51 | + {children} |
| 52 | + </tr> |
| 53 | + ); |
| 54 | +}; |
| 55 | + |
| 56 | +// Table Head Cell Component |
| 57 | +interface TableHeadProps extends React.ThHTMLAttributes<HTMLTableCellElement> { |
| 58 | + children: React.ReactNode; |
| 59 | +} |
| 60 | + |
| 61 | +const TableHead: React.FC<TableHeadProps> = ({ children, className, ...props }) => { |
| 62 | + return ( |
| 63 | + <th |
| 64 | + className={cn( |
| 65 | + 'px-4 py-3 text-left font-bold text-neutral-1000 dark:text-neutral-300', |
| 66 | + 'first:rounded-tl-lg last:rounded-tr-lg', |
| 67 | + 'border-t first:border-l last:border-r border-b border-neutral-300 dark:border-neutral-1000', |
| 68 | + className, |
| 69 | + )} |
| 70 | + {...props} |
| 71 | + > |
| 72 | + {children} |
| 73 | + </th> |
| 74 | + ); |
| 75 | +}; |
| 76 | + |
| 77 | +// Table Cell Component |
| 78 | +interface TableCellProps extends React.TdHTMLAttributes<HTMLTableCellElement> { |
| 79 | + children: React.ReactNode; |
| 80 | +} |
| 81 | + |
| 82 | +const TableCell: React.FC<TableCellProps> = ({ children, className, ...props }) => { |
| 83 | + return ( |
| 84 | + <td |
| 85 | + className={cn( |
| 86 | + 'px-4 py-3 font-medium text-neutral-1000 dark:text-neutral-300', |
| 87 | + 'first:border-l last:border-r border-b border-neutral-300 dark:border-neutral-1000', |
| 88 | + '[table>tbody:first-child_tr:first-child_&]:border-t', |
| 89 | + '[table>tbody:first-child_tr:first-child_&]:first:rounded-tl-lg [table>tbody:first-child_tr:first-child_&]:last:rounded-tr-lg', |
| 90 | + '[tr:last-child_&]:first:rounded-bl-lg [tr:last-child_&]:last:rounded-br-lg', |
| 91 | + className, |
| 92 | + )} |
| 93 | + {...props} |
| 94 | + > |
| 95 | + {children} |
| 96 | + </td> |
| 97 | + ); |
| 98 | +}; |
| 99 | + |
| 100 | +// Table Caption Component (optional) |
| 101 | +interface TableCaptionProps extends React.HTMLAttributes<HTMLTableCaptionElement> { |
| 102 | + children: React.ReactNode; |
| 103 | +} |
| 104 | + |
| 105 | +const TableCaption: React.FC<TableCaptionProps> = ({ children, className, ...props }) => { |
| 106 | + return ( |
| 107 | + <caption className={cn('mt-2 text-sm text-neutral-600 dark:text-neutral-700', className)} {...props}> |
| 108 | + {children} |
| 109 | + </caption> |
| 110 | + ); |
| 111 | +}; |
| 112 | + |
| 113 | +// Compound component pattern |
| 114 | +export const Table = Object.assign(TableRoot, { |
| 115 | + Root: TableRoot, |
| 116 | + Header: TableHeader, |
| 117 | + Body: TableBody, |
| 118 | + Row: TableRow, |
| 119 | + Head: TableHead, |
| 120 | + Cell: TableCell, |
| 121 | + Caption: TableCaption, |
| 122 | +}); |
| 123 | + |
| 124 | +export default Table; |
0 commit comments