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 6631 DragDrop Tree keyboard navigation #6646

Merged
merged 4 commits into from
May 24, 2024
Merged
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
3 changes: 2 additions & 1 deletion components/lib/tree/Tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export const Tree = React.memo(
nodeElement.tabIndex = '-1';
}

const childElement = listElement.children[0];
//skip droppoint
const childElement = props.dragdropScope ? listElement.children[1] : listElement.children[0];

if (childElement) {
childElement.tabIndex = '0';
Expand Down
60 changes: 53 additions & 7 deletions components/lib/tree/UITreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,41 @@ export const UITreeNode = React.memo((props) => {
}
};

const findNextNonDroppointSibling = (nodeElement) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conditions can be refactored

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to study this one...

const nextNodeSibling = nodeElement.nextSibling;

if (nextNodeSibling) {
const isNextDropPoint = nextNodeSibling.getAttribute('data-pc-section') === 'droppoint';

if (isNextDropPoint) {
//skip drop point and return next elemnt
if (nextNodeSibling.nextElementSibling) {
return nextNodeSibling.nextElementSibling;
} else {
//nothing after droppoint go outside
return null;
}
}

return nextNodeSibling;
}

return null;
};

const findNextSiblingOfAncestor = (nodeElement) => {
const parentNodeElement = getParentNodeElement(nodeElement);

return parentNodeElement ? parentNodeElement.nextElementSibling || findNextSiblingOfAncestor(parentNodeElement) : null;
return parentNodeElement ? findNextNonDroppointSibling(parentNodeElement) || findNextSiblingOfAncestor(parentNodeElement) : null;
};

const findLastVisibleDescendant = (nodeElement) => {
const childrenListElement = nodeElement.children[1];

if (childrenListElement) {
const lastChildElement = childrenListElement.children[childrenListElement.children.length - 1];
//skip droppoint
const offset = props.dragdropScope ? 2 : 1;
const lastChildElement = childrenListElement.children[childrenListElement.children.length - offset];

return findLastVisibleDescendant(lastChildElement);
}
Expand Down Expand Up @@ -344,11 +368,12 @@ 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, listElement.children[0]);
} else if (nodeElement.nextElementSibling) {
focusRowChange(nodeElement, nodeElement.nextElementSibling);
focusRowChange(nodeElement, props.dragdropScope ? listElement.children[1] : listElement.children[0]);
} else if (nextElement) {
focusRowChange(nodeElement, nextElement);
} else {
let nextSiblingAncestor = findNextSiblingOfAncestor(nodeElement);

Expand All @@ -360,11 +385,32 @@ export const UITreeNode = React.memo((props) => {
event.preventDefault();
};

const getPreviousElement = (element) => {
const prev = element.previousElementSibling;

if (prev) {
return !props.dragdropScope ? prev : prev.previousElementSibling;
}

return null;
};

const getNextElement = (element) => {
const next = element.nextElementSibling;

if (next) {
return !props.dragdropScope ? next : next.nextElementSibling;
}

return null;
};

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

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

Expand Down
Loading