Skip to content

Commit

Permalink
Add card with vertical tabs. (#156)
Browse files Browse the repository at this point in the history
* Add card with vertical tabs.

* Add border, grid, remove ripple.
  • Loading branch information
godwinpang committed Sep 20, 2020
1 parent 0f32248 commit 04abeeb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/components/cards/CardWithVerticalTabs.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import { Story, Meta } from '@storybook/react';

import {
CardWithVerticalTabs,
CardWithVerticalTabsProps,
} from './CardWithVerticalTabs';

export default {
title: 'Cards/Card with Vertical Tabs',
component: CardWithVerticalTabs,
} as Meta;

const Template: Story<CardWithVerticalTabsProps> = args => {
const { items } = args;
return <CardWithVerticalTabs items={items} />;
};

export const SampleCardWithVerticalTabs = Template.bind({});
SampleCardWithVerticalTabs.args = {
items: [
{ title: 'Title 1', element: <h1>Title 1</h1> },
{ title: 'Title 2', element: <h1>Title 2</h1> },
{ title: 'Title 3', element: <h1>Title 3</h1> },
],
};
49 changes: 49 additions & 0 deletions src/components/cards/CardWithVerticalTabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { useState } from 'react';
import {
Grid,
Tabs as MuiTabs,
Tab as MuiTab,
makeStyles,
Theme,
} from '@material-ui/core';

import { Card } from './Card';

export interface CardWithVerticalTabsProps {
items: { title: string; element: JSX.Element }[];
}

const useStyles = makeStyles((theme: Theme) => ({
tabs: { borderRight: `1px solid ${theme.palette.divider}` },
}));

export function CardWithVerticalTabs({ items }: CardWithVerticalTabsProps) {
const [index, setIndex] = useState(0);
const classes = useStyles();

const handleChange = (_: React.ChangeEvent<unknown>, newValue: number) => {
setIndex(newValue);
};

const tabElements: JSX.Element[] = items.map(item => (
<MuiTab disableRipple label={item.title} key={item.title} />
));

return (
<Card>
<Grid container spacing={2}>
<Grid item>
<MuiTabs
orientation='vertical'
value={index}
onChange={handleChange}
className={classes.tabs}
>
{tabElements}
</MuiTabs>
</Grid>
<Grid item>{items[index].element}</Grid>
</Grid>
</Card>
);
}

0 comments on commit 04abeeb

Please sign in to comment.