|
| 1 | +/* eslint-disable react/jsx-key */ |
| 2 | +import clsx from 'clsx'; |
| 3 | +import { Icon } from 'components/Icon/Icon'; |
| 4 | +import { Pagination, PaginationLocalization } from 'components/Pagination/Pagination'; |
| 5 | +import React, { ReactElement, ReactNode, useMemo } from 'react'; |
| 6 | +import { |
| 7 | + Row, |
| 8 | + TableHeaderProps, |
| 9 | + TableOptions, |
| 10 | + useFlexLayout, |
| 11 | + usePagination, |
| 12 | + useSortBy, |
| 13 | + useTable |
| 14 | +} from 'react-table'; |
| 15 | + |
| 16 | +export interface TableProps<T extends Record<string, unknown>> extends TableOptions<T> { |
| 17 | + className?: string; |
| 18 | + paginationLocalization?: PaginationLocalization; |
| 19 | + onClick?: (row: Row<T>) => void; |
| 20 | +} |
| 21 | + |
| 22 | +export const Table = <T extends Record<string, unknown>>(props: TableProps<T>): ReactElement => { |
| 23 | + const { className, paginationLocalization, onClick, ...rest } = props; |
| 24 | + |
| 25 | + const defaultColumn = useMemo( |
| 26 | + () => ({ |
| 27 | + width: 150, |
| 28 | + minWidth: 100 |
| 29 | + }), |
| 30 | + [] |
| 31 | + ); |
| 32 | + |
| 33 | + const tableInstance = useTable( |
| 34 | + { ...rest, defaultColumn }, |
| 35 | + useSortBy, |
| 36 | + useFlexLayout, |
| 37 | + usePagination |
| 38 | + ); |
| 39 | + |
| 40 | + const { |
| 41 | + headerGroups, |
| 42 | + prepareRow, |
| 43 | + getTableProps, |
| 44 | + getTableBodyProps, |
| 45 | + page, |
| 46 | + state, |
| 47 | + gotoPage, |
| 48 | + rows |
| 49 | + } = tableInstance; |
| 50 | + |
| 51 | + const { pageIndex, pageSize } = state; |
| 52 | + |
| 53 | + return ( |
| 54 | + <> |
| 55 | + <div className={clsx('ui-overflow-auto', className)}> |
| 56 | + <table {...getTableProps()} className='ui-w-full'> |
| 57 | + <thead> |
| 58 | + {headerGroups.map((headerGroup) => ( |
| 59 | + <tr {...headerGroup.getHeaderGroupProps()}> |
| 60 | + {headerGroup.headers.map((column) => ( |
| 61 | + <TableHeader |
| 62 | + className={clsx( |
| 63 | + 'ui-text-gray-500 ui-select-none ui-p-2 ui-tg-caption-bold lg:ui-tg-content-bold ui-transition-colors', |
| 64 | + column.columnClass || 'ui-text-right', |
| 65 | + { |
| 66 | + 'hover:ui-text-primary': column.canSort |
| 67 | + } |
| 68 | + )} |
| 69 | + {...column.getHeaderProps(column.getSortByToggleProps())} |
| 70 | + > |
| 71 | + <> |
| 72 | + {column.render('Header')} |
| 73 | + <span> |
| 74 | + {column.isSorted && ( |
| 75 | + <Icon |
| 76 | + className='ui-text-xs ui-ml-1' |
| 77 | + name={column.isSortedDesc ? 'Top' : 'Bottom'} |
| 78 | + /> |
| 79 | + )} |
| 80 | + </span> |
| 81 | + </> |
| 82 | + </TableHeader> |
| 83 | + ))} |
| 84 | + </tr> |
| 85 | + ))} |
| 86 | + </thead> |
| 87 | + |
| 88 | + <tbody {...getTableBodyProps()}> |
| 89 | + {page.map((row) => { |
| 90 | + prepareRow(row); |
| 91 | + return ( |
| 92 | + <tr |
| 93 | + onClick={() => onClick?.(row)} |
| 94 | + {...row.getRowProps()} |
| 95 | + className={clsx( |
| 96 | + 'ui-text-white ui-border-t last:ui-border-b ui-border-solid ui-border-gray-700', |
| 97 | + { |
| 98 | + 'ui-cursor-pointer': !!onClick |
| 99 | + } |
| 100 | + )} |
| 101 | + > |
| 102 | + {row.cells.map((cell) => ( |
| 103 | + <td |
| 104 | + className={clsx( |
| 105 | + 'ui-p-2 ui-tg-caption lg:ui-tg-content', |
| 106 | + cell.column.cellClass || 'ui-text-right' |
| 107 | + )} |
| 108 | + {...cell.getCellProps()} |
| 109 | + > |
| 110 | + <span className='ui-inline-flex ui-items-center ui-h-full'> |
| 111 | + {cell.render('Cell') as ReactNode} |
| 112 | + </span> |
| 113 | + </td> |
| 114 | + ))} |
| 115 | + </tr> |
| 116 | + ); |
| 117 | + })} |
| 118 | + </tbody> |
| 119 | + </table> |
| 120 | + </div> |
| 121 | + <Pagination |
| 122 | + className='ui-mt-6 ui-w-full' |
| 123 | + gotoPage={gotoPage} |
| 124 | + length={rows.length} |
| 125 | + localization={paginationLocalization} |
| 126 | + pageIndex={pageIndex} |
| 127 | + pageSize={pageSize} |
| 128 | + /> |
| 129 | + </> |
| 130 | + ); |
| 131 | +}; |
| 132 | + |
| 133 | +interface HeaderProps extends TableHeaderProps { |
| 134 | + children: React.ReactNode; |
| 135 | +} |
| 136 | + |
| 137 | +const TableHeader: React.FC<HeaderProps> = ({ children, ...props }) => { |
| 138 | + return <th {...props}>{children}</th>; |
| 139 | +}; |
0 commit comments