import Actionables, { ActionType } from "../../molecules/actionables" import FilteringOptions, { FilteringOptionProps } from "./filtering-option" import React from "react" import SortingIcon from "../../fundamentals/icons/sorting-icon" import TableSearch from "./table-search" import clsx from "clsx" import { useNavigate } from "react-router-dom" type TableRowProps = React.HTMLAttributes & { forceDropdown?: boolean actions?: ActionType[] linkTo?: string clickable?: boolean } type TableCellProps = React.TdHTMLAttributes & { linkTo?: string name?: string } type SortingHeadCellProps = { onSortClicked: () => void sortDirection?: "ASC" | "DESC" setSortDirection: (string) => void } & React.HTMLAttributes export type TableProps = { filteringOptions?: FilteringOptionProps[] | React.ReactNode tableActions?: React.ReactNode enableSearch?: boolean searchClassName?: string immediateSearchFocus?: boolean searchPlaceholder?: string searchValue?: string containerClassName?: string handleSearch?: (searchTerm: string) => void } & React.HTMLAttributes type TableElement = React.ForwardRefExoticComponent & React.RefAttributes type TableType = { Head: TableElement> HeadRow: TableElement> HeadCell: TableElement> SortingHeadCell: TableElement Body: TableElement> Row: TableElement Cell: TableElement } & TableElement const Table = React.forwardRef( ( { className, children, tableActions, enableSearch, searchClassName, immediateSearchFocus, searchPlaceholder, searchValue, handleSearch, filteringOptions, containerClassName, ...props }, ref ) => { if (enableSearch && !handleSearch) { throw new Error("Table cannot enable search without a search handler") } return (
{filteringOptions ? (
{Array.isArray(filteringOptions) ? filteringOptions.map((fo, idx) => ) : filteringOptions}
) : ( )}
{tableActions &&
{tableActions}
} {enableSearch && ( )}
{children}
) } ) as TableType Table.Head = React.forwardRef< HTMLTableSectionElement, React.HTMLAttributes >(({ className, children, ...props }, ref) => ( {children} )) Table.HeadRow = React.forwardRef< HTMLTableRowElement, React.HTMLAttributes >(({ className, children, ...props }, ref) => ( {children} )) Table.HeadCell = React.forwardRef< HTMLTableCellElement, React.HTMLAttributes >(({ className, children, ...props }, ref) => ( {children} )) Table.SortingHeadCell = React.forwardRef< HTMLTableCellElement, SortingHeadCellProps >( ( { onSortClicked, sortDirection, setSortDirection, className, children, ...props }: SortingHeadCellProps, ref ) => { return (
{ e.preventDefault() if (!sortDirection) { setSortDirection("ASC") } else { if (sortDirection === "ASC") { setSortDirection("DESC") } else { setSortDirection(undefined) } } onSortClicked() }} > {children}
) } ) Table.Body = React.forwardRef< HTMLTableSectionElement, React.HTMLAttributes >(({ className, children, ...props }, ref) => ( {children} )) Table.Cell = React.forwardRef( ({ className, linkTo, children, ...props }, ref) => { const navigate = useNavigate() return ( { navigate(linkTo) e.stopPropagation() }, })} > {children} ) } ) Table.Row = React.forwardRef( ( { className, actions, children, linkTo, forceDropdown, clickable, ...props }, ref ) => { const navigate = useNavigate() return ( { navigate(linkTo) }, })} > {children} {actions && ( e.stopPropagation()} className="w-[32px]"> )} ) } ) export default Table