fix(design-system): redo tab view to be based on index instead of title

This commit is contained in:
41666 2020-10-10 05:01:12 -04:00
parent 878f94ea93
commit cdafaa90db
2 changed files with 22 additions and 15 deletions

View file

@ -12,13 +12,16 @@ export default {
}; };
export const ManyTabs = ({ tabCount }) => { export const ManyTabs = ({ tabCount }) => {
const tabs = [...'0'.repeat(tabCount)].reduce( const tabs = [...'0'.repeat(tabCount)].map((_, i) => (
(acc, _, idx) => ({ <Tab title={`tab ${i}`}>
...acc, {() => (
[`Tab ${idx + 1}`]: <Tab>{() => <div>tab {idx + 1}</div>}</Tab>, <>
}), <h1>tab {i}</h1>
{} <p>hello!!!!!</p>
); </>
)}
</Tab>
));
return <TabView>{tabs}</TabView>; return <TabView>{tabs}</TabView>;
}; };

View file

@ -2,8 +2,8 @@ import * as React from 'react';
import { TabTitleRow, TabContent, TabViewStyled, TabTitle } from './TabView.styled'; import { TabTitleRow, TabContent, TabViewStyled, TabTitle } from './TabView.styled';
export type TabViewProps = { export type TabViewProps = {
children: { [title: string]: React.ReactNode }; children: React.ReactNode[];
initialTab?: string; initialTab?: number;
}; };
type TabProps = { type TabProps = {
@ -12,15 +12,19 @@ type TabProps = {
}; };
export const TabView = (props: TabViewProps) => { export const TabView = (props: TabViewProps) => {
const tabNames = Object.keys(props.children); const tabNames = React.Children.map(props.children, (child) => {
if (!React.isValidElement(child)) {
return null;
}
return child.props.title;
});
if (tabNames.length === 0) { if (tabNames.length === 0) {
return null; return null;
} }
const [currentTab, setCurrentTab] = React.useState<keyof TabViewProps['children']>( const [currentTab, setCurrentTab] = React.useState<number>(props.initialTab ?? 0);
props.initialTab ?? tabNames[0]
);
return ( return (
<TabViewStyled> <TabViewStyled>
@ -28,7 +32,7 @@ export const TabView = (props: TabViewProps) => {
{tabNames.map((tabName, idx) => ( {tabNames.map((tabName, idx) => (
<TabTitle <TabTitle
selected={currentTab === tabName} selected={currentTab === tabName}
onClick={() => setCurrentTab(tabName)} onClick={() => setCurrentTab(idx)}
key={`tab${tabName}${idx}`} key={`tab${tabName}${idx}`}
> >
{tabName} {tabName}
@ -37,7 +41,7 @@ export const TabView = (props: TabViewProps) => {
</TabTitleRow> </TabTitleRow>
<TabContent> <TabContent>
{props.children[currentTab] || ( {props.children[currentTab] || (
<i onLoad={() => setCurrentTab(tabNames[0])}> <i onLoad={() => setCurrentTab(0)}>
Tabs were misconfigured, resetting to zero. Tabs were misconfigured, resetting to zero.
</i> </i>
)} )}