Skip to content

Commit c46045a

Browse files
authored
fix(SideNav): update check to verify selected sidebar item (#291)
2 parents 3752319 + a99e59c commit c46045a

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/SideNav/SideNavButton.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import { useContext } from 'react';
33
import classNames from 'classnames';
44
import PropTypes from 'prop-types';
5-
import SideNavContext, { SideNavEventKey } from './SideNavContext';
5+
import SideNavContext, { SideNavEventKey, isSideNavItemSelected } from './SideNavContext';
66
import SideNavItemContext from './SideNavItemContext';
77
import {
88
BsPrefixProps,
@@ -15,7 +15,7 @@ type EventHandler = React.EventHandler<React.SyntheticEvent>;
1515

1616
export interface SideNavButtonProps
1717
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
18-
BsPrefixProps {
18+
BsPrefixProps {
1919
/** Providing a `href` will render an `<a>` element, _styled_ as a button. */
2020
href?: string;
2121
}
@@ -108,12 +108,12 @@ export const SideNavButton: BsPrefixRefForwardingComponent<
108108
onClick={sideNavOnClick}
109109
{...props}
110110
aria-expanded={eventKey === activeEventKey}
111-
aria-haspopup="menu"
111+
aria-haspopup='menu'
112112
className={classNames(
113113
className,
114114
setCollapseCSS(activeEventKey, eventKey),
115115
// add active class when sidenav item is open or when multiple side nav items are open during alwaysOpen
116-
(eventKey === activeEventKey || activeEventKey?.includes(eventKey)) && "active"
116+
isSideNavItemSelected(activeEventKey, eventKey) ? 'active' : ''
117117
)}
118118
>
119119
{children}

tests/SideNav/SideNav.test.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ const Component = ({alwaysOpen, initialActiveKey}: ComponentProps) => {
323323
SideNav Item #3
324324
</SideNav.Button>
325325
</SideNav.Item>
326+
<SideNav.Item eventKey="00">
327+
<SideNav.Button onClick={() => clickButtonLink('00')} href="#">
328+
SideNav Item #11
329+
</SideNav.Button>
330+
</SideNav.Item>
326331
</SideNav>
327332
);
328333
};
@@ -377,6 +382,9 @@ describe('Active style added to Sidenav when ', () => {
377382
expect(container.querySelectorAll('.btn')[2].classList).not.toContain(
378383
'active'
379384
);
385+
expect(container.querySelectorAll('.btn')[3].classList).not.toContain(
386+
'active'
387+
);
380388

381389
fireEvent.click(getByText('SideNav Item #1'));
382390
await waitFor(() => {
@@ -389,6 +397,25 @@ describe('Active style added to Sidenav when ', () => {
389397
expect(container.querySelectorAll('.btn')[2].classList).not.toContain(
390398
'active'
391399
);
400+
expect(container.querySelectorAll('.btn')[3].classList).not.toContain(
401+
'active'
402+
);
403+
});
404+
405+
fireEvent.click(getByText('SideNav Item #11'));
406+
await waitFor(() => {
407+
expect(container.querySelectorAll('.btn')[0].classList).not.toContain(
408+
'active'
409+
);
410+
expect(container.querySelectorAll('.btn')[1].classList).not.toContain(
411+
'active'
412+
);
413+
expect(container.querySelectorAll('.btn')[2].classList).not.toContain(
414+
'active'
415+
);
416+
expect(container.querySelectorAll('.btn')[3].classList).toContain(
417+
'active'
418+
);
392419
});
393420
})
394421
it("on click on sidenav button in alwaysOpen mode", async() => {
@@ -403,6 +430,9 @@ describe('Active style added to Sidenav when ', () => {
403430
expect(container.querySelectorAll('.btn')[2].classList).not.toContain(
404431
'active'
405432
);
433+
expect(container.querySelectorAll('.btn')[3].classList).not.toContain(
434+
'active'
435+
);
406436

407437
// fireEvent.click(getByText('SideNav Item #1'));
408438
// await waitFor(() => {
@@ -418,4 +448,4 @@ describe('Active style added to Sidenav when ', () => {
418448
// });
419449
})
420450

421-
})
451+
})

0 commit comments

Comments
 (0)