Skip to content

Commit

Permalink
use navigateToUrl to navigate to recent nav links (#77446)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet authored Sep 16, 2020
1 parent ab92bbb commit 7df0b34
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/core/public/chrome/ui/header/collapsible_nav.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function mockProps() {
onIsLockedUpdate: () => {},
closeNav: () => {},
navigateToApp: () => Promise.resolve(),
navigateToUrl: () => Promise.resolve(),
customNavLink$: new BehaviorSubject(undefined),
};
}
Expand Down
16 changes: 11 additions & 5 deletions src/core/public/chrome/ui/header/collapsible_nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ interface Props {
onIsLockedUpdate: OnIsLockedUpdate;
closeNav: () => void;
navigateToApp: InternalApplicationStart['navigateToApp'];
navigateToUrl: InternalApplicationStart['navigateToUrl'];
customNavLink$: Rx.Observable<ChromeNavLink | undefined>;
}

Expand All @@ -100,6 +101,7 @@ export function CollapsibleNav({
onIsLockedUpdate,
closeNav,
navigateToApp,
navigateToUrl,
...observables
}: Props) {
const navLinks = useObservable(observables.navLinks$, []).filter((link) => !link.hidden);
Expand Down Expand Up @@ -217,17 +219,21 @@ export function CollapsibleNav({
listItems={recentlyAccessed.map((link) => {
// TODO #64541
// Can remove icon from recent links completely
const { iconType, ...hydratedLink } = createRecentNavLink(link, navLinks, basePath);
const { iconType, onClick, ...hydratedLink } = createRecentNavLink(
link,
navLinks,
basePath,
navigateToUrl
);

return {
...hydratedLink,
'data-test-subj': 'collapsibleNavAppLink--recent',
onClick: (event) => {
if (isModifiedOrPrevented(event)) {
return;
if (!isModifiedOrPrevented(event)) {
closeNav();
onClick(event);
}

closeNav();
},
};
})}
Expand Down
1 change: 1 addition & 0 deletions src/core/public/chrome/ui/header/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ export function Header({
homeHref={homeHref}
basePath={basePath}
navigateToApp={application.navigateToApp}
navigateToUrl={application.navigateToUrl}
onIsLockedUpdate={onIsLockedUpdate}
closeNav={() => {
setIsNavOpen(false);
Expand Down
14 changes: 12 additions & 2 deletions src/core/public/chrome/ui/header/nav_link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { i18n } from '@kbn/i18n';
import React from 'react';
import { ChromeNavLink, ChromeRecentlyAccessedHistoryItem, CoreStart } from '../../..';
import { HttpStart } from '../../../http';
import { InternalApplicationStart } from '../../../application/types';
import { relativeToAbsolute } from '../../nav_links/to_nav_link';

export const isModifiedOrPrevented = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) =>
Expand Down Expand Up @@ -87,6 +88,7 @@ export interface RecentNavLink {
title: string;
'aria-label': string;
iconType?: string;
onClick: React.MouseEventHandler;
}

/**
Expand All @@ -102,8 +104,9 @@ export interface RecentNavLink {
export function createRecentNavLink(
recentLink: ChromeRecentlyAccessedHistoryItem,
navLinks: ChromeNavLink[],
basePath: HttpStart['basePath']
) {
basePath: HttpStart['basePath'],
navigateToUrl: InternalApplicationStart['navigateToUrl']
): RecentNavLink {
const { link, label } = recentLink;
const href = relativeToAbsolute(basePath.prepend(link));
const navLink = navLinks.find((nl) => href.startsWith(nl.baseUrl));
Expand All @@ -125,5 +128,12 @@ export function createRecentNavLink(
title: titleAndAriaLabel,
'aria-label': titleAndAriaLabel,
iconType: navLink?.euiIconType,
/* Use href and onClick to support "open in new tab" and SPA navigation in the same link */
onClick(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) {
if (event.button === 0 && !isModifiedOrPrevented(event)) {
event.preventDefault();
navigateToUrl(href);
}
},
};
}

0 comments on commit 7df0b34

Please sign in to comment.