To use react-draggable-tab, manage the tabs array and the selectedTab key in your component state. Use the event handlers provided by Tabs to update this state when tabs are selected, closed, moved, or added.
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
tabs: [
<Tab key='tab0' title='Tab 0'><div>Content 0</div></Tab>,
<Tab key='tab1' title='Tab 1'><div>Content 1</div></Tab>
],
selectedTab: 'tab0'
};
}
handleTabSelect = (e, key, currentTabs) => {
this.setState({ selectedTab: key, tabs: currentTabs });
};
handleTabClose = (e, key, currentTabs) => {
this.setState({ tabs: currentTabs });
};
handleTabPositionChange = (e, key, currentTabs) => {
this.setState({ tabs: currentTabs });
};
handleTabAddButtonClick = (e, currentTabs) => {
const key = 'newTab_' + Date.now();
const newTab = <Tab key={key} title='New Tab'><div>New Content</div></Tab>;
this.setState({
tabs: currentTabs.concat([newTab]),
selectedTab: key
});
};
render() {
return (
<Tabs
tabs={this.state.tabs}
selectedTab={this.state.selectedTab}
onTabSelect={this.handleTabSelect}
onTabClose={this.handleTabClose}
onTabPositionChange={this.handleTabPositionChange}
onTabAddButtonClick={this.handleTabAddButtonClick}
/>
);
}
}
class App extends React.Component {
constructor(props) {
super(props);
let icon = (<image src='icon.png' style={{height:'13px'}}/>);
let fonticon = (<icon className='icon-html5'/>);
let badge = (<DynamicTabBadge />);
this.state = {
tabs:[
(<Tab key={'tab0'} title={'unclosable tab'} unclosable={true} >
<div>
<h1>This tab cannot close</h1>
</div>
</Tab>),
(<Tab key={'tab1'} title={'1stTab'} beforeTitle={icon} >
<div>
<h1>This is tab1</h1>
</div>
</Tab>),
(<Tab key={'tab2'} title={'2ndTab Too long Toooooooooooooooooo long'} beforeTitle={fonticon} >
<div>
<pre>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
</pre>
</div>
</Tab>),
(<Tab key={'tab3'} title={'Dynamic tab'} afterTitle={badge}>
<DynamicTabContent/>
</Tab>)
],
badgeCount: 0
};
}
handleTabSelect(e, key, currentTabs) {
console.log('handleTabSelect key:', key);
this.setState({selectedTab: key, tabs: currentTabs});
}
handleTabClose(e, key, currentTabs) {
console.log('tabClosed key:', key);
this.setState({tabs: currentTabs});
}
handleTabPositionChange(e, key, currentTabs) {
console.log('tabPositionChanged key:', key);
this.setState({tabs: currentTabs});
}
handleTabAddButtonClick(e, currentTabs) {
// key must be unique
const key = 'newTab_' + Date.now();
let newTab = (<Tab key={key} title='untitled'>
<div>
<h1>New Empty Tab</h1>
</div
</Tab>);
let newTabs = currentTabs.concat([newTab]);
this.setState({
tabs: newTabs,
selectedTab: key
});
}
render() {
return (
<Tabs
tabsClassNames={tabsClassNames}
tabsStyles={tabsStyles}
selectedTab={this.state.selectedTab ? this.state.selectedTab : "tab2"}
onTabSelect={this.handleTabSelect.bind(this)}
onTabClose={this.handleTabClose.bind(this)}
onTabAddButtonClick={this.handleTabAddButtonClick.bind(this)}
onTabPositionChange={this.handleTabPositionChange.bind(this)}
tabs={this.state.tabs}
shortCutKeys={
{
'close': ['alt+command+w', 'alt+ctrl+w'],
'create': ['alt+command+t', 'alt+ctrl+t'],
'moveRight': ['alt+command+tab', 'alt+ctrl+tab'],
'moveLeft': ['shift+alt+command+tab', 'shift+alt+ctrl+tab']
}
}
/>
)
}
};