Skip to content

Commit

Permalink
Fixes #37025 - Navigation should only have 1 item expended
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaAga authored and ofedoren committed Jan 4, 2024
1 parent 5423d2c commit bf39cd0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ const Navigation = ({
[items.length, currentPath]
);

const [currentExpanded, setCurrentExpanded] = useState(
subItemToItemMap[pathFragment(getCurrentPath())]
);
useEffect(() => {
setCurrentExpanded(subItemToItemMap[pathFragment(getCurrentPath())]);
// we only want to run this when we get new items from the API to set the default expanded item, which is the current location
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [items.length]);
if (!items.length) return null;

const clickAndNavigate = (_onClick, href, event) => {
Expand All @@ -104,18 +112,22 @@ const Navigation = ({
<NavExpandable
ouiaId={`nav-expandable-${index}`}
title={titleWithIcon(title, iconClass)}
groupId="nav-expandable-group-1"
groupId={`nav-expandable-group-${title}`}
isActive={
subItemToItemMap[pathFragment(getCurrentPath())] === title
}
isExpanded={
subItemToItemMap[pathFragment(getCurrentPath())] === title
}
isExpanded={currentExpanded === title}
className={className}
onClick={() => onMouseOver(index)}
onFocus={() => {
onMouseOver(index);
}}
onExpand={() => {
// if the current expanded item is the same as the clicked item, collapse it
const isExpanded = currentExpanded === title;
// only have 1 item expanded at a time
setCurrentExpanded(isExpanded ? null : title);
}}
>
{groups.map((group, groupIndex) =>
groupIndex === 0 ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ describe('Layout', () => {
await act(async () => {
await fireEvent.click(screen.getByText('Hosts'));
});
expect(screen.getByText('Monitor')).toBeVisible();
expect(screen.getByText('Dashboard')).not.toBeVisible();
expect(screen.getByText('All Hosts')).toBeVisible();
expect(screen.getByText('Dashboard')).toBeVisible();
});
});

0 comments on commit bf39cd0

Please sign in to comment.