Debian Codegraph @

node/@patternfly/react-core/dist/esm/components/TreeView/TreeViewRoot.js

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