import React, { forwardRef, useState } from "react"; import cx from "classnames"; import { SubIcon, VibeComponent, VibeComponentProps } from "../../../types"; import styles from "./TableHeaderCell.module.scss"; import Icon from "../../Icon/Icon"; import IconButton from "../../IconButton/IconButton"; import Info from "../../Icon/Icons/components/Info"; import { ButtonType } from "../../Button/ButtonConstants"; import Text from "../../Text/Text"; import Flex from "../../Flex/Flex"; import { getAriaSort, getNextSortState, getSortIcon } from "../Table/tableHelpers"; import Tooltip from "../../Tooltip/Tooltip"; import { getTestId } from "../../../tests/test-ids-utils"; import { ComponentDefaultTestId } from "../../../tests/constants"; import { getStyle } from "../../../helpers/typesciptCssModulesHelper"; export interface ITableHeaderCellProps extends VibeComponentProps { title: string | React.ReactNode; icon?: SubIcon; infoContent?: string; sortState?: "asc" | "desc" | "none"; onSortClicked?: (direction: "asc" | "desc" | "none") => void; sortButtonAriaLabel?: string; sticky?: boolean; } const TableHeaderCell: VibeComponent = forwardRef( ( { id, className, "data-testid": dataTestId, title, onSortClicked, infoContent, icon, sortState = "none", sortButtonAriaLabel = "Sort", sticky }, ref ) => { const [isHovered, setIsHovered] = useState(false); const ariaSort = getAriaSort(sortState); const isSortActive = onSortClicked && ariaSort !== "none"; const shouldShowSortIcon = ariaSort !== "none" || isHovered; return (
setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} onFocus={() => setIsHovered(true)} onBlur={() => setIsHovered(false)} aria-sort={onSortClicked ? ariaSort : undefined} tabIndex={onSortClicked ? 0 : undefined} > {icon && } {typeof title === "string" ? ( {title} ) : ( title )} {infoContent && ( )} {onSortClicked && ( onSortClicked(getNextSortState(sortState))} /> )}
); } ); export default TableHeaderCell;