Skip to content

Tabs ​

Storybook

Browse the props, controls and all the stories of Tabs in Storybook.

The Tabs components organize content in panels displayed one at a time. They are composable: Tabs holds the selected tab, TabList renders the tab buttons, Tab is a single button, TabsBody wraps the panels and TabPanel is displayed when its value matches the selected tab. Tabs support keyboard navigation (arrow keys, Home / End, with RTL support) and the tab / tabpanel ARIA roles.

Usage ​

tsx
import { Tab, TabList, TabPanel, Tabs, TabsBody } from "@tsed/react-formio/molecules/tabs/all";

<Tabs selected={0}>
  <TabList>
    <Tab value={0} icon="edit">Edit</Tab>
    <Tab value={1} icon="data">Data</Tab>
    <Tab value={2} icon="test-tube" onClick={(value) => console.log(value)}>
      Preview
    </Tab>
  </TabList>
  <TabsBody>
    <TabPanel value={0}>Edit content</TabPanel>
    <TabPanel value={1}>Data content</TabPanel>
    <TabPanel value={2}>Preview content</TabPanel>
  </TabsBody>
</Tabs>;

Each component can also be imported from its own module, for example @tsed/react-formio/molecules/tabs/Tab.

Live exampleOpen in Storybook ↗

Props ​

Tabs ​

NameTypeDefaultDescription
selectednumber0Value of the tab selected on mount.
classNamestringAdditional CSS classes.
styleCSSPropertiesInline style of the tabs root.

selected is only used as the initial value: the selected tab is then managed by the component.

Tab ​

NameTypeDefaultDescription
valuenumberValue of the tab, matched against the value of a TabPanel.
iconstringIcon displayed before the label.
onClick(value: number) => voidCalled when the tab becomes active (by click or keyboard).
afterReactNodeContent rendered inside the button, after the label.
classNamestringAdditional CSS classes on the tab wrapper (for example -reverse to put the icon after the label).
childrenReactNodeLabel of the tab.

TabList ​

TabList renders the tab buttons in a tablist. Its Tab children are grouped together, the other children are rendered before or after them depending on their position, which lets you add extra controls (see below).

TabPanel ​

NameTypeDefaultDescription
valuenumberValue of the panel, displayed when the matching tab is selected.
classNamestringAdditional CSS classes.
childrenReactNodeContent of the panel.

TabsBody ​

TabsBody wraps the TabPanel components. It has no props besides children.

Examples ​

With selected tab ​

Open the tabs on a given tab with selected:

Live exampleOpen in Storybook ↗

With extra controls ​

Non-Tab children of TabList are rendered before or after the tab buttons:

tsx
import { Icon } from "@tsed/react-formio/atoms/icon/Icon";
import { Button } from "@tsed/react-formio/molecules/button/Button";
import { Tab, TabList, TabPanel, Tabs, TabsBody } from "@tsed/react-formio/molecules/tabs/all";

<Tabs>
  <TabList>
    <Button variant="link" aria-label="Previous tab">
      <Icon name="chevron-left" />
    </Button>
    <Tab value={0}>Edit</Tab>
    <Tab value={1}>Data</Tab>
    <Button variant="link" aria-label="Add tab">
      <Icon name="plus" />
    </Button>
  </TabList>
  <TabsBody>
    <TabPanel value={0}>Edit content</TabPanel>
    <TabPanel value={1}>Data content</TabPanel>
  </TabsBody>
</Tabs>;
Live exampleOpen in Storybook ↗

Legacy tabs ​

Storybook

See TabsLegacy in Storybook.

TabsLegacy builds the tabs from a list of items. It is kept for the components using the previous tabs API.

tsx
import { TabsLegacy } from "@tsed/react-formio/molecules/tabs/TabsLegacy";

<TabsLegacy
  items={[
    { label: "Edit", icon: "edit", children: <div>Edit</div> },
    { label: "Data", icon: "data", children: <div>Data</div> }
  ]}
  onClick={(item) => console.log(item.label)}
/>;
NameTypeDefaultDescription
itemsTabsItemProps[][]Tabs to display: label, icon and children (or content). Items without label and icon are ignored.
onClick(item: TabsItemProps) => voidCalled with the item when a tab becomes active.
currentTabsItemPropsCurrent item, passed to AddButton and HeaderChildren.
AddButtonComponentTypeComponent rendered after the tab buttons.
HeaderChildrenComponentTypeComponent rendered below the tab list.
afterReactNodeContent rendered in each tab button, after the label.
reversebooleanAdds the -reverse class to each tab.
i18nFormOptions["i18n"]Translations of the tab labels.
classNamestringAdditional CSS classes.
styleCSSPropertiesInline style of the tabs root.

Extra props are passed to AddButton and HeaderChildren.

Live exampleOpen in Storybook ↗

Override ​

Tabs, TabList, Tab, TabsBody and TabPanel are registered in the components registry with their own name. TabsLegacy resolves them from the registry. See Registering react components.

tsx
import { registerComponent } from "@tsed/react-formio/registries/components";

registerComponent("Tab", MyCustomTab);

Released under the MIT License.