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(Tabs)!: add default selected tab option #885

Merged
merged 5 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 13 additions & 5 deletions packages/react/src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const AddIcon = styled(Icon)`
`;

export interface Tab {
id?: string;
id: string;
title: string;
leftIcon?: IconName;
rightIcon?: IconName;
Expand All @@ -118,12 +118,19 @@ interface Props {
forceRenderTabPanels?: boolean;
global?: boolean;
tabs: Tab[];
defaultSelectedId?: string;
onAddTab?(): void;
onRemove?(tabId: string): void;
}

export const Tabs: VoidFunctionComponent<Props> = ({
className, global, forceRenderTabPanels, tabs, onAddTab, onRemove,
className,
global,
forceRenderTabPanels,
tabs,
defaultSelectedId,
onAddTab,
onRemove,
}) => {
const { t } = useTranslation('tabs');
const tabsListRef = createRef<HTMLDivElement>();
Expand All @@ -140,15 +147,16 @@ export const Tabs: VoidFunctionComponent<Props> = ({
});

const tabItems: TabItem[] = useMemo((): TabItem[] => tabs.map(
(tab, i) => ({
(tab) => ({
...tab,
id: tab.id ?? `${i}`,
id: tab.id,
panelId: uuid(),
buttonRef: createRef<HTMLButtonElement>(),
}),
), [tabs]);

const [selectedTab, setSelectedTab] = useState(tabItems[0]);
const defaultSelectedTab = tabItems.find((tab) => tab.id === defaultSelectedId);
const [selectedTab, setSelectedTab] = useState(defaultSelectedTab ?? tabItems[0]);

function isTabSelected(tabId: string): boolean {
return selectedTab.id === tabId;
Expand Down
31 changes: 31 additions & 0 deletions packages/storybook/stories/tabs.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export const Scrollable: Story = {
...TabsMeta,
render: () => {
const customTabs: Tab[] = [...Array(15).keys()].map((i) => ({
id: `tab${i + 1}`,
title: `Tab ${i + 1}`,
panelContent: (
<StyledDiv>
Expand Down Expand Up @@ -159,6 +160,7 @@ export const UnloadTabCallback: Story = {
render: () => {
const customTabs: Tab[] = [
{
id: 'tab1',
title: 'Tab that cannot change because onBeforeUnload resolves to false',
panelContent: <StyledDiv>First tab content</StyledDiv>,
onBeforeUnload: () => {
Expand All @@ -167,11 +169,13 @@ export const UnloadTabCallback: Story = {
},
},
{
id: 'tab2',
title: 'Second Button',
panelContent: <StyledDiv>Second tab content</StyledDiv>,
onBeforeUnload: () => Promise.resolve(true),
},
{
id: 'tab2',
title: 'Third Button',
panelContent: <StyledDiv>Third tab content</StyledDiv>,
onBeforeUnload: () => Promise.resolve(true),
Expand All @@ -183,3 +187,30 @@ export const UnloadTabCallback: Story = {
);
},
};

export const DefaultSelectedTab: Story = {
...TabsMeta,
render: () => {
const customTabs: Tab[] = [
{
id: 'tab1',
title: 'First Button',
panelContent: <StyledDiv>First tab content</StyledDiv>,
},
{
id: 'tab2',
title: 'Second Button',
panelContent: <StyledDiv>Second tab content</StyledDiv>,
},
{
id: 'tab3',
title: 'Third Button',
panelContent: <StyledDiv>Third tab content</StyledDiv>,
},
];

return (
<Tabs tabs={customTabs} defaultSelectedId='tab2' />
);
},
};
Loading