import { memo, type MouseEvent } from 'react'; import { css } from 'ecij'; import { useRovingTabIndex } from './hooks'; import { createCellEvent, getCellClassname, getCellStyle, isCellEditableUtil } from './utils'; import type { CellMouseEventHandler, RenderCellProps, SetActivePositionOptions } from './types'; const cellDraggedOver = css` @layer rdg.Cell { background-color: #ccccff; @media (forced-colors: active) { background-color: Mark; } } `; const cellDraggedOverClassname = `rdg-cell-dragged-over ${cellDraggedOver}`; function Cell({ column, colSpan, isCellActive, isDraggedOver, row, rowIdx, className, onMouseDown, onCellMouseDown, onClick, onCellClick, onDoubleClick, onCellDoubleClick, onContextMenu, onCellContextMenu, onRowChange, setActivePosition, style, ...props }: RenderCellProps) { const { tabIndex, childTabIndex, onFocus } = useRovingTabIndex(isCellActive); const { cellClass } = column; className = getCellClassname( column, isDraggedOver && cellDraggedOverClassname, typeof cellClass === 'function' ? cellClass(row) : cellClass, className ); const isEditable = isCellEditableUtil(column, row); function setActivePositionWrapper(options?: SetActivePositionOptions) { setActivePosition({ idx: column.idx, rowIdx }, options); } function handleMouseEvent( event: React.MouseEvent, eventHandler: CellMouseEventHandler ) { let eventHandled = false; if (eventHandler) { const cellEvent = createCellEvent(event); eventHandler({ rowIdx, row, column, setActivePosition: setActivePositionWrapper }, cellEvent); eventHandled = cellEvent.isGridDefaultPrevented(); } return eventHandled; } function handleMouseDown(event: MouseEvent) { onMouseDown?.(event); if (!handleMouseEvent(event, onCellMouseDown)) { // activate the cell if the event is not prevented setActivePositionWrapper(); } } function handleClick(event: MouseEvent) { onClick?.(event); handleMouseEvent(event, onCellClick); } function handleDoubleClick(event: MouseEvent) { onDoubleClick?.(event); if (!handleMouseEvent(event, onCellDoubleClick)) { // go into edit mode if the event is not prevented setActivePositionWrapper({ enableEditor: true }); } } function handleContextMenu(event: MouseEvent) { onContextMenu?.(event); handleMouseEvent(event, onCellContextMenu); } function handleRowChange(newRow: R) { onRowChange(column, rowIdx, newRow); } return (
1 ? colSpan : undefined} aria-selected={isCellActive} aria-readonly={!isEditable || undefined} tabIndex={tabIndex} className={className} style={{ ...getCellStyle(column, colSpan), ...style }} onClick={handleClick} onMouseDown={handleMouseDown} onDoubleClick={handleDoubleClick} onContextMenu={handleContextMenu} onFocus={onFocus} {...props} > {column.renderCell({ column, row, rowIdx, isCellEditable: isEditable, tabIndex: childTabIndex, onRowChange: handleRowChange })}
); } const MemoCell = memo(Cell) as (props: RenderCellProps) => React.JSX.Element; export { MemoCell as Cell }; export function defaultRenderCell(key: React.Key, props: RenderCellProps) { return ; }