|
| 1 | +import {useState} from 'react'; |
| 2 | + |
| 3 | +import type {Column, OnSort, SortOrderType, SortParams} from './types'; |
| 4 | +import {ASCENDING, DEFAULT_SORT_ORDER, DEFAULT_TABLE_ROW_HEIGHT, DESCENDING} from './constants'; |
| 5 | +import {b} from './shared'; |
| 6 | + |
| 7 | +// Icon similar to original DataTable icons to keep the same tables across diferent pages and tabs |
| 8 | +const SortIcon = ({order}: {order?: SortOrderType}) => { |
| 9 | + return ( |
| 10 | + <svg |
| 11 | + className={b('icon', {desc: order === DESCENDING})} |
| 12 | + viewBox="0 0 10 6" |
| 13 | + width="10" |
| 14 | + height="6" |
| 15 | + > |
| 16 | + <path fill="currentColor" d="M0 5h10l-5 -5z" /> |
| 17 | + </svg> |
| 18 | + ); |
| 19 | +}; |
| 20 | + |
| 21 | +interface ColumnSortIconProps { |
| 22 | + sortOrder?: SortOrderType; |
| 23 | + sortable?: boolean; |
| 24 | + defaultSortOrder: SortOrderType; |
| 25 | +} |
| 26 | + |
| 27 | +const ColumnSortIcon = ({sortOrder, sortable, defaultSortOrder}: ColumnSortIconProps) => { |
| 28 | + if (sortable) { |
| 29 | + return ( |
| 30 | + <span className={b('sort-icon', {shadow: !sortOrder})}> |
| 31 | + <SortIcon order={sortOrder || defaultSortOrder} /> |
| 32 | + </span> |
| 33 | + ); |
| 34 | + } else { |
| 35 | + return null; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +interface TableHeadProps<T> { |
| 40 | + columns: Column<T>[]; |
| 41 | + onSort?: OnSort; |
| 42 | + defaultSortOrder?: SortOrderType; |
| 43 | + rowHeight?: number; |
| 44 | +} |
| 45 | + |
| 46 | +export const TableHead = <T,>({ |
| 47 | + columns, |
| 48 | + onSort, |
| 49 | + defaultSortOrder = DEFAULT_SORT_ORDER, |
| 50 | + rowHeight = DEFAULT_TABLE_ROW_HEIGHT, |
| 51 | +}: TableHeadProps<T>) => { |
| 52 | + const [sortParams, setSortParams] = useState<SortParams>({}); |
| 53 | + |
| 54 | + const handleSort = (columnId: string) => { |
| 55 | + let newSortParams: SortParams = {}; |
| 56 | + |
| 57 | + // Order is changed in following order: |
| 58 | + // 1. Inactive Sort Order - gray icon of default order |
| 59 | + // 2. Active default order |
| 60 | + // 3. Active not default order |
| 61 | + if (columnId === sortParams.columnId) { |
| 62 | + if (sortParams.sortOrder && sortParams.sortOrder !== defaultSortOrder) { |
| 63 | + setSortParams(newSortParams); |
| 64 | + onSort?.(newSortParams); |
| 65 | + return; |
| 66 | + } |
| 67 | + const newSortOrder = sortParams.sortOrder === ASCENDING ? DESCENDING : ASCENDING; |
| 68 | + newSortParams = { |
| 69 | + sortOrder: newSortOrder, |
| 70 | + columnId: columnId, |
| 71 | + }; |
| 72 | + } else { |
| 73 | + newSortParams = { |
| 74 | + sortOrder: defaultSortOrder, |
| 75 | + columnId: columnId, |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + onSort?.(newSortParams); |
| 80 | + setSortParams(newSortParams); |
| 81 | + }; |
| 82 | + |
| 83 | + const renderTableColGroups = () => { |
| 84 | + return ( |
| 85 | + <colgroup> |
| 86 | + {columns.map((column) => { |
| 87 | + return <col key={column.name} style={{width: `${column.width}px`}} />; |
| 88 | + })} |
| 89 | + </colgroup> |
| 90 | + ); |
| 91 | + }; |
| 92 | + |
| 93 | + const renderTableHead = () => { |
| 94 | + return ( |
| 95 | + <thead className={b('head')}> |
| 96 | + <tr> |
| 97 | + {columns.map((column) => { |
| 98 | + const content = column.header ?? column.name; |
| 99 | + const sortOrder = |
| 100 | + sortParams.columnId === column.name ? sortParams.sortOrder : undefined; |
| 101 | + |
| 102 | + return ( |
| 103 | + <th |
| 104 | + key={column.name} |
| 105 | + className={b( |
| 106 | + 'th', |
| 107 | + {align: column.align, sortable: column.sortable}, |
| 108 | + column.className, |
| 109 | + )} |
| 110 | + style={{ |
| 111 | + height: `${rowHeight}px`, |
| 112 | + }} |
| 113 | + onClick={() => { |
| 114 | + handleSort(column.name); |
| 115 | + }} |
| 116 | + > |
| 117 | + <div className={b('head-cell')}> |
| 118 | + {content} |
| 119 | + <ColumnSortIcon |
| 120 | + sortOrder={sortOrder} |
| 121 | + sortable={column.sortable} |
| 122 | + defaultSortOrder={defaultSortOrder} |
| 123 | + /> |
| 124 | + </div> |
| 125 | + </th> |
| 126 | + ); |
| 127 | + })} |
| 128 | + </tr> |
| 129 | + </thead> |
| 130 | + ); |
| 131 | + }; |
| 132 | + |
| 133 | + return ( |
| 134 | + <> |
| 135 | + {renderTableColGroups()} |
| 136 | + {renderTableHead()} |
| 137 | + </> |
| 138 | + ); |
| 139 | +}; |
0 commit comments