/* eslint-disable no-nested-ternary */ import { Box } from './Box'; import { Divider } from './Divider'; import { GeneratedPropTypes } from '../types'; import { Icon } from './Icon'; import { Spinner } from './Spinner'; import { Text } from './Typography'; import { baseShadow, colors } from '../theme'; import { generateProps } from 'styled-gen'; import React from 'react'; import styled from 'styled-components'; // #region ====== style === const TableWrapper = styled.div` border: 0.063rem solid ${colors.g200}; border-radius: 0.5rem; overflow: hidden; ${baseShadow}; ${generateProps}; `; const TableOverflow = styled.div<{ isLoading: boolean; noResults: boolean }>` display: block; overflow-x: ${({ isLoading, noResults }) => (isLoading || noResults ? 'hidden' : 'auto')}; `; const BaseTable = styled.table` background: ${colors.n01}; width: 100%; `; const TableHeader = styled.thead` background: ${colors.g50}; `; const TableBody = styled.tbody` & tr:last-child { & td { border-bottom: 0; } } `; const TableRow = styled.tr``; const THColumn = styled.th` border-bottom: 0.063rem solid ${colors.g200}; padding: 0.75rem 1.5rem; text-align: left; vertical-align: top; & p { word-break: unset; } & .disabled { opacity: 0.5; pointer-events: none; } ${generateProps}; `; const TBColumn = styled.td` border-bottom: 0.063rem solid ${colors.g200}; padding: 1.625rem 1.5rem; text-align: left; vertical-align: middle; `; // #endregion === style === export type ColumnProps = { title?: string; value?: string; width?: string | number; minWidth?: string | number; sortable?: boolean; render?: Function; }; export type TableProps = { columns: ColumnProps[]; isLoading: boolean; rows: any; noResults: string; pagination?: any; handleSort?: any; sortKey?: { key: string; sortDesc: boolean; }; } & GeneratedPropTypes; export const Table: React.FC = props => { const { columns, isLoading, rows, noResults, pagination, handleSort, sortKey, ...forwardProps } = props; const renderColumnTitle = (column: ColumnProps) => { if (column.sortable) { return ( !isLoading && rows?.length > 0 && handleSort(column.value)} > {column.title} ); } return ( {column.title} ); }; return ( {columns?.length > 0 && ( {columns.map((column: ColumnProps, index: number) => ( {renderColumnTitle(column)} ))} )} {!isLoading && rows?.length > 0 && ( {rows.map((row: any, index: number) => ( {columns.map((column: ColumnProps, indexColumn: number) => ( {!!column.render ? column.render(row) : column.value ? row[column.value] : null} ))} ))} )} {isLoading && ( )} {!isLoading && rows?.length === 0 && !!noResults && ( {noResults} )} {!!pagination && pagination?.props?.pageCount > 1 && rows?.length > 0 && ( <> {pagination} )} ); };