-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(LinkTabs): Create the component
- Loading branch information
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
import { StoryFn, Meta } from '@storybook/react'; | ||
import { LinkTabs } from './LinkTabs'; | ||
|
||
export default { | ||
title: 'components/LinkTabs', | ||
component: LinkTabs, | ||
args: { | ||
tabs: [ | ||
{ key: 'tab-1', title: 'Tab 1 Title', link: 'example.com' }, | ||
{ key: 'tab-2', title: 'Tab 2 Title', link: 'example.com' }, | ||
], | ||
}, | ||
} as Meta<typeof LinkTabs>; | ||
|
||
const Template: StoryFn<typeof LinkTabs> = (args) => <LinkTabs {...args} />; | ||
|
||
export const Default = Template.bind({}); | ||
Default.args = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Box, Button, Flex } from '@chakra-ui/react'; | ||
import React, { FC, useState } from 'react'; | ||
|
||
export interface LinkTabsProps { | ||
tabs: { key: string; title: string; link: string }[]; | ||
onChange: (activeTabKey: string) => void; | ||
} | ||
|
||
export const LinkTabs: FC<LinkTabsProps> = ({ tabs, onChange }: LinkTabsProps) => { | ||
const [activeTabKey, setActiveTabKey] = useState(tabs[0].key); | ||
|
||
const onClick = (newTabKey: string, link: string) => { | ||
setActiveTabKey(newTabKey); | ||
onChange && onChange(newTabKey); | ||
window.history.pushState(null, '', link); | ||
}; | ||
|
||
return ( | ||
<Box> | ||
<Flex width="full" gap="8" overflowX="auto"> | ||
{tabs.map((tab) => ( | ||
<Box key={tab.key}> | ||
<Button | ||
variant="link" | ||
color="primary.800" | ||
fontWeight="bold" | ||
pb="2" | ||
onClick={() => onClick(tab.key, tab.link)} | ||
cursor="pointer" | ||
> | ||
{tab.title} | ||
</Button> | ||
{tab.key === activeTabKey && ( | ||
<Box backgroundColor="primary.700" borderTopRadius="full" height="3px" /> | ||
)} | ||
</Box> | ||
))} | ||
</Flex> | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { LinkTabs } from './LinkTabs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters