import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; import useMeasure from "react-use-measure"; import { VirtualTableSize } from "../../VirtualTable"; import { getRowHeight } from "../../VirtualTable/common"; import { cn, RemoveCircleIcon, Tooltip } from "@firecms/ui"; import { ErrorBoundary } from "../../../components"; interface EntityTableCellProps { children: React.ReactNode; actions?: React.ReactNode; /** * The value is used only to check changes and force re-renders */ value?: any; disabled: boolean; saved?: boolean; error?: Error; allowScroll?: boolean; align: "right" | "left" | "center"; size: VirtualTableSize; disabledTooltip?: string; width: number; showExpandIcon?: boolean; removePadding?: boolean; fullHeight?: boolean; selected?: boolean; hideOverflow?: boolean; onSelect?: (cellRect: DOMRect | undefined) => void; } type TableCellInnerProps = { justifyContent: string; scrollable: boolean; faded: boolean; fullHeight: boolean; children: React.ReactNode; } const TableCellInner = ({ justifyContent, scrollable, faded, fullHeight, children }: TableCellInnerProps) => { return (
{children}
); }; export const EntityTableCell = React.memo( function EntityTableCell({ children, actions, size, selected, disabled, disabledTooltip, saved, error, align, allowScroll, removePadding, fullHeight, onSelect, width, hideOverflow = true, showExpandIcon = true }: EntityTableCellProps) { const [measureRef, bounds] = useMeasure(); const ref = useRef(null); const maxHeight = useMemo(() => getRowHeight(size), [size]); const [onHover, setOnHover] = useState(false); const [internalSaved, setInternalSaved] = useState(saved); const showError = !disabled && Boolean(error); useEffect(() => { if (saved) { setInternalSaved(true); } const handler = setTimeout(() => { setInternalSaved(false); }, 800); return () => { clearTimeout(handler); }; }, [saved]); let p = 0; if (!removePadding) { switch (size) { case "l": case "xl": p = 4; break; case "m": p = 2; break; case "s": default: p = 1; break; } } let justifyContent; switch (align) { case "right": justifyContent = "flex-end"; break; case "center": justifyContent = "center"; break; case "left": default: justifyContent = "flex-start"; } // const onClick = useCallback((event: React.MouseEvent) => { // if (event.detail === 3) { // doOpenPopup(); // } // }, [doOpenPopup]); const onSelectCallback = useCallback(() => { if (!onSelect) return; const cellRect = ref && ref?.current?.getBoundingClientRect(); if (disabled) { onSelect(undefined); } else if (!selected && cellRect) { onSelect(cellRect); } }, [ref, onSelect, selected, disabled]); const onFocus = useCallback((event: React.SyntheticEvent) => { onSelectCallback(); event.stopPropagation(); }, [onSelectCallback]); const isOverflowing = useMemo(() => { if (bounds) { return bounds.height > maxHeight; } return false; }, [bounds, maxHeight]); const isSelected = !showError && selected; const scrollable = !disabled && allowScroll && isOverflowing; const faded = !disabled && !allowScroll && isOverflowing; const setOnHoverTrue = useCallback(() => setOnHover(true), []); const setOnHoverFalse = useCallback(() => setOnHover(false), []); const borderClass = showError ? "border-red-500" : internalSaved ? "border-green-500" : isSelected ? "border-primary" : "border-transparent"; return (
{fullHeight && !faded && children} {(!fullHeight || faded) && {!fullHeight &&
{children}
}
}
{actions} {disabled && onHover && disabledTooltip &&
}
); }, (a, b) => { return a.error === b.error && a.value === b.value && a.disabled === b.disabled && a.saved === b.saved && a.allowScroll === b.allowScroll && a.align === b.align && a.size === b.size && a.disabledTooltip === b.disabledTooltip && a.width === b.width && a.showExpandIcon === b.showExpandIcon && a.removePadding === b.removePadding && a.fullHeight === b.fullHeight && a.selected === b.selected; }) as React.FunctionComponent;