import { capitalizeFirstLetter } from '~/lib/util' import classNames from '~/lib/classNames' import LoadingSpinner from '~/components/LoadingSpinner' import React, { RefObject } from 'react' export type StyledTableProps = { loading?: boolean tableProps?: React.HTMLAttributes tableRowStyle?: React.CSSProperties tableBodyClassName?: string tableBodyStyle?: React.CSSProperties data?: T[] noDataText?: string onRowClick?: (record: T) => void columns?: { accessor: keyof T title?: React.ReactNode render?: (record: T, index: number) => React.ReactNode className?: string }[] className?: string rowLines?: boolean ref?: RefObject containerProps?: React.HTMLAttributes compact?: boolean } function StyledTable({ loading = false, tableProps = {}, tableRowStyle = {}, tableBodyClassName = '', tableBodyStyle = {}, data = [], noDataText = 'No records found', onRowClick, columns = [], className = '', ref, containerProps = {}, rowLines = true, compact = false, }: StyledTableProps) { const { className: tableClassName, ...restTableProps } = tableProps const leftPadding = compact ? 'pl-2' : 'pl-4 sm:pl-6' return (
{columns.map((column, index) => ( ))} {!loading && data.length !== 0 && data.map((record, recordIdx) => ( onRowClick?.(record)} style={{ ...tableRowStyle, height: 'height' in record ? record.height : 'auto', transform: 'translateY' in record ? 'translateY(' + record.transformY + 'px)' : undefined, }} className={classNames( rowLines ? 'border-b border-gray-200' : '', onRowClick ? `cursor-pointer hover:bg-gray-100 ` : '' )} > {columns.map((column, index) => ( ))} ))} {!loading && data.length === 0 && ( )} {loading && ( )}
{column.title ?? capitalizeFirstLetter(column.accessor.toString())}
{column.render ? column.render(record, index) : (record[column.accessor] as React.ReactNode)}
{noDataText}
) } export default StyledTable