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

fix: sub menu links should open the correct herf #167

Merged
merged 2 commits into from
Mar 22, 2024
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ReactNode, type MouseEvent } from 'react'
import { type HrefOptions } from 'src/utils/utils'

export interface IBaseGlobalNavigationItem {
type?: 'menu' | 'link'
Expand All @@ -20,9 +21,9 @@ export interface IGlobalNavigationMenu extends IBaseGlobalNavigationItem {

export interface IGlobalNavigationLink extends IBaseGlobalNavigationItem {
type?: 'link'
hrefOptions?: { href: string; hrefTarget?: '_self' | '_blank' }
hrefOptions?: HrefOptions
hideLabel?: boolean
onClick?: (e: MouseEvent) => void
}

export type IGlobalNavigationItem = IGlobalNavigationMenu | IGlobalNavigationLink
export type IGlobalNavigationItem = IGlobalNavigationMenu | IGlobalNavigationLink
18 changes: 6 additions & 12 deletions src/components/navigation/GlobalNavigation/NavigationItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type MouseEvent, type ReactNode } from 'react'
import { NavigationIcon } from 'src/components/navigation/GlobalNavigation/NavigationIcon'
import { NavigationList } from 'src/components/navigation/GlobalNavigation/NavigationList'
import { type IGlobalNavigationItem, Tooltip } from 'src/components'
import { buildLinkFromHrefOptions, type HrefOptions } from 'src/utils/utils'

export interface INavigationItemProps {
type: 'link' | 'menu'
Expand All @@ -11,7 +12,7 @@ export interface INavigationItemProps {
items?: IGlobalNavigationItem[]
isActive?: boolean
onClick?: (e: MouseEvent) => void // link only
hrefOptions?: { href: string; hrefTarget?: '_self' | '_blank' } // link only
hrefOptions?: HrefOptions // link only
}

export function NavigationItem(props: INavigationItemProps) {
Expand All @@ -31,16 +32,9 @@ export function NavigationItem(props: INavigationItemProps) {
/>
)

const resultNavigationIcon = props.hrefOptions ? (
<a
href={props.hrefOptions.href}
target={props.hrefOptions.hrefTarget ?? '_self'}
rel={props.hrefOptions.hrefTarget === '_blank' ? 'noopener' : undefined}>
{navigationIcon}
</a>
) : (
navigationIcon
)
const resultNavigationIcon = props.hrefOptions
? buildLinkFromHrefOptions(navigationIcon, props.hrefOptions)
: navigationIcon

if (props.hideLabel) {
return (
Expand All @@ -51,4 +45,4 @@ export function NavigationItem(props: INavigationItemProps) {
}

return resultNavigationIcon
}
}
4 changes: 2 additions & 2 deletions src/components/navigation/GlobalNavigation/NavigationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Center } from 'src/components'
import { type IGlobalNavigationItem } from 'src/components/navigation/GlobalNavigation/GlobalNavigationItems'
import { type IGlobalNavigationLink } from 'src/components/navigation/GlobalNavigation/GlobalNavigationItems'
import { Fragment } from 'react'
import { buildLinkFromHrefOptions } from 'src/utils/utils'

export interface INavigationListProps {
items: IGlobalNavigationItem[]
Expand Down Expand Up @@ -43,11 +44,10 @@ function generateMenuItem(item: IGlobalNavigationItem, i: number) {
...linkItem,
expandIcon: linkItem.isNestedMenu ? true : null,
key: `${String(linkItem.label)}${j}`,
label: <a href={linkItem.href}>{linkItem.label}</a>,
label: buildLinkFromHrefOptions(linkItem.label, linkItem.hrefOptions),
})),
)
}

const navigationIcon = (
<NavigationIcon
icon={item.icon}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ describe('Testing utils', () => {
expect(actualOS).toBe('Macintosh')
})
})
})
})
15 changes: 15 additions & 0 deletions src/utils/utils.ts → src/utils/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type ReactNode } from 'react'

export function getInitials(str: string = ''): string {
const getInitialsRegex = new RegExp(
'(?:[\\W\\d]*\\b)*' + // Ignore non-word abd number characters that may exist at the beginning of the word
Expand Down Expand Up @@ -48,3 +50,16 @@ export function hasImageAtSrc(src: string, hasImageSetter?: (hasImage: boolean)
img.src = src
})
}

export type HrefOptions = { href: string; hrefTarget?: '_self' | '_blank' }

export function buildLinkFromHrefOptions(label: ReactNode, hrefOptions?: HrefOptions): ReactNode {
return (
<a
href={hrefOptions?.href}
target={hrefOptions?.hrefTarget ?? '_self'}
rel={hrefOptions?.hrefTarget === '_blank' ? 'noopener' : undefined}>
{label}
</a>
)
}
Loading