Skip to content

Commit bf39cd0

Browse files
MariaAgaofedoren
authored andcommitted
Fixes #37025 - Navigation should only have 1 item expended
1 parent 5423d2c commit bf39cd0

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

webpack/assets/javascripts/react_app/components/Layout/Navigation.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ const Navigation = ({
8282
[items.length, currentPath]
8383
);
8484

85+
const [currentExpanded, setCurrentExpanded] = useState(
86+
subItemToItemMap[pathFragment(getCurrentPath())]
87+
);
88+
useEffect(() => {
89+
setCurrentExpanded(subItemToItemMap[pathFragment(getCurrentPath())]);
90+
// 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
91+
// eslint-disable-next-line react-hooks/exhaustive-deps
92+
}, [items.length]);
8593
if (!items.length) return null;
8694

8795
const clickAndNavigate = (_onClick, href, event) => {
@@ -104,18 +112,22 @@ const Navigation = ({
104112
<NavExpandable
105113
ouiaId={`nav-expandable-${index}`}
106114
title={titleWithIcon(title, iconClass)}
107-
groupId="nav-expandable-group-1"
115+
groupId={`nav-expandable-group-${title}`}
108116
isActive={
109117
subItemToItemMap[pathFragment(getCurrentPath())] === title
110118
}
111-
isExpanded={
112-
subItemToItemMap[pathFragment(getCurrentPath())] === title
113-
}
119+
isExpanded={currentExpanded === title}
114120
className={className}
115121
onClick={() => onMouseOver(index)}
116122
onFocus={() => {
117123
onMouseOver(index);
118124
}}
125+
onExpand={() => {
126+
// if the current expanded item is the same as the clicked item, collapse it
127+
const isExpanded = currentExpanded === title;
128+
// only have 1 item expanded at a time
129+
setCurrentExpanded(isExpanded ? null : title);
130+
}}
119131
>
120132
{groups.map((group, groupIndex) =>
121133
groupIndex === 0 ? (

webpack/assets/javascripts/react_app/components/Layout/__tests__/Layout.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ describe('Layout', () => {
2828
await act(async () => {
2929
await fireEvent.click(screen.getByText('Hosts'));
3030
});
31+
expect(screen.getByText('Monitor')).toBeVisible();
32+
expect(screen.getByText('Dashboard')).not.toBeVisible();
3133
expect(screen.getByText('All Hosts')).toBeVisible();
32-
expect(screen.getByText('Dashboard')).toBeVisible();
3334
});
3435
});

0 commit comments

Comments
 (0)