import { RowData, Cell, Row, Table } from '../types' import { flattenBy, memo } from '../utils' import { createCell } from './cell' export interface CoreRow { id: string index: number original: TData depth: number _valuesCache: Record _uniqueValuesCache: Record getValue: (columnId: string) => TValue getUniqueValues: (columnId: string) => TValue[] renderValue: (columnId: string) => TValue subRows: Row[] getLeafRows: () => Row[] originalSubRows?: TData[] getAllCells: () => Cell[] _getAllCellsByColumnId: () => Record> } export const createRow = ( table: Table, id: string, original: TData, rowIndex: number, depth: number, subRows?: Row[] ): Row => { let row: CoreRow = { id, index: rowIndex, original, depth, _valuesCache: {}, _uniqueValuesCache: {}, getValue: columnId => { if (row._valuesCache.hasOwnProperty(columnId)) { return row._valuesCache[columnId] } const column = table.getColumn(columnId) if (!column?.accessorFn) { return undefined } row._valuesCache[columnId] = column.accessorFn( row.original as TData, rowIndex ) return row._valuesCache[columnId] as any }, getUniqueValues: columnId => { if (row._uniqueValuesCache.hasOwnProperty(columnId)) { return row._uniqueValuesCache[columnId] } const column = table.getColumn(columnId) if (!column?.accessorFn) { return undefined } if (!column.columnDef.getUniqueValues) { row._uniqueValuesCache[columnId] = [row.getValue(columnId)] return row._uniqueValuesCache[columnId] } row._uniqueValuesCache[columnId] = column.columnDef.getUniqueValues( row.original as TData, rowIndex ) return row._uniqueValuesCache[columnId] as any }, renderValue: columnId => row.getValue(columnId) ?? table.options.renderFallbackValue, subRows: subRows ?? [], getLeafRows: () => flattenBy(row.subRows, d => d.subRows), getAllCells: memo( () => [table.getAllLeafColumns()], leafColumns => { return leafColumns.map(column => { return createCell(table, row as Row, column, column.id) }) }, { key: process.env.NODE_ENV === 'development' && 'row.getAllCells', debug: () => table.options.debugAll ?? table.options.debugRows, } ), _getAllCellsByColumnId: memo( () => [row.getAllCells()], allCells => { return allCells.reduce((acc, cell) => { acc[cell.column.id] = cell return acc }, {} as Record>) }, { key: process.env.NODE_ENV === 'production' && 'row.getAllCellsByColumnId', debug: () => table.options.debugAll ?? table.options.debugRows, } ), } for (let i = 0; i < table._features.length; i++) { const feature = table._features[i] Object.assign(row, feature?.createRow?.(row, table)) } return row as Row }