To add the Tabs
component to your app:
1 2import { Tabs } from '@forge/react';
Tabs are used to organize content by grouping similar information on the same page.
Name | Type | Required | Description |
---|---|---|---|
children | TabList | TabPanel | Yes | The children of Tabs . The first child should be a TabList filled with Tab s. Subsequent children should be TabPanel s. There should be a Tab for each TabPanel . If you want to customize Tab or TabPanel , refer to the examples in the documentation. |
id | string | Yes | A unique ID that will be used to generate IDs for tabs and tab panels. This is required for accessibility purposes. |
defaultSelected | number | No | The index of the tab that will be selected by default when the component mounts. If not set, the first tab will be displayed by default. |
onChange | (index) => undefined | No | A callback function which will be fired when a tab is changed. It will be passed to the index of the selected tab and UIAnalyticsEvent . |
selected | number | No | The selected tab's index. If this prop is set, the component behaves as a controlled component. It will be up to you to listen to onChange . |
shouldUnmountTabPanelOnChange | boolean | No | Tabs by default leave tab panels mounted on the page after they have been selected. If you would like to unmount a tab panel when it is not selected, set this prop to be true . |
The default form of tabs.
1 2import {Tabs, TabList, Tab, TabPanel, Box} from "@forge/react"; const TabsDefaultExample = () => { return ( <Tabs id="default"> <TabList> <Tab>Tab 1</Tab> <Tab>Tab 2</Tab> <Tab>Tab 3</Tab> </TabList> <TabPanel> <Box padding="space.300"> This is the content area of the first tab. </Box> </TabPanel> <TabPanel> <Box padding="space.300"> This is the content area of the second tab. </Box> </TabPanel> <TabPanel> <Box padding="space.300"> This is the content area of the third tab. </Box> </TabPanel> </Tabs> ); };
Tabs can be used as a controlled component.
1 2const TabsControlledExample = () => { const [selected, setSelected] = useState(0); const handleUpdate = (index) => setSelected(index); return ( <> <Tabs id="controlled" onChange={handleUpdate} selected={selected}> <TabList> <Tab>Tab 1</Tab> <Tab>Tab 2</Tab> <Tab>Tab 3</Tab> </TabList> <TabPanel> <Box padding="space.300"> This is the content area of the first tab. </Box> </TabPanel> <TabPanel> <Box padding="space.300"> This is the content area of the second tab. </Box> </TabPanel> <TabPanel> <Box padding="space.300"> This is the content area of the third tab. </Box> </TabPanel> </Tabs> <Button onClick={() => handleUpdate(2)}> Select the last tab </Button> </> ); };
You can wrap a tab in other presentational components. In this example we have added a tooltip
to each tab.
1 2const TabWrappingExample = () => { return ( <Tabs id="tabs-wrapping"> <TabList> <Tooltip content="Tooltip for tab 1"> <Tab>Tab 1</Tab> </Tooltip> <Tooltip content="Tooltip for tab 2"> <Tab>Tab 2</Tab> </Tooltip> <Tooltip content="Tooltip for tab 3"> <Tab>Tab 3</Tab> </Tooltip> </TabList> <TabPanel> <Box padding="space.300"> This is the content area of the first tab. </Box> </TabPanel> <TabPanel> <Box padding="space.300"> This is the content area of the second tab. </Box> </TabPanel> <TabPanel> <Box padding="space.300"> This is the content area of the third tab. </Box> </TabPanel> </Tabs> ); };
Rate this page: