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

PT-1753 | Dropdown keyboard navigation logic #330

Merged
merged 1 commit into from
Sep 26, 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
2 changes: 1 addition & 1 deletion src/assets/styles/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ button {
cursor: pointer;

&:focus {
outline: 2px solid $focusOutline;
outline: 3px solid $focusOutline;
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/common/components/multiSelectDropdown/MultiSelectDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const MultiSelectDropdown: React.FC<MultiselectDropdownProps> = ({
const inputPlaceholderText =
inputPlaceholder || t('common:multiSelectDropdown.inputPlaceholder');
const [internalInput, setInternalInput] = React.useState('');
const [focusStyles, setFocusStyles] = React.useState(false);
const input = inputValue !== undefined ? inputValue : internalInput;

const dropdown = React.useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -88,6 +89,8 @@ const MultiSelectDropdown: React.FC<MultiselectDropdownProps> = ({
[setInputValue]
);

const handleToggleFocusStyles = () => setFocusStyles((prev) => !prev);

const {
focusedIndex,
setup: setupKeyboardNav,
Expand All @@ -114,6 +117,9 @@ const MultiSelectDropdown: React.FC<MultiselectDropdownProps> = ({
case 'Enter':
if (isToggleButtonFocused()) {
handleToggleButtonClick();
} else {
setIsMenuOpen(false);
setFocusToToggleButton();
}
event.preventDefault();
}
Expand All @@ -124,7 +130,6 @@ const MultiSelectDropdown: React.FC<MultiselectDropdownProps> = ({
const isComponentFocused = () => {
const active = document.activeElement;
const current = dropdown.current;

return !!current?.contains(active);
};

Expand Down Expand Up @@ -158,7 +163,6 @@ const MultiSelectDropdown: React.FC<MultiselectDropdownProps> = ({
const isToggleButtonFocused = () => {
const active = document.activeElement;
const current = toggleButton.current;

return !!current?.contains(active);
};

Expand Down Expand Up @@ -293,6 +297,8 @@ const MultiSelectDropdown: React.FC<MultiselectDropdownProps> = ({
className={styles.toggleButton}
onClick={handleToggleButtonClick}
ref={toggleButton}
onFocus={handleToggleFocusStyles}
onBlur={handleToggleFocusStyles}
>
<div className={styles.iconWrapper}>{icon}</div>
<div className={styles.title}>
Expand All @@ -305,7 +311,12 @@ const MultiSelectDropdown: React.FC<MultiselectDropdownProps> = ({
<div className={styles.placeholder}>{title}</div>
)}
</div>
<div className={styles.arrowWrapper}>
<div
className={classNames(
styles.arrowWrapper,
focusStyles ? styles.focused : ''
)}
>
{isMenuOpen ? <IconAngleUp /> : <IconAngleDown />}
</div>
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@
.arrowWrapper {
display: flex;
align-self: center;
margin-bottom: 2px;

&.focused {
border: var(--color-coat-of-arms) solid 3px;
}

svg {
margin-left: 0.75rem;
pointer-events: none;
}
}
Expand Down
Loading