2023-12-17 20:19:28 +03:00
|
|
|
import { flexRender, Header, HeaderGroup, Table } from '@tanstack/react-table';
|
|
|
|
|
|
|
|
import SelectAll from './SelectAll';
|
|
|
|
import SortingIcon from './SortingIcon';
|
|
|
|
|
|
|
|
interface TableHeaderProps<TData> {
|
2023-12-28 14:04:44 +03:00
|
|
|
table: Table<TData>;
|
|
|
|
headPosition?: string;
|
|
|
|
enableRowSelection?: boolean;
|
|
|
|
enableSorting?: boolean;
|
2023-12-17 20:19:28 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
function TableHeader<TData>({ table, headPosition, enableRowSelection, enableSorting }: TableHeaderProps<TData>) {
|
2023-12-17 20:19:28 +03:00
|
|
|
return (
|
2023-12-28 14:04:44 +03:00
|
|
|
<thead
|
|
|
|
className={`clr-app shadow-border`}
|
|
|
|
style={{
|
|
|
|
top: headPosition,
|
|
|
|
position: 'sticky'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{table.getHeaderGroups().map((headerGroup: HeaderGroup<TData>) => (
|
|
|
|
<tr key={headerGroup.id}>
|
|
|
|
{enableRowSelection ? (
|
|
|
|
<th className='pl-3 pr-1'>
|
|
|
|
<SelectAll table={table} />
|
|
|
|
</th>
|
|
|
|
) : null}
|
|
|
|
{headerGroup.headers.map((header: Header<TData, unknown>) => (
|
|
|
|
<th
|
|
|
|
key={header.id}
|
|
|
|
colSpan={header.colSpan}
|
2023-12-30 19:43:24 +03:00
|
|
|
className='px-2 py-2 text-xs font-medium select-none whitespace-nowrap'
|
2023-12-28 14:04:44 +03:00
|
|
|
style={{
|
2023-12-30 19:43:24 +03:00
|
|
|
textAlign: 'center',
|
2023-12-28 14:04:44 +03:00
|
|
|
width: header.getSize(),
|
|
|
|
cursor: enableSorting && header.column.getCanSort() ? 'pointer' : 'auto'
|
|
|
|
}}
|
|
|
|
onClick={enableSorting ? header.column.getToggleSortingHandler() : undefined}
|
|
|
|
>
|
|
|
|
{!header.isPlaceholder ? (
|
2023-12-30 19:43:24 +03:00
|
|
|
<span className='inline-flex gap-1'>
|
2023-12-28 14:04:44 +03:00
|
|
|
{flexRender(header.column.columnDef.header, header.getContext())}
|
|
|
|
{enableSorting && header.column.getCanSort() ? <SortingIcon column={header.column} /> : null}
|
2023-12-30 19:43:24 +03:00
|
|
|
</span>
|
2023-12-28 14:04:44 +03:00
|
|
|
) : null}
|
|
|
|
</th>
|
|
|
|
))}
|
|
|
|
</tr>
|
2023-12-17 20:19:28 +03:00
|
|
|
))}
|
2023-12-28 14:04:44 +03:00
|
|
|
</thead>
|
|
|
|
);
|
2023-12-17 20:19:28 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
export default TableHeader;
|