Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
UXPLATFORM-10191 Refactored focus logic
Browse files Browse the repository at this point in the history
  • Loading branch information
adavijit committed Apr 17, 2024
1 parent 4670cee commit c497d54
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
3 changes: 3 additions & 0 deletions packages/terra-form-select/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Added
* Added visual focus dashed border for `terra-form-select` tags.

* Fixed
* Fixed accessibility issue in `MultiSelect` component.

Expand Down
37 changes: 22 additions & 15 deletions packages/terra-form-select/src/shared/_Tag.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,42 @@ const Tag = ({
children, onDeselect, value, disabled, intl,
}) => {
const theme = React.useContext(ThemeContext);
const deselectRef = useRef(null);
const tagRef = useRef(null);

const handleEnterKeyPress = (event) => {
if (event.key === 'Enter' && !disabled) {
if ((event.key === 'Enter' || event.key === 'Backspace') && !disabled) {
event.stopPropagation();
onDeselect(value);
const parentUl = deselectRef.current.closest('ul');
// Filtering out hidden children
const visibleChildrenLis = Array.from(parentUl.children).filter(child => child.tagName === 'LI' && !/visually-hidden-component/.test(child.className));
if (visibleChildrenLis.length >= 2) {
const secondLastLi = visibleChildrenLis[visibleChildrenLis.length - 2];
if (secondLastLi) {
const spanChildren = secondLastLi.children;
if (spanChildren[1]) {
spanChildren[1].focus();
}
const previousLi = tagRef.current.previousElementSibling;

if (previousLi) {
const deselectElement = previousLi.children[1];
if (deselectElement) {
deselectElement.focus();
}
} else {
parentUl.click();
const nextLi = tagRef.current.nextElementSibling;
const parentUl = tagRef.current.closest('ul');
if (nextLi) {
const deselectElement = nextLi.children[1];
if (deselectElement) {
deselectElement.focus();
} else {
parentUl.click();
}
} else {
parentUl.click();
}
}
}
};
return (
<li className={cx('tag', theme.className)}>
<li className={cx('tag', theme.className)} ref={tagRef}>
<span className={cx('display')}>
{children}
</span>
<span
id={`terra-tag-deselect-${value}`}
ref={deselectRef}
onKeyDown={handleEnterKeyPress}
className={cx('deselect')}
onClick={() => { if (!disabled) onDeselect(value); }}
Expand Down

0 comments on commit c497d54

Please sign in to comment.