import { memo, useState } from 'react'; import { css } from 'ecij'; import { classnames } from './utils'; import type { CalculatedColumn, Direction, MapViewportColumns, Maybe, Position, ResizedWidth } from './types'; import type { DataGridProps } from './DataGrid'; import HeaderCell from './HeaderCell'; import { cell, cellFrozenBase } from './style/cell'; import { rowActiveClassname } from './style/row'; type SharedDataGridProps = Pick< DataGridProps, 'sortColumns' | 'onSortColumnsChange' | 'onColumnsReorder' >; export interface HeaderRowProps extends SharedDataGridProps { rowIdx: number; mapViewportColumns: MapViewportColumns; onColumnResize: (column: CalculatedColumn, width: ResizedWidth) => void; onColumnResizeEnd: () => void; activeCellIdx: number | undefined; setPosition: (position: Position) => void; shouldFocusGrid: boolean; direction: Direction; headerRowClass: Maybe; } const headerRow = css` @layer rdg.HeaderRow { display: contents; background-color: var(--rdg-header-background-color); font-weight: bold; & > .${cell} { /* Should have a higher value than 1 to show up above regular cells and the focus sink */ z-index: 2; position: sticky; } & > .${cellFrozenBase} { z-index: 3; } } `; export const headerRowClassname = `rdg-header-row ${headerRow}`; function HeaderRow({ headerRowClass, rowIdx, mapViewportColumns, onColumnResize, onColumnResizeEnd, onColumnsReorder, sortColumns, onSortColumnsChange, activeCellIdx, setPosition, shouldFocusGrid, direction }: HeaderRowProps) { const [draggedColumnKey, setDraggedColumnKey] = useState(); const isPositionOnRow = activeCellIdx === -1; const cells = mapViewportColumns( activeCellIdx, { type: 'HEADER' }, (column, isCellActive, colSpan, index) => ( key={column.key} column={column} colSpan={colSpan} rowIdx={rowIdx} isCellActive={isCellActive} onColumnResize={onColumnResize} onColumnResizeEnd={onColumnResizeEnd} onColumnsReorder={onColumnsReorder} onSortColumnsChange={onSortColumnsChange} sortColumns={sortColumns} setPosition={setPosition} shouldFocusGrid={shouldFocusGrid && index === 0} direction={direction} draggedColumnKey={draggedColumnKey} setDraggedColumnKey={setDraggedColumnKey} /> ) ); return (
{cells}
); } export default memo(HeaderRow) as ( props: HeaderRowProps ) => React.JSX.Element;