UNPKG

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