import React, { useContext } from "react" import classnames from "classnames" import { flexRender, Header } from "@tanstack/react-table" import { GenericObject } from "../../types" import { GlobalProps } from "../../utilities/globalProps" import Flex from "../../pb_flex/_flex" import { SortIconButton } from "./SortIconButton" import { ToggleIconButton } from "./ToggleIconButton" import { isChrome } from "../Utilities/BrowserCheck" import AdvancedTableContext from "../Context/AdvancedTableContext" type TableHeaderCellProps = { enableSorting?: boolean enableToggleExpansion?: "all" | "header" | "none" handleExpandOrCollapse?: () => void header?: Header headerChildren?: React.ReactNode | React.ReactNode[] isPinnedLeft?: boolean loading?: boolean sortIcon?: string | string[] } & GlobalProps export const TableHeaderCell = ({ enableSorting, enableToggleExpansion, handleExpandOrCollapse, header, headerChildren, isPinnedLeft = false, loading, sortIcon, }: TableHeaderCellProps) => { const { sortControl, responsive } = useContext(AdvancedTableContext) const toggleSortButton = (event: React.SyntheticEvent) => { if (sortControl) { const sortIsDesc = header.column.getIsSorted() === "desc" sortIsDesc ? sortControl.onChange({ desc: true }) : sortControl.onChange({ desc: false }) } else { header.column.getToggleSortingHandler()(event) } } const cellClassName = classnames("table-header-cells", `${isChrome() ? "chrome-styles" : ""}`, `${enableSorting ? "table-header-cells-active" : ""}`, { 'pinned-left': responsive === "scroll" && isPinnedLeft }, ) const cellId = `${loading ? `loading-${header.id}` : `${header.id}` }` const isToggleExpansionEnabledLoading = header.index === 0 && loading && (enableToggleExpansion === "all" || "header") && enableToggleExpansion !== "none" const isToggleExpansionEnabled = header.index === 0 && !loading && (enableToggleExpansion === "all" || "header") && enableToggleExpansion !== "none" return ( {header.isPlaceholder ? null : headerChildren && header.index === 0 ? ( {headerChildren}
{flexRender(header.column.columnDef.header, header.getContext())}
) : ( {isToggleExpansionEnabled && ( )} {isToggleExpansionEnabledLoading &&(
)} toggleSortButton(event), onKeyDown: (event: React.KeyboardEvent) => { if (event.key === "Enter") { toggleSortButton(event) } }, tabIndex: 0, }, })} justify={header.index === 0 && enableSorting ? "between" : "none"} paddingLeft={enableSorting ? "xxs" : "xs"} >
{flexRender(header.column.columnDef.header, header.getContext())}
{header.index === 0 && header.column.getCanSort() && enableSorting && (loading ? (
) : ( ))} )} ) }