Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f

### Enhancements

- Enables optional `onClick` property for `subNavigationItem` on `Nav/Item` component to execute, if provided ([#4394](https://github.com/Shopify/polaris-react/pull/4394))
- Added optional `onClick` key to `secondaryAction` on `Nav/Item` component ([#4374](https://github.com/Shopify/polaris-react/pull/4374))
- Added `id` prop to `Layout` and `Heading` for hash linking ([#4307](https://github.com/Shopify/polaris-react/pull/4307))
- Added `external` prop to `Navigation.Item` component ([#4310](https://github.com/Shopify/polaris-react/pull/4310))
Expand Down
15 changes: 13 additions & 2 deletions src/components/Navigation/components/Item/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface SubNavigationItem extends ItemURLDetails {
label: string;
disabled?: boolean;
new?: boolean;
onClick?(event: MouseEvent<HTMLElement>): void;
onClick?(): void;
}

interface SecondaryAction {
Expand Down Expand Up @@ -182,6 +182,7 @@ export function Item({
styles.Item,
disabled && styles['Item-disabled'],
keyFocused && styles.keyFocused,
selectedOverride && styles['Item-selected'],
);

return (
Expand Down Expand Up @@ -267,13 +268,23 @@ export function Item({
<Secondary expanded={showExpanded} id={secondaryNavigationId}>
{subNavigationItems.map((item) => {
const {label, ...rest} = item;
const onClick = () => {
if (onNavigationDismiss) {
onNavigationDismiss();
}

if (item.onClick && item.onClick !== onNavigationDismiss) {
item.onClick();
}
};

return (
<Item
{...rest}
key={label}
label={label}
matches={item === longestMatch}
onClick={onNavigationDismiss}
onClick={onClick}
/>
);
})}
Expand Down
67 changes: 67 additions & 0 deletions src/components/Navigation/components/Item/tests/Item.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,73 @@ describe('<Nav.Item />', () => {
'aria-expanded': true,
});
});

it('invokes the provided onClick handler', () => {
const spy = jest.fn();
const item = mountWithNavigationProvider(
<Item
label="some label"
url="/admin/orders"
subNavigationItems={[
{
url: '/admin/draft_orders',
disabled: false,
label: 'draft orders',
onClick: spy,
},
]}
/>,
{
location: '/admin/orders',
},
);

item!
.find(Secondary)!
.find('a')!
.trigger('onClick', {
preventDefault: jest.fn(),
currentTarget: {
getAttribute: () => '/admin/draft_orders',
},
});

expect(spy).toHaveBeenCalledTimes(1);
});

it('invokes onNavDismiss even if a custom onClick is provided', () => {
const spy = jest.fn();
const item = mountWithNavigationProvider(
<Item
label="some label"
url="/admin/orders"
subNavigationItems={[
{
url: '/admin/draft_orders',
disabled: false,
label: 'draft orders',
onClick: noop,
},
]}
/>,
{
location: '/admin/orders',
onNavigationDismiss: spy,
},
);

item!
.find(Secondary)!
.find('a')!
.trigger('onClick', {
preventDefault: jest.fn(),
currentTarget: {
getAttribute: () => '/admin/draft_orders',
},
});

expect(spy).toHaveBeenCalledTimes(2);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because of the recursive nature, onNavigationDismiss seems to be called twice (once at the sub nav level and once at the item level). Is that correct, @kyledurand?

Copy link
Member

Choose a reason for hiding this comment

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

Ok cool I think that makes sense 👍

});
});

describe('with exactMatch true', () => {
Expand Down