Skip to content

Commit

Permalink
Add LinsTabs
Browse files Browse the repository at this point in the history
  • Loading branch information
lialila committed Apr 27, 2024
1 parent aafa1d4 commit 151280a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/components/LinkTabs/LinkTabs.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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' },
{ key: 'tab-2', title: 'Tab 2 Title' },
{ key: 'tab-1', title: 'Tab 1 Title', link: 'example.com' },
{ key: 'tab-2', title: 'Tab 2 Title', link: 'example.com' },
],
},
} as Meta<typeof LinkTabs>;
Expand Down
25 changes: 14 additions & 11 deletions src/components/LinkTabs/LinkTabs.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import { Box, Button, Flex } from '@chakra-ui/react';
import React, { FC, useMemo, useState } from '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) => {
const onClick = (newTabKey: string, link: string) => {
setActiveTabKey(newTabKey);
onChange && onChange(newTabKey);
window.history.pushState(null, '', link);
};

const tab = useMemo(() => tabs.find((tab) => tab.key === activeTabKey), [tabs, activeTabKey]);
return (
<Box>
<Flex width="full" gap="8" overflowX="auto">
{tabs.map((tab) => (
<Box key={tab.key}>
<Button variant="link" onClick={() => onClick(tab.link)}>
<Button
variant="link"
color="primary.800"
fontWeight="bold"
pb="2"
onClick={() => onClick(tab.key, tab.link)}
cursor="pointer"
>
{tab.title}
</Button>
{activeTabKey === tab.key && (
<Box
data-testid="tabBottonIndicator"
backgroundColor="primary.700"
borderTopRadius="full"
height="3px"
/>
{tab.key === activeTabKey && (
<Box backgroundColor="primary.700" borderTopRadius="full" height="3px" />
)}
</Box>
))}
Expand Down

0 comments on commit 151280a

Please sign in to comment.