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