import React from 'react'; import {Skeleton} from '@gravity-ui/uikit'; import {KEYBOARD_FOCUSED_ROW_CLASS_NAME} from '../TableKeyboardNavigation/utils'; import {DEFAULT_ALIGN, DEFAULT_RESIZEABLE} from './constants'; import {b} from './shared'; import type {AlignType, Column, GetRowClassName, OnRowClick} from './types'; import {KeyboardRowContext} from './useKeyboardNavigation'; import {typedMemo} from './utils'; interface TableCellProps { height: number; width?: number; align?: AlignType; children: React.ReactNode; className?: string; resizeable?: boolean; } const TableRowCell = ({ children, className, height, width, align = DEFAULT_ALIGN, resizeable, }: TableCellProps) => { return ( maxWidth: resizeable ? `${width}px` : undefined, }} > {children} ); }; interface LoadingTableRowProps { columns: Column[]; height: number; } export const LoadingTableRow = typedMemo(function ({columns, height}: LoadingTableRowProps) { return ( {columns.map((column) => { const resizeable = column.resizeable ?? DEFAULT_RESIZEABLE; return ( ); })} ); }); interface TableRowProps { columns: Column[]; row: T; rowIndex?: number; height: number; getRowClassName?: GetRowClassName; onRowClick?: OnRowClick; } const TableRowCells = typedMemo(function TableRowCells({ columns, row, height, }: Pick, 'columns' | 'row' | 'height'>) { return ( {columns.map((column) => { const resizeable = column.resizeable ?? DEFAULT_RESIZEABLE; return ( {column.render({row})} ); })} ); }); const TableRowView = typedMemo(function TableRowView({ row, rowIndex, columns, getRowClassName, height, onRowClick, keyboardFocused, }: TableRowProps & {keyboardFocused: boolean}) { const additionalClassName = [ getRowClassName?.(row), keyboardFocused ? KEYBOARD_FOCUSED_ROW_CLASS_NAME : undefined, ] .filter(Boolean) .join(' '); const rowClickable = typeof onRowClick === 'function'; const handleClick: React.MouseEventHandler = (event) => { if (!rowClickable || event.defaultPrevented) { return; } onRowClick?.(row, event); }; return ( ); }); export function TableRow(props: TableRowProps) { const focusedIndex = React.useContext(KeyboardRowContext); const keyboardFocused = props.rowIndex !== undefined && props.rowIndex === focusedIndex; return ; } interface EmptyTableRowProps { columns: Column[]; children?: React.ReactNode; height: number; } export const EmptyTableRow = ({columns, children, height}: EmptyTableRowProps) => { return ( {children} ); };