v3/packages/design-system/atoms/tab-view/TabView.tsx
Katalina 7d681d69d6
Feat/editor category pass2 (#290)
* feat(design-system): add editor skeletons

* use css media queries rather than JS media queries

* init remake

* feat: add basis of toggle atom

* finish toggle

* use pointer cursor with toggle

* sync

* feat: add server message in editor

* cleanup storybook

* add short editor item and data model for categories

* chore: fix build by moving jest version downward

* chore: remove old category editor

* chore: fix EditorCategoryShort index

* add editor wiring and styling updates

* fix linting issues
2021-07-05 12:18:40 -05:00

75 lines
1.9 KiB
TypeScript

import * as React from 'react';
import { QuickNav } from '../quick-nav';
import {
TabContent,
TabContentTitle,
TabTitleRow,
TabViewStyled,
} from './TabView.styled';
export type TabViewProps = {
children: React.ReactNode[];
initialTab?: number;
masthead?: React.ReactNode;
};
type TabProps = {
title: string;
children: () => React.ReactNode;
};
export const TabView = (props: TabViewProps) => {
const tabNames = React.Children.map(props.children, (child) => {
if (!React.isValidElement(child)) {
return '(Oops)';
}
return child.props.title;
}) as string[];
if (tabNames.length === 0) {
return null;
}
const [currentTab, setCurrentTab] = React.useState<number>(props.initialTab ?? 0);
const tabRefs = tabNames.reduce<{ [name: string]: React.RefObject<HTMLDivElement> }>(
(acc, name) => ({ ...acc, [name]: React.createRef() }),
{}
);
return (
<TabViewStyled>
<TabTitleRow>
{props.masthead && props.masthead}
<QuickNav
currentNavItem={tabNames[currentTab]}
navItems={tabNames}
onNavChange={(newTabName) => {
setCurrentTab(tabNames.findIndex((name) => newTabName === name));
tabRefs[newTabName].current?.scrollIntoView({
behavior: 'smooth',
});
}}
/>
</TabTitleRow>
<TabContent>
{React.Children.map(props.children, (child) => {
if (!React.isValidElement(child)) {
return null;
}
return (
<div key={child.props.title}>
<TabContentTitle ref={tabRefs[child.props.title]}>
{child.props.title}
</TabContentTitle>
{child}
</div>
);
})}
</TabContent>
</TabViewStyled>
);
};
export const Tab = (props: TabProps) => <div>{props.children()}</div>;