Add quick, dynamic tab functionality to transition through panes of local content.
Allow the component to control its own state.
<Tabs defaultActiveKey={2} id="uncontrolled-tab-example"> <Tab eventKey={1} title="Tab 1"> Tab 1 content </Tab> <Tab eventKey={2} title="Tab 2"> Tab 2 content </Tab> <Tab eventKey={3} title="Tab 3" disabled> Tab 3 content </Tab> </Tabs>;
Pass down the active state on render via props.
class ControlledTabs extends React.Component { constructor(props, context) { super(props, context); this.handleSelect = this.handleSelect.bind(this); this.state = { key: 1 }; } handleSelect(key) { alert(`selected ${key}`); this.setState({ key }); } render() { return ( <Tabs activeKey={this.state.key} onSelect={this.handleSelect} id="controlled-tab-example" > <Tab eventKey={1} title="Tab 1"> Tab 1 content </Tab> <Tab eventKey={2} title="Tab 2"> Tab 2 content </Tab> <Tab eventKey={3} title="Tab 3" disabled> Tab 3 content </Tab> </Tabs> ); } } render(<ControlledTabs />);
Set the animation
prop to false
<Tabs defaultActiveKey={1} animation={false} id="noanim-tab-example"> <Tab eventKey={1} title="Tab 1"> Tab 1 content </Tab> <Tab eventKey={2} title="Tab 2"> Tab 2 content </Tab> <Tab eventKey={3} title="Tab 3" disabled> Tab 3 content </Tab> </Tabs>;
<Tab.Container id="tabs-with-dropdown" defaultActiveKey="first"> <Row className="clearfix"> <Col sm={12}> <Nav bsStyle="tabs"> <NavItem eventKey="first">Tab 1</NavItem> <NavItem eventKey="second">Tab 2</NavItem> <NavDropdown eventKey="3" title="Dropdown" id="nav-dropdown-within-tab"> <MenuItem eventKey="3.1">Action</MenuItem> <MenuItem eventKey="3.2">Another action</MenuItem> <MenuItem eventKey="3.3">Something else here</MenuItem> <MenuItem divider /> <MenuItem eventKey="3.4">Separated link</MenuItem> </NavDropdown> </Nav> </Col> <Col sm={12}> <Tab.Content animation> <Tab.Pane eventKey="first">Tab 1 content</Tab.Pane> <Tab.Pane eventKey="second">Tab 2 content</Tab.Pane> <Tab.Pane eventKey="3.1">Tab 3.1 content</Tab.Pane> <Tab.Pane eventKey="3.2">Tab 3.2 content</Tab.Pane> <Tab.Pane eventKey="3.3">Tab 3.3 content</Tab.Pane> <Tab.Pane eventKey="3.4">Tab 3.4 content</Tab.Pane> </Tab.Content> </Col> </Row> </Tab.Container>;
For more complex layouts the flexible TabContainer
, TabContent
, andTabPane
components along with any style of Nav
allow you to quickly piece together your own Tabs component with additional markup needed.
Just create a set of NavItems each with an eventKey
corresponding to the eventKey of a TabPane
. Wrap the whole thing in a TabContainer
and you have fully functioning custom tabs component. Check out the below example making use of the grid system and pills.
<Tab.Container id="left-tabs-example" defaultActiveKey="first"> <Row className="clearfix"> <Col sm={4}> <Nav bsStyle="pills" stacked> <NavItem eventKey="first">Tab 1</NavItem> <NavItem eventKey="second">Tab 2</NavItem> </Nav> </Col> <Col sm={8}> <Tab.Content animation> <Tab.Pane eventKey="first">Tab 1 content</Tab.Pane> <Tab.Pane eventKey="second">Tab 2 content</Tab.Pane> </Tab.Content> </Col> </Row> </Tab.Container>;
Name | Type | Default | Description |
---|---|---|---|
activeKey | any | controlled by: onSelect , initial prop: defaultActiveKey Mark the Tab with a matching | |
bsStyle | one of: 'tabs' , 'pills' | 'tabs' | Navigation style |
animation | boolean | elementType | true | Sets a default animation strategy. Use |
id | requiredForA11y(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
) | ||
onSelect | function | controls activeKey Callback fired when a Tab is selected.
| |
mountOnEnter | boolean | false | Wait until the first "enter" transition to mount tabs (add them to the DOM) |
unmountOnExit | boolean | false | Unmount tabs (remove it from the DOM) when it is no longer visible |
Name | Type | Default | Description |
---|---|---|---|
disabled | boolean | ||
title | node | ||
tabClassName | string | tabClassName is used as className for the associated NavItem | |
bsClass | string | Base CSS class and prefix for the component. Generally one should only change |
Name | Type | Default | Description |
---|---|---|---|
id | function(props, ...args) {
let error = null;
if (!props.generateChildId) {
error = idPropType(props, ...args);
if (!error && !props.id) {
error = new Error(
'In order to properly initialize Tabs in a way that is accessible ' +
'to assistive technologies (such as screen readers) an `id` or a ' +
'`generateChildId` prop to TabContainer is required'
);
}
}
return error;
| HTML id attribute, required if no | |
generateChildId | function | (eventKey, type) => `${this.props.id}-${type}-${key}` | A function that takes an The |
onSelect | function | controls activeKey A callback fired when a tab is selected. | |
activeKey | any | controlled by: onSelect , initial prop: defaultActiveKey The |
Name | Type | Default | Description |
---|---|---|---|
componentClass | elementType | 'div' | You can use a custom element type for this component. |
animation | boolean | elementType | true | Sets a default animation strategy for all children |
mountOnEnter | boolean | false | Wait until the first "enter" transition to mount tabs (add them to the DOM) |
unmountOnExit | boolean | false | Unmount tabs (remove it from the DOM) when they are no longer visible |
bsClass | string | 'tab' | Base CSS class and prefix for the component. Generally one should only change |
Name | Type | Default | Description |
---|---|---|---|
eventKey | any | Uniquely identify the | |
animation | boolean | elementType | Use animation when showing or hiding | |
bsClass | string | 'tab-pane' | Base CSS class and prefix for the component. Generally one should only change |
onEnter | function | Transition onEnter callback when animation is not | |
onEntering | function | Transition onEntering callback when animation is not | |
onEntered | function | Transition onEntered callback when animation is not | |
onExit | function | Transition onExit callback when animation is not | |
onExiting | function | Transition onExiting callback when animation is not | |
onExited | function | Transition onExited callback when animation is not | |
mountOnEnter | boolean | Wait until the first "enter" transition to mount the tab (add it to the DOM) | |
unmountOnExit | boolean | Unmount the tab (remove it from the DOM) when it is no longer visible |