@carbon/ibm-products
Version:
Carbon for IBM Products
44 lines (42 loc) • 3 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 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.
*/
require("../../../_virtual/_rolldown/runtime.js");
const require_settings = require("../../../settings.js");
let _carbon_layout = require("@carbon/layout");
//#region src/components/DataSpreadsheet/utils/createActiveCellFn.js
/**
* Copyright IBM Corp. 2022, 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const createActiveCellFn = ({ placementElement, coords, addToHeader = false, contextRef, blockClass = `${require_settings.pkg.prefix}--data-spreadsheet`, onActiveCellChange, activeCellValue, activeCellRef, cellEditorRef, defaultColumn }) => {
if (!coords) return;
const spreadsheetSelector = contextRef?.current ?? document;
const point1Element = spreadsheetSelector.querySelector(`[data-row-index="${coords.row}"][data-column-index="${coords.column}"]`) || spreadsheetSelector.querySelector(`.${blockClass}__body--td`);
const selectionAreaCellWidth = point1Element.offsetWidth;
const selectionAreaCellHeight = point1Element.offsetHeight;
const activeElementContainer = addToHeader ? contextRef?.current.querySelector(`.${blockClass}__header--container`) : contextRef?.current.querySelector(`.${blockClass}__list--container`).firstElementChild;
const relativePosition = {
top: placementElement ? placementElement.getBoundingClientRect().top - activeElementContainer.getBoundingClientRect().top : coords.row === 0 ? 0 : selectionAreaCellHeight * coords.row,
left: placementElement ? placementElement.getBoundingClientRect().left - activeElementContainer.getBoundingClientRect().left : coords.column === 0 ? 0 + (defaultColumn.rowHeaderWidth - 4) : selectionAreaCellWidth * coords.column + (defaultColumn.rowHeaderWidth - 4)
};
const activeCellButton = activeCellRef?.current;
activeCellButton.style.width = (0, _carbon_layout.px)(placementElement ? placementElement?.offsetWidth : selectionAreaCellWidth);
activeCellButton.style.height = (0, _carbon_layout.px)(placementElement ? placementElement?.offsetHeight : selectionAreaCellHeight);
activeCellButton.style.left = (0, _carbon_layout.px)(relativePosition.left);
activeCellButton.style.top = (0, _carbon_layout.px)(relativePosition.top);
activeCellButton.style.display = "block";
activeCellButton.setAttribute("data-active-row-index", typeof coords?.row === "number" ? coords.row : "header");
activeCellButton.setAttribute("data-active-column-index", typeof coords?.column === "number" ? coords.column : "header");
activeElementContainer.appendChild(activeCellButton);
activeCellButton.focus();
if (!addToHeader) activeElementContainer.appendChild(cellEditorRef.current);
if (typeof coords?.column === "number" && typeof coords?.row === "number") onActiveCellChange?.(activeCellValue);
};
//#endregion
exports.createActiveCellFn = createActiveCellFn;