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: improve keyboard navigation and event handling for logs columns popup #6599

Closed
wants to merge 5 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,8 @@
}
}
}
.add-new-column-container {
&:focus-visible {
outline: none;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable react-hooks/exhaustive-deps */
/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/click-events-have-key-events */
import './LogsFormatOptionsMenu.styles.scss';
Expand Down Expand Up @@ -100,82 +99,101 @@ export default function LogsFormatOptionsMenu({
if (maxLinesPerRow && config && config.maxLines?.onChange) {
config.maxLines.onChange(maxLinesPerRow);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [maxLinesPerRow]);

useEffect(() => {
if (fontSizeValue && config && config.fontSize?.onChange) {
config.fontSize.onChange(fontSizeValue);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fontSizeValue]);

function handleColumnSelection(
currentIndex: number,
optionsData: DefaultOptionType[],
): void {
const currentItem = optionsData[currentIndex];
const itemLength = optionsData.length;
if (addColumn && addColumn?.onSelect) {
addColumn?.onSelect(selectedValue, {
label: currentItem.label,
disabled: false,
});

// if the last element is selected then select the previous one
if (currentIndex === itemLength - 1) {
// there should be more than 1 element in the list
if (currentIndex - 1 >= 0) {
const prevValue = optionsData[currentIndex - 1]?.value || null;
setSelectedValue(prevValue as string | null);
const handleColumnSelection = useCallback(
(currentIndex: number, optionsData: DefaultOptionType[]): void => {
const currentItem = optionsData[currentIndex];
const itemLength = optionsData.length;
if (addColumn && addColumn?.onSelect) {
addColumn?.onSelect(selectedValue, {
label: currentItem.label,
disabled: false,
});

// if the last element is selected then select the previous one
if (currentIndex === itemLength - 1) {
// there should be more than 1 element in the list
if (currentIndex - 1 >= 0) {
const prevValue = optionsData[currentIndex - 1]?.value || null;
setSelectedValue(prevValue as string | null);
} else {
// if there is only one element then just select and do nothing
setSelectedValue(null);
}
} else {
// if there is only one element then just select and do nothing
setSelectedValue(null);
}
} else {
// selecting any random element from the list except the last one
const nextIndex = currentIndex + 1;
// selecting any random element from the list except the last one
const nextIndex = currentIndex + 1;

const nextValue = optionsData[nextIndex]?.value || null;
const nextValue = optionsData[nextIndex]?.value || null;

setSelectedValue(nextValue as string | null);
setSelectedValue(nextValue as string | null);
}
}
}
}
},
[addColumn, selectedValue],
);

const handleKeyDown = (e: KeyboardEvent): void => {
if (!selectedValue) return;
const handleKeyDown = useCallback(
(e: React.KeyboardEvent): void => {
if (!addColumn?.options) return;
const optionsData = addColumn.options;

const optionsData = addColumn?.options || [];
const currentIndex = optionsData.findIndex(
(item) => item?.value === selectedValue,
);

const currentIndex = optionsData.findIndex(
(item) => item?.value === selectedValue,
);
const itemLength = optionsData.length;

const itemLength = optionsData.length;
if (currentIndex === -1) {
// if there is not option selected, and the user presses enter, do nothing
if (e.key === 'Enter') return;

switch (e.key) {
case 'ArrowUp': {
const newValue = optionsData[Math.max(0, currentIndex - 1)]?.value;
if (e.key === 'ArrowDown' && optionsData.length > 0) {
setSelectedValue(optionsData[0].value as string);
e.preventDefault();
return;
}

setSelectedValue(newValue as string | null);
e.preventDefault();
break;
if (e.key === 'ArrowUp' && optionsData.length > 0) {
setSelectedValue(optionsData[optionsData.length - 1].value as string);
e.preventDefault();
return;
}
}
case 'ArrowDown': {
const newValue =
optionsData[Math.min(itemLength - 1, currentIndex + 1)]?.value;

setSelectedValue(newValue as string | null);
e.preventDefault();
break;
switch (e.key) {
case 'ArrowUp': {
const newValue = optionsData[Math.max(0, currentIndex - 1)]?.value;
setSelectedValue(newValue as string | null);
e.preventDefault();
break;
}
case 'ArrowDown': {
const newValue =
optionsData[Math.min(itemLength - 1, currentIndex + 1)]?.value;
setSelectedValue(newValue as string | null);
e.preventDefault();
break;
}
case 'Enter':
e.preventDefault();
handleColumnSelection(currentIndex, optionsData);
break;
default:
break;
}
case 'Enter':
e.preventDefault();
handleColumnSelection(currentIndex, optionsData);
break;
default:
break;
}
};
},
[addColumn?.options, selectedValue, handleColumnSelection],
);

useEffect(() => {
// Scroll the selected item into view
Expand All @@ -193,14 +211,7 @@ export default function LogsFormatOptionsMenu({
});
}
}
}, [selectedValue]);

useEffect(() => {
window.addEventListener('keydown', handleKeyDown);
return (): void => {
window.removeEventListener('keydown', handleKeyDown);
};
}, [selectedValue]);
}, [addColumn?.options, selectedValue]);

return (
<div
Expand Down Expand Up @@ -266,7 +277,13 @@ export default function LogsFormatOptionsMenu({
) : null}

{showAddNewColumnContainer && (
<div className="add-new-column-container">
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
<div
className="add-new-column-container"
onKeyDown={handleKeyDown}
role="dialog"
tabIndex={-1}
>
<div className="add-new-column-header">
<div className="title">
<div className="periscope-btn ghost" onClick={handleToggleAddNewColumn}>
Expand Down
Loading