import React from 'react'; import { useState } from 'react'; import { ColumnDefinitionType } from './Table'; import { ChevronUpDownIcon, ChevronDownIcon, ChevronUpIcon, } from '@heroicons/react/20/solid'; type TableHeaderProps = { columns: Array>; sortData: any; }; const styles = { core: `py-3.5 pl-3 pr-3 text-left text-sm font-semibold text-cu-black-900 `, thead: `bg-gray-100 `, sortable: `hover:cursor `, }; const TableHeader = ({ columns, sortData, }: TableHeaderProps) => { const [ascending, setAscending] = useState(true); const [active, setActive] = useState(''); const handleSortChange = (key: string) => { const activeColumn = key; let asc = true; if (key === active) { asc = !ascending; } setActive(activeColumn); setAscending(asc); sortData(activeColumn, asc); }; const headers = columns.map((column: any, index) => { const sortableStyles = column.sort.sortable ? 'hover:cursor-pointer' : 'hover:cursor-auto'; return ( column.sort.sortable ? handleSortChange(column.key) : undefined } aria-sort={ column.key === active && ascending ? 'descending' : column.key === active && !ascending ? 'ascending' : undefined } aria-label={column.sort?.sortable ? 'Sort by ' + column.key : undefined} > {column.sort?.sortable ? (
{column.header}
{column.key === active && ascending ? ( ) : column.key === active && !ascending ? ( ) : ( )}
) : ( column.header )} ); }); return ( {headers} ); }; export default TableHeader;