UNPKG

@carbon/react

Version:

React components for the Carbon Design System

67 lines (65 loc) 1.81 kB
/** * Copyright IBM Corp. 2016, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ import { isComponentElement } from "../../../internal/utils.js"; import { AILabel } from "../../AILabel/index.js"; import { getCellId } from "./cells.js"; //#region src/components/DataTable/tools/normalize.js /** * Copyright IBM Corp. 2016, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ /** * Normalize a collection of rows with the given headers. * * @param {Array<object>} rows * @param {Array<object>} headers * @returns {object} */ const normalize = (rows, headers, prevState = {}) => { const { rowsById: prevRowsByIds } = prevState; const rowIds = new Array(rows.length); const rowsById = {}; const cellsById = {}; rows.forEach((row, i) => { rowIds[i] = row.id; const { id, isSelected = false, isExpanded = false, disabled = false } = row; rowsById[id] = { id, isSelected, isExpanded, disabled, cells: new Array(headers.length) }; if (prevRowsByIds && prevRowsByIds[row.id] !== void 0) { rowsById[row.id].isSelected = prevRowsByIds[row.id].isSelected; rowsById[row.id].isExpanded = prevRowsByIds[row.id].isExpanded; } headers.forEach(({ key, slug, decorator }, i) => { const id = getCellId(row.id, key); cellsById[id] = { id, value: row[key], isEditable: false, isEditing: false, isValid: true, errors: null, hasAILabelHeader: !!(slug || isComponentElement(decorator, AILabel)), info: { header: key } }; rowsById[row.id].cells[i] = id; }); }); return { rowIds, rowsById, cellsById }; }; //#endregion export { normalize as default };