Skip to content

Commit

Permalink
Let Button handle logic and add LinkComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
psealock committed Aug 19, 2020
1 parent 52cf7ef commit 8864e06
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
44 changes: 29 additions & 15 deletions packages/components/src/navigation/menu-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const NavigationMenuItem = ( props ) => {
href,
id,
isActive,
linkTag,
linkTagProps,
LinkComponent,
linkProps,
onClick,
setActiveLevel,
title,
Expand All @@ -41,22 +41,36 @@ const NavigationMenuItem = ( props ) => {
onClick( props );
};

const LinkTagName = linkTag || Button;
const linkContents = (
<>
<Text variant="body.small">
<span>{ title }</span>
</Text>
{ badge && <BadgeUI>{ badge }</BadgeUI> }
{ hasChildren ? <Icon icon={ chevronRight } /> : null }
</>
);

return (
<MenuItemUI className={ classes }>
<LinkTagName
className={ classes }
href={ href }
onClick={ handleClick }
{ ...linkTagProps }
>
<Text variant="body.small">
<span>{ title }</span>
</Text>
{ badge && <BadgeUI>{ badge }</BadgeUI> }
{ hasChildren ? <Icon icon={ chevronRight } /> : null }
</LinkTagName>
{ LinkComponent ? (
<LinkComponent
className={ classes }
onClick={ handleClick }
{ ...linkProps }
>
{ linkContents }
</LinkComponent>
) : (
<Button
className={ classes }
href={ href }
onClick={ handleClick }
{ ...linkProps }
>
{ linkContents }
</Button>
) }
</MenuItemUI>
);
};
Expand Down
17 changes: 14 additions & 3 deletions packages/components/src/navigation/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export default {
component: Navigation,
};

// Example Link component from a router such as React Router
const CustomRouterLink = ( { children, onClick } ) => {
// Here I'm passing the onClick prop for simplicity, but behavior can be
// anything here.
return <Button onClick={ onClick }>{ children }</Button>;
};

const data = [
{
title: 'Item 1',
Expand All @@ -42,14 +49,18 @@ const data = [
parent: 'item-3',
},
{
title: 'Custom link',
title: 'External link',
id: 'item-4',
linkTag: 'a',
href: 'https://wordpress.com',
linkTagProps: {
linkProps: {
target: '_blank',
},
},
{
title: 'Internal link',
id: 'item-5',
LinkComponent: CustomRouterLink,
},
];

function Example() {
Expand Down

0 comments on commit 8864e06

Please sign in to comment.