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

feat(Nav): Added default nav item wrapper. Added support for nav item icons #10687

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 5 additions & 5 deletions packages/react-core/src/components/Nav/NavItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ export interface NavItemProps extends Omit<React.HTMLProps<HTMLAnchorElement>, '
onShowFlyout?: () => void;
/** z-index of the flyout nav item */
zIndex?: number;
/** Icon added before the nav item children. */
icon?: React.ReactNode;
/** Value to overwrite the randomly generated data-ouia-component-id.*/
ouiaId?: number | string;
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
ouiaSafe?: boolean;
/** Adds a wrapper around the nav link text. Improves the layout when the text is a react node. */
hasNavLinkWrapper?: boolean;
}

export const NavItem: React.FunctionComponent<NavItemProps> = ({
Expand All @@ -60,7 +60,7 @@ export const NavItem: React.FunctionComponent<NavItemProps> = ({
ouiaId,
ouiaSafe,
zIndex = 9999,
hasNavLinkWrapper,
icon,
...props
}: NavItemProps) => {
const { flyoutRef, setFlyoutRef, navRef } = React.useContext(NavContext);
Expand Down Expand Up @@ -193,8 +193,8 @@ export const NavItem: React.FunctionComponent<NavItemProps> = ({
{...(hasFlyout && { ...ariaFlyoutProps })}
{...props}
>
{hasNavLinkWrapper ? <span className={css(`${styles.nav}__link-text`)}>{children}</span> : children}
{flyout && flyoutButton}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be added back in. Right now the Flyout example doesn't render the icon to indicate the item has any sort of interaction (item "Flyout link 3" in the following screenshot):

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops that was a paste mistake! good catch!

{icon && <span className={css(`${styles.nav}__link-icon`)}>{icon}</span>}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{icon && <span className={css(`${styles.nav}__link-icon`)}>{icon}</span>}
{icon && <span className={css(styles.navLinkIcon)}>{icon}</span>}

<span className={css(`${styles.nav}__link-text`)}>{children}</span>
</Component>
);
};
Expand Down
18 changes: 18 additions & 0 deletions packages/react-core/src/components/Nav/__tests__/Nav.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import * as React from 'react';

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';

import { Nav, NavContext } from '../Nav';
import { NavList } from '../NavList';
import { NavGroup } from '../NavGroup';
import { NavItem } from '../NavItem';
import { NavExpandable } from '../NavExpandable';

import styles from '@patternfly/react-styles/css/components/Nav/nav';

const props = {
items: [
{ to: '#link1', label: 'Link 1' },
Expand Down Expand Up @@ -242,4 +245,19 @@ describe('Nav', () => {
await user.hover(screen.getByText('My custom node'));
expect(asFragment()).toMatchSnapshot();
});

test('Nav with icon', () => {
renderNav(
<Nav>
<NavList className="test-nav-icon">
{props.items.map((item) => (
<NavItem to={item.to} key={item.to} icon={<span>this is an icon</span>} className="test-nav-item-class">
{item.label}
</NavItem>
))}
</NavList>
</Nav>
);
expect(screen.getAllByText('this is an icon')[0].parentElement).toHaveClass(`${styles.nav}__link-icon`);
});
});
Loading
Loading