import * as React from "react"; import { Icon } from "../display/icon"; import { TSortDirection } from "./tableTypes"; export interface ITableHeading { name: keyof T; cell: (name: keyof T) => React.ReactNode; sortBy?: (header: keyof T, direction: TSortDirection) => void; initSort?: TSortDirection; } export function TableHeading({ name, cell, sortBy, initSort, }: React.PropsWithChildren>) { const [sortByState, setSortByState] = React.useState("asc"); React.useEffect(() => { if (initSort && sortBy) { setSortByState(initSort); } }, []); const handleSortBy = () => { if (sortBy) { sortBy(name, sortByState); setSortByState(sortByState === "asc" ? "desc" : "asc"); } }; return ( {cell ? cell(name) : name} {sortBy && ( )} ); }