| 1 |
import { Component, createRef } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/TreeView/tree-view'; |
| 4 |
import { canUseDOM } from '../../helpers/util'; |
| 5 |
import { handleArrows } from '../../helpers'; |
| 6 |
import { KeyTypes } from '../../helpers/constants'; |
| 7 |
|
| 8 |
export interface TreeViewRootProps { |
| 9 |
/** Child nodes of the tree view */ |
| 10 |
children: React.ReactNode; |
| 11 |
/** Flag indicating if the tree view has checkboxes. */ |
| 12 |
hasCheckboxes?: boolean; |
| 13 |
/** Flag indicating if tree view has guide lines. */ |
| 14 |
hasGuides?: boolean; |
| 15 |
/** Variant presentation styles for the tree view. */ |
| 16 |
variant?: 'default' | 'compact' | 'compactNoBackground'; |
| 17 |
/** Class to add to add if not passed a parentItem */ |
| 18 |
className?: string; |
| 19 |
/** Flag indicating that tree nodes should be independently selectable, even when having children */ |
| 20 |
hasSelectableNodes?: boolean; |
| 21 |
} |
| 22 |
|
| 23 |
class TreeViewRoot extends Component<TreeViewRootProps> { |
| 24 |
displayName = 'TreeViewRoot'; |
| 25 |
private treeRef = createRef<HTMLDivElement>(); |
| 26 |
|
| 27 |
componentDidMount() { |
| 28 |
if (canUseDOM) { |
| 29 |
window.addEventListener( |
| 30 |
'keydown', |
| 31 |
this.props.hasCheckboxes || this.props.hasSelectableNodes ? this.handleKeysCheckbox : this.handleKeys |
| 32 |
); |
| 33 |
} |
| 34 |
if (this.props.hasCheckboxes || this.props.hasSelectableNodes) { |
| 35 |
const firstToggle = this.treeRef.current.getElementsByClassName(styles.treeViewNodeToggle)[0] as HTMLElement; |
| 36 |
if (firstToggle) { |
| 37 |
firstToggle.tabIndex = 0; |
| 38 |
} |
| 39 |
if (this.props.hasCheckboxes) { |
| 40 |
const firstInput = this.treeRef.current.getElementsByTagName('INPUT')[0] as HTMLElement; |
| 41 |
if (firstInput) { |
| 42 |
firstInput.tabIndex = 0; |
| 43 |
} |
| 44 |
} |
| 45 |
if (this.props.hasSelectableNodes) { |
| 46 |
const firstTextButton = this.treeRef.current.getElementsByClassName(styles.treeViewNodeText)[0] as HTMLElement; |
| 47 |
if (firstTextButton) { |
| 48 |
firstTextButton.tabIndex = 0; |
| 49 |
} |
| 50 |
} |
| 51 |
} else { |
| 52 |
(this.treeRef.current?.getElementsByClassName(styles.treeViewNode)[0] as HTMLElement).tabIndex = 0; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
componentWillUnmount() { |
| 57 |
if (canUseDOM) { |
| 58 |
window.removeEventListener( |
| 59 |
'keydown', |
| 60 |
this.props.hasCheckboxes || this.props.hasSelectableNodes ? this.handleKeysCheckbox : this.handleKeys |
| 61 |
); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
handleKeys = (event: KeyboardEvent) => { |
| 66 |
if ( |
| 67 |
!this.treeRef.current.contains(event.target as HTMLElement) || |
| 68 |
!(event.target as HTMLElement).classList.contains(styles.treeViewNode) |
| 69 |
) { |
| 70 |
return; |
| 71 |
} |
| 72 |
const activeElement = document.activeElement; |
| 73 |
const key = event.key; |
| 74 |
const treeItems = Array.from(this.treeRef.current?.getElementsByClassName(styles.treeViewNode)).filter( |
| 75 |
(el) => !el.classList.contains('pf-m-disabled') && !el.closest(`.${styles.treeViewList}[inert]`) |
| 76 |
); |
| 77 |
|
| 78 |
if (key === KeyTypes.Space) { |
| 79 |
(activeElement as HTMLElement).click(); |
| 80 |
event.preventDefault(); |
| 81 |
} |
| 82 |
|
| 83 |
handleArrows( |
| 84 |
event, |
| 85 |
treeItems, |
| 86 |
(element: Element) => activeElement === element, |
| 87 |
undefined, |
| 88 |
[], |
| 89 |
undefined, |
| 90 |
true, |
| 91 |
true |
| 92 |
); |
| 93 |
|
| 94 |
if (['ArrowLeft', 'ArrowRight'].includes(key)) { |
| 95 |
const isExpandable = activeElement?.firstElementChild?.firstElementChild?.classList.contains( |
| 96 |
styles.treeViewNodeToggle |
| 97 |
); |
| 98 |
const isExpanded = activeElement?.closest('li')?.classList.contains('pf-m-expanded'); |
| 99 |
if (key === 'ArrowLeft') { |
| 100 |
if (isExpandable && isExpanded) { |
| 101 |
(activeElement as HTMLElement).click(); |
| 102 |
} else { |
| 103 |
const parentList = activeElement?.closest('ul')?.parentElement; |
| 104 |
if (parentList?.tagName !== 'DIV') { |
| 105 |
const parentButton = parentList?.querySelector('button'); |
| 106 |
(activeElement as HTMLElement).tabIndex = -1; |
| 107 |
if (parentButton) { |
| 108 |
parentButton.tabIndex = 0; |
| 109 |
parentButton.focus(); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} else { |
| 114 |
if (isExpandable && !isExpanded) { |
| 115 |
(activeElement as HTMLElement).tabIndex = -1; |
| 116 |
(activeElement as HTMLElement).click(); |
| 117 |
const childElement = activeElement?.closest('li')?.querySelector('ul > li')?.querySelector('button'); |
| 118 |
if (childElement) { |
| 119 |
childElement.tabIndex = 0; |
| 120 |
childElement.focus(); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
event.preventDefault(); |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
handleKeysCheckbox = (event: KeyboardEvent) => { |
| 129 |
if (!this.treeRef.current.contains(event.target as HTMLElement)) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
const activeElement = document.activeElement; |
| 134 |
const key = event.key; |
| 135 |
|
| 136 |
if (key === KeyTypes.Space) { |
| 137 |
(activeElement as HTMLElement).click(); |
| 138 |
event.preventDefault(); |
| 139 |
} |
| 140 |
|
| 141 |
const treeNodes = Array.from(this.treeRef.current?.getElementsByClassName(styles.treeViewNode)).filter( |
| 142 |
(el) => !el.closest(`.${styles.treeViewList}[inert]`) |
| 143 |
); |
| 144 |
|
| 145 |
handleArrows( |
| 146 |
event, |
| 147 |
treeNodes as HTMLElement[], |
| 148 |
(element: Element) => element.contains(activeElement), |
| 149 |
(element: Element) => element.querySelector('button,input'), |
| 150 |
[], |
| 151 |
undefined, |
| 152 |
true, |
| 153 |
true |
| 154 |
); |
| 155 |
|
| 156 |
if (['ArrowLeft', 'ArrowRight'].includes(key)) { |
| 157 |
if (key === 'ArrowLeft') { |
| 158 |
if (activeElement?.tagName === 'INPUT') { |
| 159 |
activeElement?.parentElement?.previousSibling && |
| 160 |
(activeElement.parentElement.previousSibling as HTMLElement).focus(); |
| 161 |
} else if (activeElement?.previousSibling) { |
| 162 |
if (activeElement.previousElementSibling?.tagName === 'SPAN') { |
| 163 |
(activeElement.previousSibling.firstChild as HTMLElement).focus(); |
| 164 |
} else { |
| 165 |
(activeElement.previousSibling as HTMLElement).focus(); |
| 166 |
} |
| 167 |
} |
| 168 |
} else { |
| 169 |
if (activeElement?.tagName === 'INPUT') { |
| 170 |
activeElement.parentElement?.nextSibling && (activeElement.parentElement.nextSibling as HTMLElement).focus(); |
| 171 |
} else if (activeElement?.nextSibling) { |
| 172 |
if (activeElement.nextElementSibling?.tagName === 'SPAN') { |
| 173 |
(activeElement.nextSibling.firstChild as HTMLElement).focus(); |
| 174 |
} else { |
| 175 |
(activeElement.nextSibling as HTMLElement).focus(); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
event.preventDefault(); |
| 180 |
} |
| 181 |
}; |
| 182 |
|
| 183 |
variantStyleModifiers: { [key in TreeViewRootProps['variant']]: string | string[] } = { |
| 184 |
default: '', |
| 185 |
compact: styles.modifiers.compact, |
| 186 |
compactNoBackground: [styles.modifiers.compact, styles.modifiers.noBackground] |
| 187 |
}; |
| 188 |
|
| 189 |
render() { |
| 190 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 191 |
const { children, hasCheckboxes, hasGuides, variant, className, hasSelectableNodes, ...props } = this.props; |
| 192 |
return ( |
| 193 |
<div |
| 194 |
className={css( |
| 195 |
styles.treeView, |
| 196 |
hasGuides && styles.modifiers.guides, |
| 197 |
this.variantStyleModifiers[variant], |
| 198 |
className |
| 199 |
)} |
| 200 |
ref={this.treeRef} |
| 201 |
{...props} |
| 202 |
> |
| 203 |
{children} |
| 204 |
</div> |
| 205 |
); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
export { TreeViewRoot }; |