import React, {FC} from "react"; import {useMeasure} from "react-use"; import classNames from "classnames"; import {ColorOption, ColorOptionWithCustomTint, ColorWithCustomTint, request} from "./Color"; import {CardRenderer, CardRendererContext} from "./CardRenderers"; import {EmulatedButton} from "./EmulatedButton"; export type CardItemProps = ColorOptionWithCustomTint & { position: 'first' | 'last' | 'middle' | 'first last' | 'single', cardRenderer: CardRenderer, colors?: { background?: ColorWithCustomTint, icon?: { background?: ColorWithCustomTint, color?: ColorWithCustomTint, close?: { background?: ColorWithCustomTint, color?: ColorWithCustomTint, } }, }, verticalDistance?: string, context?: CardRendererContext, onClick?: React.MouseEventHandler | undefined, onRemoveClick?: React.MouseEventHandler | undefined, dynamicRemoveButton?: boolean, active?: boolean, customPadding?: string, topConnectorMarker?: React.ReactNode, topConnectorMarkerCenterOffset?: number, onMeasuredHeightChange?: (height: number) => void, className?: string, } export const CardItem: FC = ({ position, cardRenderer, color, colors, verticalDistance, context = '', onClick, onRemoveClick, active = false, dynamicRemoveButton, customPadding, topConnectorMarker, topConnectorMarkerCenterOffset, onMeasuredHeightChange, className, }) => { const [ref, {height}] = useMeasure(); const isFirst = position === 'first' const isMiddle = position === 'middle' const isLast = position === 'last' const isSingle = position === 'single' || (position.includes('first') && position.includes('last')) const isClickable = !!onClick const connectorPadding = 6 * 2 const connectorTopHeight = !isSingle && !isFirst ? (isMiddle ? (height + connectorPadding) / 2 : height / 2) : 0 const connectorBottomHeight = !isSingle && !isLast ? (isMiddle ? (height + connectorPadding) / 2 : height / 2) : 0 const resolvedTopConnectorMarkerCenterOffset = topConnectorMarkerCenterOffset ?? (connectorTopHeight / 2) if (!cardRenderer || typeof cardRenderer?.icon === 'undefined' || !cardRenderer.ComputedValue) { return <>; } const removeButtonRef = React.useRef(null); React.useEffect(() => { if (height > 0) { onMeasuredHeightChange?.(height) } }, [height, onMeasuredHeightChange]) return { // Check if the click target is the remove button or its children if (removeButtonRef.current && (removeButtonRef.current === event.target || removeButtonRef.current.contains(event.target as Node))) { return false; } }}> {cardRenderer.icon &&
{ // @ts-ignore onRemoveClick?.(event) }}> {!isSingle && connectorTopHeight > 0 &&
} {!isSingle && connectorBottomHeight > 0 &&
} {topConnectorMarker && connectorTopHeight > 0 &&
{topConnectorMarker}
} {dynamicRemoveButton && }
}
{cardRenderer.AboveComputedValue && } {cardRenderer.BelowComputedValue && }
{false && onRemoveClick && }
; }