@carbon/ibm-products
Version:
Carbon for IBM Products
43 lines (41 loc) • 1.59 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.
*/
import { useCallback, useEffect } from "react";
//#region src/components/DataSpreadsheet/hooks/useMoveActiveCell.js
/**
* Copyright IBM Corp. 2022, 2024
*
* 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 useMoveActiveCell = ({ spreadsheetRef, activeCellCoordinates, containerHasFocus, createActiveCell, activeCellContent, isActiveHeaderCellChanged }) => {
let performCreateActiveCell;
useEffect(() => {
performCreateActiveCell();
}, [
activeCellContent,
performCreateActiveCell,
isActiveHeaderCellChanged
]);
performCreateActiveCell = useCallback(() => {
const activeCellPlacementElement = spreadsheetRef?.current.querySelector(`[data-row-index="${activeCellCoordinates?.row}"][data-column-index="${activeCellCoordinates?.column}"]`);
const shouldPlaceActiveCellInHeader = activeCellCoordinates?.row === "header" && true;
const selectAllElement = spreadsheetRef?.current.querySelector(`[data-row-index="header"][data-column-index="header"]`);
if (containerHasFocus) createActiveCell({
placementElement: activeCellCoordinates ? activeCellPlacementElement : selectAllElement,
coords: activeCellCoordinates,
addToHeader: shouldPlaceActiveCellInHeader
});
}, [
spreadsheetRef,
activeCellCoordinates,
containerHasFocus,
createActiveCell
]);
};
//#endregion
export { useMoveActiveCell };