import React, { useMemo } from "react"; import { editingCellContext } from "../context/EditorContext"; import { IColumnProps } from "../IColumnProps"; import { usePropsRef } from "../context/PropsContext"; import { selectionContext } from "../context/SelectionContext"; import { StaticCell } from "./StaticCell"; import { StickyCell } from "./StickyCell"; type IProps = { y: number; row: TRow; columnProps: IColumnProps[]; errors: (TError | null)[]; numStickyColumns: number; numHeaders: number; numUnselectableColumns: number; }; /** * One horizontal row in the table. Lots of memoization is going on here to make * sure that rows are only rerendered when the data in them changes, not when * other rows change */ export const Row = ({ y, row, columnProps, numStickyColumns, numHeaders, errors, numUnselectableColumns, }: IProps) => useMemo( () => ( ), [ y, row, columnProps, numStickyColumns, numHeaders, errors, numUnselectableColumns, ] ); /** * This component determines a bunch of stuff about the cells, like whether they * have errors and whether they are readonly */ const MemoRow = ({ y, row, columnProps, errors, numStickyColumns, numHeaders, numUnselectableColumns, }: IProps) => { const propsRef = usePropsRef(); const setEditingCell = editingCellContext.useSetter(); const setSelection = selectionContext.useSetter(); if (!row) { return null; } return ( <> {columnProps.map((props, x) => { const error = errors[x]; const isReadonly = props.isReadonly?.(row) ?? false; const content = props.renderCell({ row, onEdit: () => { setEditingCell({ x, y }); setSelection({ start: { x, y }, height: 1, width: 1, primary: { x, y }, }); }, errors, isReadonly, onChange: (row) => propsRef.current.onChange([row]), }); return x < numStickyColumns ? ( = numUnselectableColumns} borderRightColor={ props.borderRightColor ?? columnProps[x + 1]?.borderLeftColor } isReadonly={isReadonly} > {content} ) : ( = numUnselectableColumns} borderRightColor={ props.borderRightColor ?? columnProps[x + 1]?.borderLeftColor } isReadonly={isReadonly} > {content} ); })} ); };