| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.TreeViewRoot = 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 react_styles_1 = require("@patternfly/react-styles"); |
| 8 |
const tree_view_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/TreeView/tree-view")); |
| 9 |
const util_1 = require("../../helpers/util"); |
| 10 |
const helpers_1 = require("../../helpers"); |
| 11 |
const constants_1 = require("../../helpers/constants"); |
| 12 |
class TreeViewRoot extends react_1.Component { |
| 13 |
constructor() { |
| 14 |
super(...arguments); |
| 15 |
this.displayName = 'TreeViewRoot'; |
| 16 |
this.treeRef = (0, react_1.createRef)(); |
| 17 |
this.handleKeys = (event) => { |
| 18 |
var _a, _b, _c, _d, _e, _f, _g; |
| 19 |
if (!this.treeRef.current.contains(event.target) || |
| 20 |
!event.target.classList.contains(tree_view_1.default.treeViewNode)) { |
| 21 |
return; |
| 22 |
} |
| 23 |
const activeElement = document.activeElement; |
| 24 |
const key = event.key; |
| 25 |
const treeItems = Array.from((_a = this.treeRef.current) === null || _a === void 0 ? void 0 : _a.getElementsByClassName(tree_view_1.default.treeViewNode)).filter((el) => !el.classList.contains('pf-m-disabled') && !el.closest(`.${tree_view_1.default.treeViewList}[inert]`)); |
| 26 |
if (key === constants_1.KeyTypes.Space) { |
| 27 |
activeElement.click(); |
| 28 |
event.preventDefault(); |
| 29 |
} |
| 30 |
(0, helpers_1.handleArrows)(event, treeItems, (element) => activeElement === element, undefined, [], undefined, true, true); |
| 31 |
if (['ArrowLeft', 'ArrowRight'].includes(key)) { |
| 32 |
const isExpandable = (_c = (_b = activeElement === null || activeElement === void 0 ? void 0 : activeElement.firstElementChild) === null || _b === void 0 ? void 0 : _b.firstElementChild) === null || _c === void 0 ? void 0 : _c.classList.contains(tree_view_1.default.treeViewNodeToggle); |
| 33 |
const isExpanded = (_d = activeElement === null || activeElement === void 0 ? void 0 : activeElement.closest('li')) === null || _d === void 0 ? void 0 : _d.classList.contains('pf-m-expanded'); |
| 34 |
if (key === 'ArrowLeft') { |
| 35 |
if (isExpandable && isExpanded) { |
| 36 |
activeElement.click(); |
| 37 |
} |
| 38 |
else { |
| 39 |
const parentList = (_e = activeElement === null || activeElement === void 0 ? void 0 : activeElement.closest('ul')) === null || _e === void 0 ? void 0 : _e.parentElement; |
| 40 |
if ((parentList === null || parentList === void 0 ? void 0 : parentList.tagName) !== 'DIV') { |
| 41 |
const parentButton = parentList === null || parentList === void 0 ? void 0 : parentList.querySelector('button'); |
| 42 |
activeElement.tabIndex = -1; |
| 43 |
if (parentButton) { |
| 44 |
parentButton.tabIndex = 0; |
| 45 |
parentButton.focus(); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
else { |
| 51 |
if (isExpandable && !isExpanded) { |
| 52 |
activeElement.tabIndex = -1; |
| 53 |
activeElement.click(); |
| 54 |
const childElement = (_g = (_f = activeElement === null || activeElement === void 0 ? void 0 : activeElement.closest('li')) === null || _f === void 0 ? void 0 : _f.querySelector('ul > li')) === null || _g === void 0 ? void 0 : _g.querySelector('button'); |
| 55 |
if (childElement) { |
| 56 |
childElement.tabIndex = 0; |
| 57 |
childElement.focus(); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
event.preventDefault(); |
| 62 |
} |
| 63 |
}; |
| 64 |
this.handleKeysCheckbox = (event) => { |
| 65 |
var _a, _b, _c, _d, _e; |
| 66 |
if (!this.treeRef.current.contains(event.target)) { |
| 67 |
return; |
| 68 |
} |
| 69 |
const activeElement = document.activeElement; |
| 70 |
const key = event.key; |
| 71 |
if (key === constants_1.KeyTypes.Space) { |
| 72 |
activeElement.click(); |
| 73 |
event.preventDefault(); |
| 74 |
} |
| 75 |
const treeNodes = Array.from((_a = this.treeRef.current) === null || _a === void 0 ? void 0 : _a.getElementsByClassName(tree_view_1.default.treeViewNode)).filter((el) => !el.closest(`.${tree_view_1.default.treeViewList}[inert]`)); |
| 76 |
(0, helpers_1.handleArrows)(event, treeNodes, (element) => element.contains(activeElement), (element) => element.querySelector('button,input'), [], undefined, true, true); |
| 77 |
if (['ArrowLeft', 'ArrowRight'].includes(key)) { |
| 78 |
if (key === 'ArrowLeft') { |
| 79 |
if ((activeElement === null || activeElement === void 0 ? void 0 : activeElement.tagName) === 'INPUT') { |
| 80 |
((_b = activeElement === null || activeElement === void 0 ? void 0 : activeElement.parentElement) === null || _b === void 0 ? void 0 : _b.previousSibling) && |
| 81 |
activeElement.parentElement.previousSibling.focus(); |
| 82 |
} |
| 83 |
else if (activeElement === null || activeElement === void 0 ? void 0 : activeElement.previousSibling) { |
| 84 |
if (((_c = activeElement.previousElementSibling) === null || _c === void 0 ? void 0 : _c.tagName) === 'SPAN') { |
| 85 |
activeElement.previousSibling.firstChild.focus(); |
| 86 |
} |
| 87 |
else { |
| 88 |
activeElement.previousSibling.focus(); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
else { |
| 93 |
if ((activeElement === null || activeElement === void 0 ? void 0 : activeElement.tagName) === 'INPUT') { |
| 94 |
((_d = activeElement.parentElement) === null || _d === void 0 ? void 0 : _d.nextSibling) && activeElement.parentElement.nextSibling.focus(); |
| 95 |
} |
| 96 |
else if (activeElement === null || activeElement === void 0 ? void 0 : activeElement.nextSibling) { |
| 97 |
if (((_e = activeElement.nextElementSibling) === null || _e === void 0 ? void 0 : _e.tagName) === 'SPAN') { |
| 98 |
activeElement.nextSibling.firstChild.focus(); |
| 99 |
} |
| 100 |
else { |
| 101 |
activeElement.nextSibling.focus(); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
event.preventDefault(); |
| 106 |
} |
| 107 |
}; |
| 108 |
this.variantStyleModifiers = { |
| 109 |
default: '', |
| 110 |
compact: tree_view_1.default.modifiers.compact, |
| 111 |
compactNoBackground: [tree_view_1.default.modifiers.compact, tree_view_1.default.modifiers.noBackground] |
| 112 |
}; |
| 113 |
} |
| 114 |
componentDidMount() { |
| 115 |
var _a; |
| 116 |
if (util_1.canUseDOM) { |
| 117 |
window.addEventListener('keydown', this.props.hasCheckboxes || this.props.hasSelectableNodes ? this.handleKeysCheckbox : this.handleKeys); |
| 118 |
} |
| 119 |
if (this.props.hasCheckboxes || this.props.hasSelectableNodes) { |
| 120 |
const firstToggle = this.treeRef.current.getElementsByClassName(tree_view_1.default.treeViewNodeToggle)[0]; |
| 121 |
if (firstToggle) { |
| 122 |
firstToggle.tabIndex = 0; |
| 123 |
} |
| 124 |
if (this.props.hasCheckboxes) { |
| 125 |
const firstInput = this.treeRef.current.getElementsByTagName('INPUT')[0]; |
| 126 |
if (firstInput) { |
| 127 |
firstInput.tabIndex = 0; |
| 128 |
} |
| 129 |
} |
| 130 |
if (this.props.hasSelectableNodes) { |
| 131 |
const firstTextButton = this.treeRef.current.getElementsByClassName(tree_view_1.default.treeViewNodeText)[0]; |
| 132 |
if (firstTextButton) { |
| 133 |
firstTextButton.tabIndex = 0; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
else { |
| 138 |
((_a = this.treeRef.current) === null || _a === void 0 ? void 0 : _a.getElementsByClassName(tree_view_1.default.treeViewNode)[0]).tabIndex = 0; |
| 139 |
} |
| 140 |
} |
| 141 |
componentWillUnmount() { |
| 142 |
if (util_1.canUseDOM) { |
| 143 |
window.removeEventListener('keydown', this.props.hasCheckboxes || this.props.hasSelectableNodes ? this.handleKeysCheckbox : this.handleKeys); |
| 144 |
} |
| 145 |
} |
| 146 |
render() { |
| 147 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 148 |
const _a = this.props, { children, hasCheckboxes, hasGuides, variant, className, hasSelectableNodes } = _a, props = tslib_1.__rest(_a, ["children", "hasCheckboxes", "hasGuides", "variant", "className", "hasSelectableNodes"]); |
| 149 |
return ((0, jsx_runtime_1.jsx)("div", Object.assign({ className: (0, react_styles_1.css)(tree_view_1.default.treeView, hasGuides && tree_view_1.default.modifiers.guides, this.variantStyleModifiers[variant], className), ref: this.treeRef }, props, { children: children }))); |
| 150 |
} |
| 151 |
} |
| 152 |
exports.TreeViewRoot = TreeViewRoot; |
| 153 |
//# sourceMappingURL=TreeViewRoot.js.map |