diff --git a/src/components/molecules/tab/Tab.stories.tsx b/src/components/molecules/tab/Tab.stories.tsx new file mode 100644 index 0000000..d8e7a97 --- /dev/null +++ b/src/components/molecules/tab/Tab.stories.tsx @@ -0,0 +1,58 @@ +'use client'; + +import type { Meta, StoryObj } from '@storybook/react'; +import { useState } from 'react'; +import { Tab, TabInfo, TabProps } from './Tab'; + +// Tab 타입 정의 +type TabType = 'comment' | 'photo' | 'link'; + +const defaultTabInfo: TabInfo = [ + { tabType: 'comment', label: '댓글' }, + { tabType: 'photo', label: '사진', count: 0 }, + { tabType: 'link', label: '링크', count: 0 }, +]; + +const meta: Meta = { + title: 'components/Tab', + component: Tab, + argTypes: { + selectedTab: { control: 'text' }, + onClick: { action: 'clicked' }, + }, +}; +export default meta; + +type Story = StoryObj>; + +// 기본 Tab UI +export const Default: Story = { + render: (args) => { + const [selectedTab, setSelectedTab] = useState('comment'); + + return ; + }, + args: { + tabInfo: defaultTabInfo, + }, +}; + +// count가 있는 Tab +export const WithCounts: Story = { + render: (args) => { + const [selectedTab, setSelectedTab] = useState('comment'); + + return ( + + ); + }, +}; diff --git a/src/components/molecules/tab/Tab.tsx b/src/components/molecules/tab/Tab.tsx new file mode 100644 index 0000000..d9a3985 --- /dev/null +++ b/src/components/molecules/tab/Tab.tsx @@ -0,0 +1,57 @@ +'use client'; + +import { Dispatch, forwardRef, ReactNode, SetStateAction } from 'react'; + +// Tab 정보를 나타내는 타입 +export type TabInfo = { + tabType: T; // Tab의 타입을 나타내는 제네릭 타입 T + label: string; // Tab의 이름 + count?: number; // 컨텐츠 개수 +}[]; + +// 각 tab에 대응되는 content +export type ContentInfo = Record< + TabType, + { render: () => ReactNode } +>; + +export interface TabProps { + tabInfo: TabInfo; + selectedTab: T; + className?: string; + onClick: Dispatch>; +} + +const TabComponent = ( + { className, tabInfo, selectedTab, onClick, ...props }: TabProps, + ref: React.Ref, +) => { + return ( +
+ {tabInfo.map(({ tabType, label, count }) => ( + + ))} +
+ ); +}; + +export const Tab = forwardRef(TabComponent) as ( + props: TabProps & { ref?: React.Ref }, +) => JSX.Element; diff --git a/src/components/molecules/tab/index.ts b/src/components/molecules/tab/index.ts new file mode 100644 index 0000000..3080831 --- /dev/null +++ b/src/components/molecules/tab/index.ts @@ -0,0 +1 @@ +export { Tab } from './Tab';