Skip to content

Commit

Permalink
feat: ViewPagerTabGroup에서 탭 초기값을 설정할 수 있다
Browse files Browse the repository at this point in the history
  • Loading branch information
loki-class101 committed Sep 26, 2024
1 parent 3b7b310 commit 1a53eda
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,29 @@ import { ViewPagerTabGroupItem } from './ViewPagerTabGroupItem';
import type { ViewPagerTabGroupProps } from './ViewPagerTabGroupProps';
import { withViewPagerTabGroupVariation } from './ViewPagerTabGroupProps';

export const ViewPagerTabGroup = withViewPagerTabGroupVariation(({ children, testId, onTabChange, tabSpacing }) => (
<TabView
testId={testId}
renderTobBarContainer={props => (
<HStack px={[20, 20, 0]} spacing={tabSpacing} data-testid="top-bar-container">
{props}
</HStack>
)}
renderTobBarItem={({ isSelected, onClick, title, tabId }) => (
<VStack key={title} spacing={8}>
<Pressable onClick={onClick} data-testid={`top-bar-${tabId}`}>
<Title level={5}>{title}</Title>
</Pressable>
{isSelected && <Paper borderColor="onView1" borderStyle="solid" borderWidth={1} />}
</VStack>
)}
onTabChange={onTabChange}
>
{children}
</TabView>
)) as ComponentWithRef<ViewPagerTabGroupProps> & {
export const ViewPagerTabGroup = withViewPagerTabGroupVariation(({ children, testId, onTabChange, tabSpacing, initialIndex }) => (
<TabView
testId={testId}
renderTobBarContainer={props => (
<HStack px={[20, 20, 0]} spacing={tabSpacing} data-testid="top-bar-container">
{props}
</HStack>
)}
renderTobBarItem={({ isSelected, onClick, title, tabId }) => (
<VStack key={title} spacing={8}>
<Pressable onClick={onClick} data-testid={`top-bar-${tabId}`}>
<Title level={5}>{title}</Title>
</Pressable>
{isSelected && <Paper borderColor="onView1" borderStyle="solid" borderWidth={1} />}
</VStack>
)}
initialIndex={initialIndex}
onTabChange={onTabChange}
>
{children}
</TabView>
)
) as ComponentWithRef<ViewPagerTabGroupProps> & {
Item: ComponentWithRef<ViewPagerTabGroupItemProps>;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type ViewPagerTabGroupProps = {
tabSpacing?: number;
onTabChange?: () => void;
testId?: string;
initialIndex?: number;
};

export const withViewPagerTabGroupVariation = withVariation<ViewPagerTabGroupProps>('ViewPagerTabGroup')();
4 changes: 2 additions & 2 deletions packages/vibrant-core/src/lib/TabView/TabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { TabViewItemProps } from '../TabViewItem';
import { withTabViewVariation } from './TabViewProps';

export const TabView = withTabViewVariation(
({ children, testId = 'tab-view', onTabChange, renderTobBarItem, renderTobBarContainer }) => {
const [currentIndex, setCurrentIndex] = useState(0);
({ children, testId = 'tab-view', onTabChange, renderTobBarItem, renderTobBarContainer, initialIndex = 0 }) => {
const [currentIndex, setCurrentIndex] = useState(initialIndex);
const childrenElement = Children.toArray(children).filter(isValidElement<TabViewItemProps>);

const selectedTab = childrenElement.find((_, index) => index === currentIndex);
Expand Down
1 change: 1 addition & 0 deletions packages/vibrant-core/src/lib/TabView/TabViewProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type TabViewProps = {
renderTobBarContainer: (props: ReactElementChildren) => ReactElement;
onTabChange?: () => void;
testId?: string;
initialIndex?: number;
};

export const withTabViewVariation = withVariation<TabViewProps>('TabView')();

0 comments on commit 1a53eda

Please sign in to comment.