Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #7593: disableKeyboardNavigation prop for Tree component #7588

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions components/doc/common/apidoc/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -52450,6 +52450,14 @@
"default": "",
"description": "A single key to control the selection with the context menu."
},
{
"name": "disableKeyboardNavigation",
"optional": true,
"readonly": false,
"type": "boolean",
"default": "false",
"description": "When present, it disables keyboard navigation around the tree."
},
{
"name": "disabled",
"optional": true,
Expand Down
1 change: 1 addition & 0 deletions components/lib/tree/Tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ export const Tree = React.memo(
collapseIcon={props.collapseIcon}
contextMenuSelectionKey={props.contextMenuSelectionKey}
cx={cx}
disableKeyboardNavigation={props.disableKeyboardNavigation}
disabled={props.disabled}
dragdropScope={props.dragdropScope}
expandIcon={props.expandIcon}
Expand Down
1 change: 1 addition & 0 deletions components/lib/tree/TreeBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const TreeBase = ComponentBase.extend({
contentClassName: null,
contentStyle: null,
contextMenuSelectionKey: null,
disableKeyboardNavigation: false,
disabled: false,
dragdropScope: null,
emptyMessage: null,
Expand Down
108 changes: 60 additions & 48 deletions components/lib/tree/UITreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,21 @@ export const UITreeNode = React.memo((props) => {
};

const onArrowDown = (event) => {
const nodeElement = event.target.getAttribute('data-pc-section') === 'toggler' ? event.target.closest('[role="treeitem"]') : event.target;
const listElement = nodeElement.children[1];
const nextElement = getNextElement(nodeElement);

if (listElement) {
focusRowChange(nodeElement, props.dragdropScope ? listElement.children[1] : listElement.children[0]);
} else if (nextElement) {
focusRowChange(nodeElement, nextElement);
} else {
let nextSiblingAncestor = findNextSiblingOfAncestor(nodeElement);

if (nextSiblingAncestor) {
focusRowChange(nodeElement, nextSiblingAncestor);
if(!props.disableKeyboardNavigation) {
const nodeElement = event.target.getAttribute('data-pc-section') === 'toggler' ? event.target.closest('[role="treeitem"]') : event.target;
const listElement = nodeElement.children[1];
const nextElement = getNextElement(nodeElement);

if (listElement) {
focusRowChange(nodeElement, props.dragdropScope ? listElement.children[1] : listElement.children[0]);
} else if (nextElement) {
focusRowChange(nodeElement, nextElement);
} else {
let nextSiblingAncestor = findNextSiblingOfAncestor(nodeElement);

if (nextSiblingAncestor) {
focusRowChange(nodeElement, nextSiblingAncestor);
}
}
}

Expand Down Expand Up @@ -412,61 +414,71 @@ export const UITreeNode = React.memo((props) => {
};

const onArrowUp = (event) => {
const nodeElement = event.target;
const previous = getPreviousElement(nodeElement);

if (previous) {
focusRowChange(nodeElement, previous, findLastVisibleDescendant(previous));
} else {
let parentNodeElement = getParentNodeElement(nodeElement);

if (parentNodeElement) {
focusRowChange(nodeElement, parentNodeElement);
if(!props.disableKeyboardNavigation) {
const nodeElement = event.target;
const previous = getPreviousElement(nodeElement);

if (previous) {
focusRowChange(nodeElement, previous, findLastVisibleDescendant(previous));
} else {
let parentNodeElement = getParentNodeElement(nodeElement);

if (parentNodeElement) {
focusRowChange(nodeElement, parentNodeElement);
}
}
}

event.preventDefault();
};

const onArrowRight = (event) => {
if (isLeaf || expanded) {
return;
if(!props.disableKeyboardNavigation) {
if (isLeaf || expanded) {
return;
}

event.currentTarget.tabIndex = -1;

expand(event, true);
}

event.currentTarget.tabIndex = -1;

expand(event, true);
};

const onArrowLeft = (event) => {
const togglerElement = DomHandler.findSingle(event.currentTarget, '[data-pc-section="toggler"]');

if (props.level === 0 && !expanded) {
return false;
}

if (expanded && !isLeaf) {
togglerElement.click();

return false;
}

const target = findBeforeClickableNode(event.currentTarget);

if (target) {
focusRowChange(event.currentTarget, target);
if(!props.disableKeyboardNavigation) {
const togglerElement = DomHandler.findSingle(event.currentTarget, '[data-pc-section="toggler"]');

if (props.level === 0 && !expanded) {
return false;
}

if (expanded && !isLeaf) {
togglerElement.click();

return false;
}

const target = findBeforeClickableNode(event.currentTarget);

if (target) {
focusRowChange(event.currentTarget, target);
}
}
};

const onEnterKey = (event) => {
setTabIndexForSelectionMode(event, nodeTouched.current);
onClick(event);
if(!props.disableKeyboardNavigation) {
setTabIndexForSelectionMode(event, nodeTouched.current);
onClick(event);
}

event.preventDefault();
};

const onTabKey = () => {
setAllNodesTabIndexes();
if(!props.disableKeyboardNavigation) {
setAllNodesTabIndexes();
}
};

const setAllNodesTabIndexes = () => {
Expand Down
Loading