Skip to content

Commit 73701d2

Browse files
committed
feat(NavExpandable): allow option to pass an icon
1 parent 4ca2e04 commit 73701d2

4 files changed

Lines changed: 46 additions & 7 deletions

File tree

packages/react-core/src/components/Nav/NavExpandable.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export interface NavExpandableProps
3030
onExpand?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>, val: boolean) => void;
3131
/** Additional props added to the NavExpandable <button> */
3232
buttonProps?: any;
33+
/** Icon added before the nav item children. */
34+
icon?: React.ReactNode;
3335
/** Value to overwrite the randomly generated data-ouia-component-id.*/
3436
ouiaId?: number | string;
3537
}
@@ -84,6 +86,7 @@ class NavExpandable extends Component<NavExpandableProps, NavExpandableState> {
8486

8587
render() {
8688
const {
89+
icon,
8790
title,
8891
srText,
8992
children,
@@ -132,11 +135,8 @@ class NavExpandable extends Component<NavExpandableProps, NavExpandableState> {
132135
tabIndex={isSidebarOpen ? null : -1}
133136
{...buttonProps}
134137
>
135-
{typeof title !== 'string' ? (
136-
<span className={css(`${styles.nav}__link-text`)}>{title}</span>
137-
) : (
138-
title
139-
)}
138+
{icon && <span className={css(styles.navLinkIcon)}>{icon}</span>}
139+
{typeof title !== 'string' ? <span className={css(styles.navLinkText)}>{title}</span> : title}
140140
<span className={css(styles.navToggle)}>
141141
<span className={css(styles.navToggleIcon)}>
142142
<RhMicronsCaretDownIcon />

packages/react-core/src/components/Nav/__tests__/NavExpandable.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,20 @@ test('Does not render with the inert attribute when isExpanded is true', () => {
1313

1414
expect(screen.getByLabelText('NavExpandable')).not.toHaveAttribute('inert', '');
1515
});
16+
17+
test('Renders icon wrapped in nav link icon class when icon prop is provided', () => {
18+
render(
19+
<NavExpandable id="grp-1" title="NavExpandable" icon={<span data-testid="test-icon">Icon</span>}></NavExpandable>
20+
);
21+
22+
const icon = screen.getByTestId('test-icon');
23+
expect(icon).toBeInTheDocument();
24+
expect(icon.parentElement).toHaveClass('pf-v6-c-nav__link-icon');
25+
});
26+
27+
test('Does not render nav link icon wrapper when icon prop is not provided', () => {
28+
render(<NavExpandable id="grp-1" title="NavExpandable"></NavExpandable>);
29+
30+
const button = screen.getByRole('button', { name: 'NavExpandable' });
31+
expect(button.querySelector('.pf-v6-c-nav__link-icon')).not.toBeInTheDocument();
32+
});

packages/react-core/src/components/Nav/examples/Nav.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import ArrowRightIcon from '@patternfly/react-icons/dist/esm/icons/arrow-right-i
1212
import UserIcon from '@patternfly/react-icons/dist/esm/icons/user-icon';
1313
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon';
1414
import FolderIcon from '@patternfly/react-icons/dist/esm/icons/folder-icon';
15+
import FolderOpenIcon from '@patternfly/react-icons/dist/esm/icons/folder-open-icon';
1516
import CloudIcon from '@patternfly/react-icons/dist/esm/icons/cloud-icon';
1617
import LinkIcon from '@patternfly/react-icons/dist/esm/icons/link-icon';
1718

@@ -75,7 +76,7 @@ A flyout should be a `Menu` component. Press `space` or `right arrow` to open a
7576

7677
```
7778

78-
### With item icons
79+
### With icons
7980

8081
```ts file="./NavIcons.tsx"
8182

packages/react-core/src/components/Nav/examples/NavIcons.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { useState } from 'react';
2-
import { Nav, NavItem, NavList } from '@patternfly/react-core';
2+
import { Nav, NavExpandable, NavItem, NavList } from '@patternfly/react-core';
33
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon';
44
import FolderIcon from '@patternfly/react-icons/dist/esm/icons/folder-icon';
55
import CloudIcon from '@patternfly/react-icons/dist/esm/icons/cloud-icon';
6+
import FolderOpenIcon from '@patternfly/react-icons/dist/esm/icons/folder-open-icon';
67
import LinkIcon from '@patternfly/react-icons/dist/esm/icons/link-icon';
78

89
export const NavIcons: React.FunctionComponent = () => {
@@ -55,6 +56,26 @@ export const NavIcons: React.FunctionComponent = () => {
5556
>
5657
Link 4
5758
</NavItem>
59+
<NavExpandable title="Expandable" icon={<FolderOpenIcon />} groupId="nav-icon-expandable">
60+
<NavItem
61+
preventDefault
62+
id="nav-icon-expandable-link1"
63+
to="#nav-icon-expandable-link1"
64+
itemId={4}
65+
isActive={activeItem === 4}
66+
>
67+
Subnav link 1
68+
</NavItem>
69+
<NavItem
70+
preventDefault
71+
id="nav-icon-expandable-link2"
72+
to="#nav-icon-expandable-link2"
73+
itemId={5}
74+
isActive={activeItem === 5}
75+
>
76+
Subnav link 2
77+
</NavItem>
78+
</NavExpandable>
5879
</NavList>
5980
</Nav>
6081
);

0 commit comments

Comments
 (0)