| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.Table = exports.TableContext = void 0; |
| 4 |
const tslib_1 = require("tslib"); |
| 5 |
const jsx_runtime_1 = require("react/jsx-runtime"); |
| 6 |
const react_1 = require("react"); |
| 7 |
const table_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Table/table")); |
| 8 |
const table_grid_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Table/table-grid")); |
| 9 |
const table_tree_view_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Table/table-tree-view")); |
| 10 |
const react_styles_1 = require("@patternfly/react-styles"); |
| 11 |
const utils_1 = require("./utils"); |
| 12 |
const KeyboardHandler_1 = require('@patternfly/react-core/dist/js/helpers/KeyboardHandler'); |
| 13 |
const constants_1 = require('@patternfly/react-core/dist/js/helpers/constants'); |
| 14 |
const ouia_1 = require('@patternfly/react-core/dist/js/helpers/OUIA/ouia'); |
| 15 |
const helpers_1 = require('@patternfly/react-core/dist/js/helpers'); |
| 16 |
const TableTypes_1 = require("./TableTypes"); |
| 17 |
exports.TableContext = (0, react_1.createContext)({ |
| 18 |
registerSelectableRow: () => { }, |
| 19 |
hasAnimations: false, |
| 20 |
variant: undefined |
| 21 |
}); |
| 22 |
const TableBase = (_a) => { |
| 23 |
var _b, _c; |
| 24 |
var { children, className, variant, borders = true, isStickyHeader = false, isStickyHeaderBase = false, isStickyHeaderStuck = false, isPlain = false, isNoPlainOnGlass = false, gridBreakPoint = TableTypes_1.TableGridBreakpoint.gridMd, 'aria-label': ariaLabel, role = 'grid', innerRef, ouiaId, ouiaSafe = true, isTreeTable = false, isNested = false, isStriped = false, isExpandable = false, hasAnimations: hasAnimationsProp, hasNoInset = false, |
| 25 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 26 |
nestedHeaderColumnSpans, selectableRowCaptionText } = _a, props = tslib_1.__rest(_a, ["children", "className", "variant", "borders", "isStickyHeader", "isStickyHeaderBase", "isStickyHeaderStuck", "isPlain", "isNoPlainOnGlass", "gridBreakPoint", 'aria-label', "role", "innerRef", "ouiaId", "ouiaSafe", "isTreeTable", "isNested", "isStriped", "isExpandable", "hasAnimations", "hasNoInset", "nestedHeaderColumnSpans", "selectableRowCaptionText"]); |
| 27 |
const hasAnimations = (0, helpers_1.useHasAnimations)(hasAnimationsProp); |
| 28 |
const ref = (0, react_1.useRef)(null); |
| 29 |
const tableRef = innerRef || ref; |
| 30 |
const [hasSelectableRows, setHasSelectableRows] = (0, react_1.useState)(false); |
| 31 |
const [tableCaption, setTableCaption] = (0, react_1.useState)(); |
| 32 |
(0, react_1.useEffect)(() => { |
| 33 |
document.addEventListener('keydown', handleKeys); |
| 34 |
// sets up roving tab-index to tree tables only |
| 35 |
if (tableRef && tableRef.current && tableRef.current.classList.contains('pf-m-tree-view')) { |
| 36 |
const tbody = tableRef.current.querySelector('tbody'); |
| 37 |
tbody && (0, KeyboardHandler_1.setTabIndex)(Array.from(tbody.querySelectorAll('button, a, input'))); |
| 38 |
} |
| 39 |
return function cleanup() { |
| 40 |
document.removeEventListener('keydown', handleKeys); |
| 41 |
}; |
| 42 |
}, [tableRef, tableRef.current]); |
| 43 |
(0, react_1.useEffect)(() => { |
| 44 |
if (selectableRowCaptionText) { |
| 45 |
setTableCaption((0, jsx_runtime_1.jsxs)("caption", { children: [selectableRowCaptionText, (0, jsx_runtime_1.jsx)("div", { className: "pf-v6-screen-reader", children: "This table has selectable rows. It can be navigated by row using tab, and each row can be selected using space or enter." })] })); |
| 46 |
} |
| 47 |
else { |
| 48 |
setTableCaption((0, jsx_runtime_1.jsx)("caption", { className: "pf-v6-screen-reader", children: "This table has selectable rows. It can be navigated by row using tab, and each row can be selected using space or enter." })); |
| 49 |
} |
| 50 |
}, [selectableRowCaptionText]); |
| 51 |
const ouiaProps = (0, ouia_1.useOUIAProps)('Table', ouiaId, ouiaSafe); |
| 52 |
const grid = (_b = table_grid_1.default.modifiers) === null || _b === void 0 ? void 0 : _b[(0, utils_1.toCamel)(gridBreakPoint || '').replace(/-?2xl/, '_2xl')]; |
| 53 |
const breakPointPrefix = `treeView${gridBreakPoint.charAt(0).toUpperCase() + gridBreakPoint.slice(1)}`; |
| 54 |
const treeGrid = (_c = table_tree_view_1.default.modifiers) === null || _c === void 0 ? void 0 : _c[(0, utils_1.toCamel)(breakPointPrefix || '').replace(/-?2xl/, '_2xl')]; |
| 55 |
const handleKeys = (event) => { |
| 56 |
if (isNested || |
| 57 |
!(tableRef && tableRef.current && tableRef.current.classList.contains(table_tree_view_1.default.modifiers.treeView)) || // implements roving tab-index to tree tables only |
| 58 |
(tableRef && tableRef.current !== event.target.closest(`.${table_1.default.table}:not(.pf-m-nested)`))) { |
| 59 |
return; |
| 60 |
} |
| 61 |
const activeElement = document.activeElement; |
| 62 |
const key = event.key; |
| 63 |
const rows = Array.from(tableRef.current.querySelectorAll('tbody tr')).filter((el) => !el.classList.contains('pf-m-disabled') && !el.hidden); |
| 64 |
if (key === constants_1.KeyTypes.Space || key === constants_1.KeyTypes.Enter) { |
| 65 |
activeElement.click(); |
| 66 |
event.preventDefault(); |
| 67 |
} |
| 68 |
const getFocusableElement = (element) => element.querySelectorAll('button:not(:disabled), input:not(:disabled), a:not(:disabled)')[0]; |
| 69 |
(0, KeyboardHandler_1.handleArrows)(event, rows, (element) => element === activeElement.closest('tr'), getFocusableElement, ['button', 'input', 'a'], undefined, false, true, false); |
| 70 |
}; |
| 71 |
const registerSelectableRow = () => { |
| 72 |
!hasSelectableRows && setHasSelectableRows(true); |
| 73 |
}; |
| 74 |
return ((0, jsx_runtime_1.jsx)(exports.TableContext.Provider, { value: { registerSelectableRow, hasAnimations, variant }, children: (0, jsx_runtime_1.jsxs)("table", Object.assign({ "aria-label": ariaLabel, role: role, className: (0, react_styles_1.css)(className, table_1.default.table, isTreeTable ? treeGrid : grid, table_1.default.modifiers[variant], !borders && table_1.default.modifiers.noBorderRows, isStickyHeader && table_1.default.modifiers.stickyHeader, isStickyHeaderBase && table_1.default.modifiers.stickyHeaderBase, isStickyHeaderStuck && table_1.default.modifiers.stickyHeaderStuck, isTreeTable && table_tree_view_1.default.modifiers.treeView, isStriped && table_1.default.modifiers.striped, isExpandable && table_1.default.modifiers.expandable, isPlain && table_1.default.modifiers.plain, isNoPlainOnGlass && table_1.default.modifiers.noPlainOnGlass, hasNoInset && table_tree_view_1.default.modifiers.noInset, isNested && 'pf-m-nested', hasAnimations && table_1.default.modifiers.animateExpand), ref: tableRef }, (isTreeTable && { role: 'treegrid' }), ouiaProps, props, { children: [hasSelectableRows && tableCaption, children] })) })); |
| 75 |
}; |
| 76 |
exports.Table = (0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(TableBase, Object.assign({}, props, { innerRef: ref })))); |
| 77 |
exports.Table.displayName = 'Table'; |
| 78 |
//# sourceMappingURL=Table.js.map |