import { useState } from 'react'; import { useTheme } from '@emotion/react'; import noop from 'utils/noop'; import { SCTable, Row, ColTitle } from './style'; const sortOrders = ['default', 'desc', 'asc'] as const; type SortFunction = (_a: any, _b: any) => number; type OnSort = ( _column: string, _sorting: 'default' | 'desc' | 'asc' ) => SortFunction; interface Column { /** value of this key `props.data[key]` will be rendered into `` */ key: string; /** for display as column title */ label: string; /** align column. `left|right|center|justify`
* @see https://www.w3schools.com/tags/att_td_align.asp */ align?: 'left' | 'center' | 'right' | 'justify'; /** column render function
* if not provide will simply render the value of that key
* * @param {any} columnData value of `props.data[i][key]`
* @param {object} allColumns `props.data[i]`
* @param {array} allRows `props.data`
*/ render?: ( _columnData: any, _allColumns: Record, _allRows: any[] ) => void; /** `` become sortable when provided, * get called when user click on the column title
* you can choose to take care of the sorting yourself * and update `props.data`
* optionally returns a sorting function that will get passed into * `Array.proptotype.sort()`.
* e.g. `(a, b) => b - a`
* * @param {string} column key
* @param {string} sorting order, one of: `default`, `desc`, `asc`
* @returns {func} [optional] this function will be use as the sorting function (frontend only)
*/ onSort?: OnSort; } interface Props { /** enable hover style for row */ hoverable?: boolean; /** enable alternate style for row */ alternate?: boolean; /** columns controls */ columns: Column[]; /** table data */ data: Record[]; /** the unique property (usually id) of individual object in `props.data` */ uniqueKey?: string; /** get called when row is clicked * @param {object} data contains row data `props.data[i]`
* @param {string} key the value of the `uniqueKey` set
*/ onRowClick?: (_data: Record, _key: string) => void; /** Rest Props */ [rest: string]: any; } interface State { sortBy: string; orderBy: number; sortMemo: Record; currentSortFunc: SortFunction | null; } const Table = ({ hoverable = false, alternate = true, columns, data, uniqueKey = 'id', onRowClick = noop, ...rest }: Props) => { const theme = useTheme(); const [{ sortBy, orderBy, sortMemo, currentSortFunc }, setState] = useState< State >({ sortBy: '', orderBy: 0, sortMemo: columns.reduce( (memo, { key }) => ({ ...memo, [key]: 0, }), {} ), currentSortFunc: null, }); const handleSort = (colKey: Column['key'], handler: OnSort) => { // next sort order, overflow will wrap back to default const nextSortOrder = sortOrders[sortMemo[colKey] + 1] ? sortMemo[colKey] + 1 : 0; setState(prev => ({ sortBy: colKey, orderBy: nextSortOrder, sortMemo: { ...prev.sortMemo, ...(sortBy ? { [sortBy]: 0 } : {}), [colKey]: nextSortOrder, }, currentSortFunc: handler.apply(null, [colKey, sortOrders[nextSortOrder]]), })); }; const renderRowCols = (cols = {}) => columns.map(({ key, render, align = theme?.rtl ? 'right' : 'left' }) => { const hasRenderFunc = typeof render === 'function'; const colData = cols[key]; return (
); }); const renderRows = rows => { let daRows = rows; if (currentSortFunc) { daRows = [...rows].sort(currentSortFunc); } return daRows.map((row, index) => ( onRowClick(row, row[uniqueKey])} > {renderRowCols(row)} )); }; return ( {columns.map( ({ label, key, onSort, align = theme?.rtl ? 'right' : 'left' }) => ( ) )} {renderRows(data)} ); }; export default Table;
{hasRenderFunc ? render(colData, cols, data) : colData}
handleSort(key, onSort) })} > {label}