2023-09-21 13:25:19 +00:00
|
|
|
import { Tooltip, VegaIcon, VegaIconNames } from '@vegaprotocol/ui-toolkit';
|
|
|
|
import classNames from 'classnames';
|
2023-11-01 10:06:36 +00:00
|
|
|
import { forwardRef, type ReactNode, type HTMLAttributes } from 'react';
|
2024-01-02 15:50:04 +00:00
|
|
|
|
|
|
|
export const BORDER_COLOR = 'border-vega-clight-500 dark:border-vega-cdark-500';
|
|
|
|
export const GRADIENT =
|
|
|
|
'bg-gradient-to-b from-vega-clight-800 dark:from-vega-cdark-800 to-transparent';
|
2023-09-21 13:25:19 +00:00
|
|
|
|
|
|
|
type TableColumnDefinition = {
|
2023-11-01 10:06:36 +00:00
|
|
|
displayName?: ReactNode;
|
2023-09-21 13:25:19 +00:00
|
|
|
name: string;
|
|
|
|
tooltip?: string;
|
|
|
|
className?: string;
|
2024-01-31 14:21:29 +00:00
|
|
|
headerClassName?: string;
|
2024-01-02 15:50:04 +00:00
|
|
|
testId?: string;
|
2023-09-21 13:25:19 +00:00
|
|
|
};
|
|
|
|
|
2024-01-31 14:21:29 +00:00
|
|
|
type DataEntry = {
|
|
|
|
[key: TableColumnDefinition['name']]: ReactNode;
|
|
|
|
className?: string;
|
|
|
|
};
|
|
|
|
|
2023-09-21 13:25:19 +00:00
|
|
|
type TableProps = {
|
|
|
|
columns: TableColumnDefinition[];
|
2024-01-31 14:21:29 +00:00
|
|
|
data: DataEntry[];
|
2023-09-21 13:25:19 +00:00
|
|
|
noHeader?: boolean;
|
|
|
|
noCollapse?: boolean;
|
2024-01-31 14:21:29 +00:00
|
|
|
onRowClick?: (index: number) => void;
|
2023-09-21 13:25:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const INNER_BORDER_STYLE = `border-b ${BORDER_COLOR}`;
|
|
|
|
|
2023-10-23 14:57:18 +00:00
|
|
|
export const Table = forwardRef<
|
|
|
|
HTMLTableElement,
|
|
|
|
TableProps & HTMLAttributes<HTMLTableElement>
|
|
|
|
>(
|
|
|
|
(
|
|
|
|
{
|
|
|
|
columns,
|
|
|
|
data,
|
|
|
|
noHeader = false,
|
|
|
|
noCollapse = false,
|
|
|
|
className,
|
2024-01-31 14:21:29 +00:00
|
|
|
onRowClick,
|
2023-10-23 14:57:18 +00:00
|
|
|
...props
|
|
|
|
},
|
|
|
|
ref
|
|
|
|
) => {
|
|
|
|
const header = (
|
|
|
|
<thead className={classNames({ 'max-md:hidden': !noCollapse })}>
|
|
|
|
<tr>
|
2024-01-31 14:21:29 +00:00
|
|
|
{columns.map(({ displayName, name, tooltip, headerClassName }) => (
|
2023-10-23 14:57:18 +00:00
|
|
|
<th
|
|
|
|
key={name}
|
|
|
|
col-id={name}
|
|
|
|
className={classNames(
|
2024-01-02 15:50:04 +00:00
|
|
|
'px-5 py-3 text-xs text-vega-clight-100 dark:text-vega-cdark-100 font-normal',
|
2024-01-31 14:21:29 +00:00
|
|
|
INNER_BORDER_STYLE,
|
|
|
|
headerClassName
|
2023-10-23 14:57:18 +00:00
|
|
|
)}
|
|
|
|
>
|
2023-11-01 10:06:36 +00:00
|
|
|
<span className="flex flex-row items-center gap-2">
|
2023-10-23 14:57:18 +00:00
|
|
|
<span>{displayName}</span>
|
|
|
|
{tooltip ? (
|
|
|
|
<Tooltip description={tooltip}>
|
|
|
|
<button className="text-vega-clight-400 dark:text-vega-cdark-400 no-underline decoration-transparent w-[12px] h-[12px] inline-flex">
|
|
|
|
<VegaIcon size={12} name={VegaIconNames.INFO} />
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
) : null}
|
|
|
|
</span>
|
|
|
|
</th>
|
|
|
|
))}
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<table
|
|
|
|
ref={ref}
|
|
|
|
className={classNames(
|
|
|
|
'w-full',
|
2024-01-02 15:50:04 +00:00
|
|
|
'border-separate border rounded-md border-spacing-0 overflow-hidden',
|
2023-10-23 14:57:18 +00:00
|
|
|
BORDER_COLOR,
|
|
|
|
GRADIENT,
|
|
|
|
className
|
|
|
|
)}
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
{!noHeader && header}
|
|
|
|
<tbody>
|
2024-01-31 14:21:29 +00:00
|
|
|
{data.map((dataEntry, i) => (
|
2023-10-23 14:57:18 +00:00
|
|
|
<tr
|
|
|
|
key={i}
|
2024-01-31 14:21:29 +00:00
|
|
|
className={classNames(dataEntry['className'] as string, {
|
2023-10-23 14:57:18 +00:00
|
|
|
'max-md:flex flex-col w-full': !noCollapse,
|
|
|
|
})}
|
2024-01-31 14:21:29 +00:00
|
|
|
onClick={() => {
|
|
|
|
if (onRowClick) {
|
|
|
|
onRowClick(i);
|
|
|
|
}
|
|
|
|
}}
|
2023-10-23 14:57:18 +00:00
|
|
|
>
|
2024-01-02 15:50:04 +00:00
|
|
|
{columns.map(({ name, displayName, className, testId }, j) => (
|
2023-10-23 14:57:18 +00:00
|
|
|
<td
|
|
|
|
className={classNames(
|
2024-01-02 15:50:04 +00:00
|
|
|
'px-5 py-3',
|
2023-10-23 14:57:18 +00:00
|
|
|
{
|
|
|
|
'max-md:flex max-md:flex-col max-md:justify-between':
|
|
|
|
!noCollapse,
|
|
|
|
},
|
|
|
|
INNER_BORDER_STYLE,
|
|
|
|
{
|
|
|
|
'border-none': i === data.length - 1 && noCollapse,
|
|
|
|
'md:border-none': i === data.length - 1,
|
|
|
|
'max-md:border-none':
|
|
|
|
i === data.length - 1 && j === columns.length - 1,
|
|
|
|
},
|
|
|
|
className
|
2023-09-21 13:25:19 +00:00
|
|
|
)}
|
2023-10-23 14:57:18 +00:00
|
|
|
key={`${i}-${name}`}
|
|
|
|
>
|
|
|
|
{/** display column name in mobile view */}
|
2023-11-01 10:06:36 +00:00
|
|
|
{!noCollapse && !noHeader && displayName && (
|
|
|
|
<span
|
|
|
|
aria-hidden
|
|
|
|
className="px-0 font-mono text-xs md:hidden text-vega-clight-100 dark:text-vega-cdark-100"
|
|
|
|
>
|
|
|
|
{displayName}
|
|
|
|
</span>
|
|
|
|
)}
|
2024-01-31 14:21:29 +00:00
|
|
|
<span data-testid={`${testId || name}-${i}`}>
|
|
|
|
{dataEntry[name]}
|
|
|
|
</span>
|
2023-10-23 14:57:18 +00:00
|
|
|
</td>
|
|
|
|
))}
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Table.displayName = 'Table';
|