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

feat(shared): Tabs 공통 컴포넌트 추가 #171

Merged
merged 2 commits into from
Nov 24, 2023
Merged
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
67 changes: 67 additions & 0 deletions packages/shared/components/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
Tab,
TabList,
TabPanel,
TabPanels,
Tabs as ChakraTabs,
} from '@chakra-ui/react';
import { type ReactNode, useState } from 'react';
import { useSearchParams } from 'react-router-dom';

type TabName = string;

type TabNode = ReactNode;

type TabsProps = {
tabs: [TabName, TabNode][];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tab 을 클릭했을때 url 이동하는 함수를 실행하는 함수가 있었으면 좋겠습니다~!

};

export default function Tabs({ tabs }: TabsProps) {
const [searchParams, setSearchParams] = useSearchParams();
const tabId = Number(searchParams.get('tab'));
const [tabIndex, setTabIndex] = useState(tabId);

const handleTabsChange = (index: number) => setTabIndex(index);

const handleSetTabParam = () => setSearchParams({ tab: `${tabIndex}` });

return (
<ChakraTabs
pt={3}
isFitted
variant="unstyled"
onChange={handleTabsChange}
index={tabIndex}
>
<TabList>
{tabs.map((tab, index) => (
<Tab
color="gray.400"
borderTop="2px solid"
borderTopColor="white"
borderBottom="1px solid"
borderBottomColor="gray.200"
_selected={{
color: 'black',
borderX: '1px solid',
borderColor: 'gray.200',
borderTopColor: 'orange.400',
borderBottomColor: 'white',
borderLeft: index === 0 ? 'none' : '1px solid',
borderRight: index === tabs.length - 1 ? 'none' : '1px solid',
}}
key={index}
onClick={handleSetTabParam}
>
{tab[0]}
</Tab>
))}
</TabList>
<TabPanels>
{tabs.map((tab, index) => (
<TabPanel key={index}>{tab[1]}</TabPanel>
))}
</TabPanels>
</ChakraTabs>
);
}