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

TabPanel: Convert to TypeScript #43536

Merged
merged 8 commits into from
Sep 1, 2022
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
3 changes: 1 addition & 2 deletions packages/components/src/tab-panel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ The class to add to the active tab

#### initialTabName

Optionally provide a tab name for a tab to be selected upon mounting of component. If this prop is not set, the first tab will be selected by default.
The name of the tab to be selected upon mounting of component. If this prop is not set, the first tab will be selected by default.

- Type: `String`
- Required: No
Expand All @@ -145,7 +145,6 @@ Optionally provide a tab name for a tab to be selected upon mounting of componen
#### children

A function which renders the tabviews given the selected tab. The function is passed the active tab object as an argument as defined the tabs prop.
The element to which the tooltip should anchor.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like copypasta 🧹


- Type: (`Object`) => `Element`
- Required: Yes
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ import { useInstanceId } from '@wordpress/compose';
*/
import { NavigableMenu } from '../navigable-container';
import Button from '../button';
import type { TabButtonProps, TabPanelProps } from './types';
import type { WordPressComponentProps } from '../ui/context';

const noop = () => {};

const TabButton = ( { tabId, onClick, children, selected, ...rest } ) => (
const TabButton = ( {
tabId,
onClick,
children,
selected,
...rest
}: TabButtonProps ) => (
<Button
role="tab"
tabIndex={ selected ? null : -1 }
Expand All @@ -31,24 +37,60 @@ const TabButton = ( { tabId, onClick, children, selected, ...rest } ) => (
</Button>
);

export default function TabPanel( {
/**
* TabPanel is an ARIA-compliant tabpanel.
*
* TabPanels organize content across different screens, data sets, and interactions.
* It has two sections: a list of tabs, and the view to show when tabs are chosen.
*
* ```jsx
* import { TabPanel } from '@wordpress/components';
*
* const onSelect = ( tabName ) => {
* console.log( 'Selecting tab', tabName );
* };
*
* const MyTabPanel = () => (
* <TabPanel
* className="my-tab-panel"
* activeClass="active-tab"
* onSelect={ onSelect }
* tabs={ [
* {
* name: 'tab1',
* title: 'Tab 1',
* className: 'tab-one',
* },
* {
* name: 'tab2',
* title: 'Tab 2',
* className: 'tab-two',
* },
* ] }
* >
* { ( tab ) => <p>{ tab.title }</p> }
* </TabPanel>
* );
* ```
*/
export function TabPanel( {
className,
children,
tabs,
initialTabName,
orientation = 'horizontal',
activeClass = 'is-active',
onSelect = noop,
} ) {
onSelect,
}: WordPressComponentProps< TabPanelProps, 'div', false > ) {
const instanceId = useInstanceId( TabPanel, 'tab-panel' );
const [ selected, setSelected ] = useState( null );
const [ selected, setSelected ] = useState< string >();

const handleClick = ( tabKey ) => {
const handleClick = ( tabKey: string ) => {
setSelected( tabKey );
onSelect( tabKey );
onSelect?.( tabKey );
};

const onNavigate = ( childIndex, child ) => {
const onNavigate = ( _childIndex: number, child: HTMLButtonElement ) => {
child.click();
};
const selectedTab = find( tabs, { name: selected } );
Expand All @@ -58,7 +100,8 @@ export default function TabPanel( {
const newSelectedTab = find( tabs, { name: selected } );
if ( ! newSelectedTab ) {
setSelected(
initialTabName || ( tabs.length > 0 ? tabs[ 0 ].name : null )
initialTabName ||
( tabs.length > 0 ? tabs[ 0 ].name : undefined )
);
}
}, [ tabs ] );
Expand Down Expand Up @@ -104,3 +147,5 @@ export default function TabPanel( {
</div>
);
}

export default TabPanel;
39 changes: 0 additions & 39 deletions packages/components/src/tab-panel/stories/index.js

This file was deleted.

37 changes: 37 additions & 0 deletions packages/components/src/tab-panel/stories/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';

/**
* Internal dependencies
*/
import TabPanel from '..';

const meta: ComponentMeta< typeof TabPanel > = {
title: 'Components/TabPanel',
component: TabPanel,
parameters: {
controls: { expanded: true },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't expand the code snippet because it isn't very helpful if the children part isn't shown ☹️

},
};
export default meta;

const Template: ComponentStory< typeof TabPanel > = ( props ) => {
return <TabPanel { ...props } />;
};

export const Default = Template.bind( {} );
Default.args = {
children: ( tab ) => <p>Selected tab: { tab.title }</p>,
tabs: [
{
name: 'tab1',
title: 'Tab 1',
},
{
name: 'tab2',
title: 'Tab 2',
},
],
};
179 changes: 0 additions & 179 deletions packages/components/src/tab-panel/test/index.js

This file was deleted.

Loading