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: restore design system Tabs api #4949

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: typescript
jmfrancois committed Oct 23, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 7943e0fa50c4fcaf51a6956cdca9229afe63577a
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from 'react';
import { TabsInternalContext } from './TabsProvider';

type TabPanelPropTypes = {
export type TabPanelPropTypes = {
id: string;
children: React.ReactNode | React.ReactNode[];
renderIf?: boolean;
82 changes: 48 additions & 34 deletions packages/design-system/src/components/Tabs/variants/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,67 @@
import { TabsProvider } from './Primitive/TabsProvider';
import { Tabs as TabList, Tab } from './Primitive/Tabs';
import { TabPanel } from './Primitive/TabPanel';
import { TabsProvider, TabsProviderPropTypes } from '../Primitive/TabsProvider';
import { Tabs as TabList, Tab, TabPropTypes } from '../Primitive/Tabs';
import { TabPanel, TabPanelPropTypes } from '../Primitive/TabPanel';

type TabTitlePropTypes = {
id?: string;
title: string;
icon?: string;
tag?: string | number;
tooltip?: string;
disabled?: boolean;
type TabTitlePropTypes = TabPropTypes & {
id: string;
};

type TabItemPropTypes = {
tabTitle: string | TabTitlePropTypes;
tabTitle?: TabTitlePropTypes | string;
tabContent: React.ReactNode;
};

export type TabsProps = {
tabs: TabItemPropTypes[];
defaultActiveKey?: string;
selectedId?: string;
size?: 'S' | 'M' | 'L';
};

export function Tabs(props: TabsProps) {
if (props.tabs) {
const tabProviderProps: Partial<TabsProviderPropTypes> = {
size: props.size,
defaultActiveKey: props.selectedId,
};
if (props.tabs.length > 0 && !props.selectedId) {
if (typeof props.tabs[0].tabTitle === 'string') {
tabProviderProps.defaultActiveKey = props.tabs[0].tabTitle;
} else if (typeof props.tabs[0].tabTitle === 'object') {
tabProviderProps.defaultActiveKey = props.tabs[0].tabTitle.id;
}
}
return (
<TabsProvider
size={props.size}
defaultActiveKey={
props.defaultActiveKey || props.tabs[0].tabTitle?.id || props.tabs[0].tabTitle
}
>
<TabsProvider {...tabProviderProps}>
<TabList>
{props.tabs.map((tab: TabItemPropTypes, index: number) => (
<Tab
key={index}
aria-controls={tab.tabTitle?.id || tab.tabTitle}
title={tab.tabTitle?.title || tab.tabTitle}
icon={tab.tabTitle?.icon}
tag={tab.tabTitle?.tag}
tooltip={tab.tabTitle?.tooltip}
disabled={tab.tabTitle?.disabled}
/>
))}
{props.tabs.map((tab: TabItemPropTypes, index: number) => {
const tabProps: Partial<TabPropTypes> = {};
if (typeof tab.tabTitle === 'string') {
tabProps['aria-controls'] = tab.tabTitle;
tabProps.title = tab.tabTitle;
} else if (typeof tab.tabTitle === 'object') {
tabProps['aria-controls'] = tab.tabTitle.id;
tabProps.title = tab.tabTitle.title;
tabProps.icon = tab.tabTitle.icon;
tabProps.tag = tab.tabTitle.tag;
tabProps.tooltip = tab.tabTitle.tooltip;
tabProps.disabled = tab.tabTitle.disabled;
}
return <Tab key={index} {...(tabProps as TabPropTypes)} />;
})}
</TabList>
{props.tabs.map((tab: TabItemPropTypes, index: number) => (
<TabPanel key={index} id={tab.tabTitle?.id || tab.tabTitle}>
{tab.tabContent}
</TabPanel>
))}
{props.tabs.map((tab: TabItemPropTypes, index: number) => {
const tabPanelProps: Partial<TabPanelPropTypes> = {};
if (typeof tab.tabTitle === 'string') {
tabPanelProps.id = tab.tabTitle;
} else if (typeof tab.tabTitle === 'object') {
tabPanelProps.id = tab.tabTitle.id;
}
return (
<TabPanel key={index} {...(tabPanelProps as TabPanelPropTypes)}>
{tab.tabContent}
</TabPanel>
);
})}
</TabsProvider>
);
}
Original file line number Diff line number Diff line change
@@ -116,6 +116,8 @@ export const TabAPI = () => (
tabTitle: {
title: 'Tabs 3',
icon: 'user',
tag: '999+',
tooltip: '1534 Favorite',
},
tabContent: <>Tab 3</>,
},