@tanstack/table-core
Version:
Headless UI for building powerful tables & datagrids for TS/JS.
90 lines (86 loc) • 3.02 kB
JavaScript
/**
* table-core
*
* Copyright (c) TanStack
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
;
var utils = require('../utils.js');
var cell = require('./cell.js');
const createRow = (table, id, original, rowIndex, depth, subRows, parentId) => {
let row = {
id,
index: rowIndex,
original,
depth,
parentId,
_valuesCache: {},
_uniqueValuesCache: {},
getValue: columnId => {
if (row._valuesCache.hasOwnProperty(columnId)) {
return row._valuesCache[columnId];
}
const column = table.getColumn(columnId);
if (!(column != null && column.accessorFn)) {
return undefined;
}
row._valuesCache[columnId] = column.accessorFn(row.original, rowIndex);
return row._valuesCache[columnId];
},
getUniqueValues: columnId => {
if (row._uniqueValuesCache.hasOwnProperty(columnId)) {
return row._uniqueValuesCache[columnId];
}
const column = table.getColumn(columnId);
if (!(column != null && 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, rowIndex);
return row._uniqueValuesCache[columnId];
},
renderValue: columnId => {
var _row$getValue;
return (_row$getValue = row.getValue(columnId)) != null ? _row$getValue : table.options.renderFallbackValue;
},
subRows: subRows != null ? subRows : [],
getLeafRows: () => utils.flattenBy(row.subRows, d => d.subRows),
getParentRow: () => row.parentId ? table.getRow(row.parentId, true) : undefined,
getParentRows: () => {
let parentRows = [];
let currentRow = row;
while (true) {
const parentRow = currentRow.getParentRow();
if (!parentRow) break;
parentRows.push(parentRow);
currentRow = parentRow;
}
return parentRows.reverse();
},
getAllCells: utils.memo(() => [table.getAllLeafColumns()], leafColumns => {
return leafColumns.map(column => {
return cell.createCell(table, row, column, column.id);
});
}, utils.getMemoOptions(table.options, 'debugRows', 'getAllCells')),
_getAllCellsByColumnId: utils.memo(() => [row.getAllCells()], allCells => {
return allCells.reduce((acc, cell) => {
acc[cell.column.id] = cell;
return acc;
}, {});
}, utils.getMemoOptions(table.options, 'debugRows', 'getAllCellsByColumnId'))
};
for (let i = 0; i < table._features.length; i++) {
const feature = table._features[i];
feature == null || feature.createRow == null || feature.createRow(row, table);
}
return row;
};
exports.createRow = createRow;
//# sourceMappingURL=row.js.map