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(react): add TabPanel component #62

Merged
merged 4 commits into from
Mar 10, 2023
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
4 changes: 4 additions & 0 deletions packages/react/.storybook/story-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export type Stories =
| 'SignIn'
| 'Stepper'
| 'Tab'
| 'TabPanel'
| 'Tabs'
| 'TextField'
| 'Toolbar'
Expand Down Expand Up @@ -253,6 +254,9 @@ const StoryConfig: StorybookConfig = {
Tab: {
hierarchy: `${StorybookCategories.Navigation}/Tab`,
},
TabPanel: {
hierarchy: `${StorybookCategories.Navigation}/Tab Panel`,
},
Tabs: {
hierarchy: `${StorybookCategories.Navigation}/Tabs`,
},
Expand Down
52 changes: 52 additions & 0 deletions packages/react/src/components/TabPanel/TabPanel.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {ArgsTable, Source, Story, Canvas, Meta} from '@storybook/addon-docs';
import dedent from 'ts-dedent';
import StoryConfig from '../../../.storybook/story-config.ts';
import TabPanel from './TabPanel.tsx';

export const meta = {
component: TabPanel,
title: StoryConfig.TabPanel.hierarchy,
};

<Meta title={meta.title} component={meta.component} />

export const Template = args => <TabPanel {...args} />;

# TabPanel

- [Overview](#overview)
- [Props](#props)
- [Usage](#usage)

## Overview

TabPanel component can be used with Tabs component to implement the content of the tab views.

<Canvas>
<Story name="Overview" args={{children: 'Sample Tab Panel'}}>
{Template.bind({})}
</Story>
</Canvas>

## Props

<ArgsTable story="Overview" />

## Usage

Import and use the `TabPanel` component in your components as follows.

<Source
language="jsx"
dark
format
code={dedent`
import TabPanel from '@oxygen-ui/react/TabPanel';\n
function Demo() {
return (
<TabPanel>
Sample Tab Panel
</TabPanel>
);
}`}
/>
57 changes: 57 additions & 0 deletions packages/react/src/components/TabPanel/TabPanel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import {BoxProps as MuiBoxProps} from '@mui/material';
import clsx from 'clsx';
import {forwardRef, ForwardRefExoticComponent, MutableRefObject, ReactElement} from 'react';
import {WithWrapperProps} from '../../models';
import {composeComponentDisplayName} from '../../utils';
import Box from '../Box';
import './tab-panel.scss';

export interface TabPanelProps extends MuiBoxProps {
/*
* The index of the corresponding `TabPanel`.
*/
index: number;
/*
* The value of the selected `TabPanel`.
*/
value: number;
}

const COMPONENT_NAME: string = 'TabPanel';

const TabPanel: ForwardRefExoticComponent<TabPanelProps> & WithWrapperProps = forwardRef(
(props: TabPanelProps, ref: MutableRefObject<HTMLDivElement>): ReactElement => {
const {className, children, value, index, ...rest} = props;

const classes: string = clsx('oxygen-tab-panel', className);

return (
<Box className={classes} ref={ref} role="tabpanel" hidden={value !== index} {...rest}>
{value === index && <Box>{children}</Box>}
JayaShakthi97 marked this conversation as resolved.
Show resolved Hide resolved
</Box>
);
},
) as ForwardRefExoticComponent<TabPanelProps> & WithWrapperProps;

TabPanel.displayName = composeComponentDisplayName(COMPONENT_NAME);
TabPanel.muiName = COMPONENT_NAME;

export default TabPanel;
32 changes: 32 additions & 0 deletions packages/react/src/components/TabPanel/__tests__/TabPanel.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import {render} from '@unit-testing';
import TabPanel from '../TabPanel';

describe('TabPanel', () => {
it('should render successfully', () => {
const {baseElement} = render(<TabPanel index={0} value={0} />);
expect(baseElement).toBeTruthy();
});

it('should match the snapshot', () => {
const {baseElement} = render(<TabPanel index={0} value={0} />);
expect(baseElement).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`TabPanel should match the snapshot 1`] = `
<body>
<div>
<div
class="oxygen-box oxygen-tab-panel MuiBox-root css-0"
role="tabpanel"
>
<div
class="oxygen-box MuiBox-root css-0"
/>
</div>
</div>
</body>
`;
20 changes: 20 additions & 0 deletions packages/react/src/components/TabPanel/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export {default} from './TabPanel';
export type {TabPanelProps} from './TabPanel';
21 changes: 21 additions & 0 deletions packages/react/src/components/TabPanel/tab-panel.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

.oxygen-tab-panel {
padding: 1rem;
}