Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 50 additions & 42 deletions packages/table-core/src/core/row-models/createCoreRowModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,55 @@ export function createCoreRowModel<
}
}

function accessRows<TFeatures extends TableFeatures, TData extends RowData>(
table: Table_Internal<TFeatures, TData>,
rowModel: RowModel<TFeatures, TData>,
originalRows: ReadonlyArray<TData>,
depth = 0,
parentRow?: Row<TFeatures, TData>,
): Array<Row<TFeatures, TData>> {
const rows = [] as Array<Row<TFeatures, TData>>

for (let i = 0; i < originalRows.length; i++) {
const originalRow = originalRows[i]!
// Make the row
const row = constructRow(
table,
table.getRowId(originalRow, i, parentRow),
originalRow,
i,
depth,
undefined,
parentRow?.id,
)

// Keep track of every row in a flat array
rowModel.flatRows.push(row)
// Also keep track of every row by its ID
rowModel.rowsById[row.id] = row
// Push table row into parent
rows.push(row)

// Get the original subrows
if (table.options.getSubRows) {
row.originalSubRows = table.options.getSubRows(originalRow, i)

// Then recursively access them
if (row.originalSubRows?.length) {
row.subRows = accessRows(
table,
rowModel,
row.originalSubRows,
depth + 1,
row,
)
}
}
}

return rows
}

function _createCoreRowModel<
TFeatures extends TableFeatures,
TData extends RowData,
Expand All @@ -47,48 +96,7 @@ function _createCoreRowModel<
rowsById: makeObjectMap(),
}

const accessRows = (
originalRows: ReadonlyArray<TData>,
depth = 0,
parentRow?: Row<TFeatures, TData>,
): Array<Row<TFeatures, TData>> => {
const rows = [] as Array<Row<TFeatures, TData>>

for (let i = 0; i < originalRows.length; i++) {
const originalRow = originalRows[i]!
// Make the row
const row = constructRow(
table,
table.getRowId(originalRow, i, parentRow),
originalRow,
i,
depth,
undefined,
parentRow?.id,
)

// Keep track of every row in a flat array
rowModel.flatRows.push(row)
// Also keep track of every row by its ID
rowModel.rowsById[row.id] = row
// Push table row into parent
rows.push(row)

// Get the original subrows
if (table.options.getSubRows) {
row.originalSubRows = table.options.getSubRows(originalRow, i)

// Then recursively access them
if (row.originalSubRows?.length) {
row.subRows = accessRows(row.originalSubRows, depth + 1, row)
}
}
}

return rows
}

rowModel.rows = accessRows(data)
rowModel.rows = accessRows(table, rowModel, data)

return rowModel
}
2 changes: 1 addition & 1 deletion packages/table-core/src/core/rows/coreRowsFeature.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export function table_getRowId<
) {
return (
table.options.getRowId?.(originalRow, index, parent) ??
`${parent ? [parent.id, index].join('.') : index}`
(parent ? `${parent.id}.${index}` : String(index))
)
}

Expand Down