UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

109 lines (108 loc) 3.51 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useTableHighlight = useTableHighlight; var _push = _interopRequireDefault(require("core-js-pure/stable/instance/push.js")); var _react = require("react"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function isHighlighted(cell) { return cell.classList.contains('dnb-table__td--highlight') || cell.classList.contains('dnb-table__th--highlight'); } function applyHighlight(table) { const highlightedColumns = new Set(); const thElements = table.querySelectorAll('thead th.dnb-table__th--highlight'); for (const th of Array.from(thElements)) { highlightedColumns.add(th.cellIndex); } const applied = []; for (let r = 0; r < table.rows.length; r++) { const row = table.rows[r]; for (let c = 0; c < row.cells.length; c++) { const cell = row.cells[c]; if (cell.tagName === 'TD' && highlightedColumns.has(c) && !cell.classList.contains('dnb-table__td--highlight')) { cell.classList.add('dnb-table__td--highlight'); (0, _push.default)(applied).call(applied, { cell, classes: ['dnb-table__td--highlight'] }); } } } for (let r = 0; r < table.rows.length; r++) { const row = table.rows[r]; const prevRow = r > 0 ? table.rows[r - 1] : null; for (let c = 0; c < row.cells.length; c++) { const cell = row.cells[c]; const cellIsHighlighted = isHighlighted(cell); if (cellIsHighlighted) { const prevCell = prevRow === null || prevRow === void 0 ? void 0 : prevRow.cells[c]; if (prevCell && isHighlighted(prevCell)) { cell.classList.add('dnb-table__td--highlight-border'); (0, _push.default)(applied).call(applied, { cell, classes: ['dnb-table__td--highlight-border'] }); } continue; } const classes = []; const leftCell = row.cells[c - 1]; if (leftCell && isHighlighted(leftCell)) { (0, _push.default)(classes).call(classes, 'dnb-table--highlight-neighbor-left'); } const rightCell = row.cells[c + 1]; if (rightCell && isHighlighted(rightCell)) { (0, _push.default)(classes).call(classes, 'dnb-table--highlight-neighbor-right'); } const topCell = prevRow === null || prevRow === void 0 ? void 0 : prevRow.cells[c]; if (topCell && isHighlighted(topCell)) { (0, _push.default)(classes).call(classes, 'dnb-table--highlight-neighbor-top'); } if (classes.length > 0) { cell.classList.add(...classes); (0, _push.default)(applied).call(applied, { cell, classes }); } } } return () => { for (const { cell, classes } of applied) { cell.classList.remove(...classes); } }; } function useTableHighlight({ enabled = true } = {}) { const ref = (0, _react.useRef)(null); const getTable = (0, _react.useCallback)(() => { const el = ref.current; if (!el) { return null; } if (el.tagName === 'TABLE') { return el; } return el.querySelector('table'); }, []); (0, _react.useEffect)(() => { const el = ref.current; if (!el || !enabled) { return undefined; } const table = getTable(); if (!table) { return undefined; } return applyHighlight(table); }); return ref; } //# sourceMappingURL=useTableHighlight.js.map