import TableStatusText from './TableStatusText'; import StatusIcon from '../statusIcon'; import { Flag } from '@wise/art'; import React, { ImgHTMLAttributes } from 'react'; import { clsx } from 'clsx'; import Body from '../body'; import Money, { MoneyProps } from '../money'; import AvatarView, { AvatarViewProps } from '../avatarView'; interface TableCellTypeProp { type: 'leading' | 'text' | 'currency' | 'status' | 'media'; } // `Media`, `Leading` and `Status` cell types have 2 text fields: `primaryText` and `secondaryText` interface TableCellTextProps { primaryText?: string; secondaryText?: string; } // `Leading`, `Text` and `Currency` cells' types can have a status indicator with `error` or `success` values interface TableCellStatusProp { status?: 'error' | 'success'; } export interface TableCellMedia extends TableCellTypeProp, TableCellTextProps { media?: ImgHTMLAttributes; } export interface TableCellLeading extends TableCellTypeProp, TableCellTextProps, TableCellStatusProp { avatar?: Pick & { asset?: AvatarViewProps['children']; /** @deprecated Use `"imgSrc" instead. */ src?: AvatarViewProps['imgSrc']; }; } export interface TableCellText extends TableCellTypeProp, TableCellStatusProp { text?: string; } export interface TableCellCurrency extends TableCellTypeProp, TableCellStatusProp { primaryCurrency?: MoneyProps; secondaryCurrency?: MoneyProps; flag?: boolean; } export interface TableCellStatus extends TableCellTypeProp, TableCellTextProps { sentiment?: 'negative' | 'neutral' | 'positive' | 'warning' | 'pending'; } export interface TableCellType { cell?: TableCellLeading & TableCellText & TableCellCurrency & TableCellStatus & TableCellMedia; alignment?: 'left' | 'right'; } // These properties should be exported only on the lib level to prevent visual issues because of incorrect usage. export interface TableCellProps extends TableCellType { className?: string; colSpan?: number; children?: React.ReactNode; } const TableCell = ({ cell, alignment = 'left', className, colSpan, children }: TableCellProps) => { const getContentMedia = () => { let mediaContent = null; if (cell?.type === 'leading' && cell?.avatar) { mediaContent = ( {cell.avatar.asset} ); } if (cell?.type === 'currency' && cell?.primaryCurrency?.currency && cell?.flag !== false) { mediaContent = ( ); } if (cell?.type === 'status') { mediaContent = ; } if (cell?.type === 'media' && cell.media) { // eslint-disable-next-line jsx-a11y/alt-text mediaContent = ; } if (mediaContent) { return ( ); } }; const formatCurrencyValue = (currency?: MoneyProps) => { if (currency) { return ; } return ''; }; return ( {cell?.type === 'text' && cell?.text && ( )} {cell?.type && ['leading', 'currency', 'status', 'media'].includes(cell?.type) && (
{getContentMedia()}
{(cell?.primaryCurrency ?? cell?.primaryText) && ( )} {(cell?.secondaryCurrency ?? cell?.secondaryText) && ( {cell?.type === 'currency' ? formatCurrencyValue(cell?.secondaryCurrency) : cell?.secondaryText} )}
)} {children} ); }; export default TableCell;