// @flow strict import * as React from 'react'; import type {ColorTypes} from '../../types/typography'; import classify from '../../utils/classify'; import type {ClassNameComponent} from '../../utils/makeClassNameComponent'; import {makeClassNameComponent} from '../../utils/makeClassNameComponent'; import {Checkbox} from '../Checkbox'; import {Icon} from '../Icon'; import {SubTitleExtraSmall, TEXT_COLORS} from '../Text'; import {BasicRow} from './DefaultRow'; import type {SortDirection} from './hooks'; import type {GenericObject} from './Table'; import css from './Table.module.css'; export type GenericHeaderItem = { label: React.Node, key: $Keys, className?: string, filterIcon?: React.Node, filtered?: boolean, subtext?: string, sortable?: boolean, headerIconClassName?: string, sticky?: boolean, render?: React.ComponentType<{ data: T, extras?: U, className?: string, selected?: boolean, disabled?: boolean, }>, }; export type GenericHeaderItems = GenericHeaderItem[]; export const BasicHeadCell: ClassNameComponent<'th'> = makeClassNameComponent( css.defaultHeaderCell, 'th', ); export const BasicTableHead: ClassNameComponent<'thead'> = makeClassNameComponent(css.defaultTableHead, 'thead'); export type TableHeaderProps = { className?: string, tableHeaderClassName?: string, sortable?: boolean, columns: GenericHeaderItems, handleSortClick?: (sortKey: $Keys) => mixed, sortKey?: $Keys, sortDirection?: SortDirection, checked?: 'true' | 'false' | 'mixed', handleCheckboxClick?: ({value: string, checked: boolean}) => mixed, disabled?: boolean, stickyHeader?: boolean, }; const SortIcon = ({ sortDirection, color, className, }: { sortDirection: SortDirection, className: string, color: ColorTypes, }) => { if (sortDirection === 'original') { return ( ); } else if (sortDirection === 'asc') { return ( ); } else if (sortDirection === 'desc') { return ( ); } }; export function DefaultTableHeader( props: TableHeaderProps, ): React.Node { const { className, sortable = false, columns, handleSortClick, sortKey, sortDirection = 'original', handleCheckboxClick, checked, disabled, stickyHeader, } = props; const tableHeaderCells = () => ( <> {columns.map((columnData, index) => { const { key, label, subtext, filterIcon, filtered, className, sticky, sortable: columnSortable = false, } = columnData; let headerClassName; const filterable = Boolean(filterIcon); if ((sortable && columnSortable) || filterable) { headerClassName = classify( css.defaultHeaderCellSortable, { [css.filtered]: filtered, [css.stickyHeaderCell]: sticky, }, css[sortDirection], className, ); } else { headerClassName = classify(className, { [css.stickyHeaderCell]: sticky, }); } const headCellClickHandler = () => { if (sortable && columnSortable && handleSortClick) { handleSortClick(key); } }; let columnSortDirection = 'original'; if (sortKey === key) { columnSortDirection = sortDirection; } return (
{label} {subtext && subtext}
{(sortable || filterIcon != null) && (
{columnSortable && ( )} {filterIcon != null && (
{filterIcon}
)}
)}
); })} ); return ( {handleCheckboxClick && (
)} {tableHeaderCells()}
); }