import { IColumnProps } from "../.."; import { ITableProps } from "../.."; import { useApplyPastes } from "./useApplyPastes"; import { useSubRows } from "./useSubRows"; import { useMemo } from "react"; import { ISubRowProps } from "./ISubRowProps"; export const useSubRowProps = ({ elements, createSubRows, areRowsEqual, columnProps, getKey, ...props }: ISubRowProps): ITableProps< { element: TElement; subRow: TSubRow }, TElement, TError > => { const { rows, errors } = useSubRows( elements, createSubRows, getKey, areRowsEqual, columnProps ); const applyPastes = useApplyPastes(getKey); const convertedColumnProps = useMemo< IColumnProps<{ element: TElement; subRow: TSubRow }, TElement, TError>[] >( () => columnProps.map( ({ renderCell, renderEditor, onCopy, onPaste, onClear, isReadonly, ...otherProps }) => ({ renderCell: ({ row: { subRow, element }, ...renderProps }) => renderCell({ element, subRow, ...renderProps, }), renderEditor: ({ row: { subRow, element }, ...renderProps }) => renderEditor({ element, subRow, ...renderProps, }), onCopy: ({ subRow, element }) => onCopy(element, subRow), onPaste: ({ element, subRow }, value) => onPaste(element, subRow, value), onClear: ({ element, subRow }) => onClear(element, subRow), isReadonly: isReadonly ? ({ element, subRow }) => isReadonly(element, subRow) : undefined, ...otherProps, }) ), [columnProps] ); return { rows, columnProps: convertedColumnProps, errors, applyPastes, ...props, }; };