Skip to content

Commit

Permalink
TabPanel: Convert to TypeScript (#43536)
Browse files Browse the repository at this point in the history
* TabPanel: Convert to TypeScript

* Convert tests

* Convert tests to TS

* Simplify

* Replace noop with conditional chaining

* Export without contextConnect

* Add description to `className`
  • Loading branch information
mirka committed Sep 1, 2022
1 parent 260de49 commit 01f3711
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 232 deletions.
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.

- 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 },
},
};
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.

0 comments on commit 01f3711

Please sign in to comment.