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): Allows disabling of padding in the content area #364

Merged
merged 2 commits into from
Sep 22, 2021
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
26 changes: 26 additions & 0 deletions src/components/Tabs/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
******************************************************************************************************************** */
import React from 'react';
import Tabs from '.';
import Box from '../../layouts/Box';
import { action } from '@storybook/addon-actions';

export default {
Expand Down Expand Up @@ -44,3 +45,28 @@ const tabs = [
export const Default = () => <Tabs tabs={tabs} onChange={action('onChange')} />;

export const Container = () => <Tabs tabs={tabs} onChange={action('onChange')} activeId="second" variant="container" />;

export const NoPaddingContainer = () => {
const tabs = [
{
label: 'First tab label',
id: 'first',
content: <Box width="100%" height="300px" bgcolor="primary.main" />,
},
{
label: 'Second tab label',
id: 'second',
content: <Box width="100%" height="300px" bgcolor="secondary.main" />,
},
];

return (
<Tabs
tabs={tabs}
onChange={action('onChange')}
paddingContentArea={false}
activeId="second"
variant="container"
/>
);
};
5 changes: 5 additions & 0 deletions src/components/Tabs/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ describe('Tabs', () => {
expect(getByText(tabs[0].content)).toBeVisible();
});

it('renders with container variant and without padding in the content area', () => {
const { getByText } = render(<Tabs tabs={tabs} variant="container" paddingContentArea={false} />);
expect(getByText(tabs[0].content)).toBeVisible();
});

it('disables a tab', () => {
const props = {
tabs: [
Expand Down
28 changes: 19 additions & 9 deletions src/components/Tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,44 @@ export interface TabsProps {
* - container: version with borders, designed to be used along with other containers
*/
variant?: 'default' | 'container';
/** Fired whenever the user selects a different tab. The event detail contains the current activeTabId. */
/**
* Whether to render padding within the content area
* */
paddingContentArea?: boolean;
/**
* Fired whenever the user selects a different tab. The event detail contains the current activeTabId. */
onChange?: (activeTabId: string) => void;
}

interface TabPanelProps {
children?: React.ReactNode;
index: number;
value: number;
paddingContentArea: boolean;
}

function TabPanel(props: TabPanelProps) {
const { children, value, index } = props;

function TabPanel({ children, value, index, paddingContentArea }: TabPanelProps) {
return (
<Typography component="div" role="tabpanel" hidden={value !== index} id={`tabpanel-${index}`}>
<Box py={3}>{children}</Box>
<Box py={paddingContentArea ? 3 : undefined}>{children}</Box>
</Typography>
);
}

/**
* Use tabs for organizing discrete blocks of information.
*/
const Tabs = ({ tabs, activeId = '', variant = 'default', onChange }: TabsProps): ReactElement => {
const Tabs = ({
tabs,
activeId = '',
variant = 'default',
paddingContentArea = true,
onChange,
}: TabsProps): ReactElement => {
const classes = useStyles({});
const tabIndex = tabs.findIndex((tab) => tab.id === activeId);
const [value, setValue] = React.useState(tabIndex === -1 ? 0 : tabIndex);
const handleChange = (event: React.ChangeEvent<{}>, index: number) => {
const handleChange = (_event: React.ChangeEvent<{}>, index: number) => {
onChange?.(tabs[index].id);
setValue(index);
};
Expand All @@ -111,13 +121,13 @@ const Tabs = ({ tabs, activeId = '', variant = 'default', onChange }: TabsProps)
);

const tabContent = tabs.map((tab, idx) => (
<TabPanel key={tab.id} value={value} index={idx}>
<TabPanel key={tab.id} value={value} index={idx} paddingContentArea={paddingContentArea}>
{tab.content}
</TabPanel>
));

return variant === 'container' ? (
<Container headerContent={headerContent} headerGutters={false}>
<Container headerContent={headerContent} headerGutters={false} gutters={paddingContentArea}>
{tabContent}
</Container>
) : (
Expand Down