import React from 'react';
import { Tabs, Button } from 'antd';
import 'antd/dist/antd.css';
const { TabPane } = Tabs;
class App extends React.Component {
constructor(props) {
super(props);
this.newTabIndex = 0;
const panes = [
{ title: 'Tab 1', content: 'Content of Tab Pane 1', key: '1' },
{ title: 'Tab 2', content: 'Content of Tab Pane 2', key: '2' },
{ title: 'Tab 3', content: 'Content of Tab Pane 3', key: '3' },
];
this.state = {
activeKey: panes[0].key,
panes,
};
}
onChange = activeKey => {
this.setState({ activeKey });
console.log(activeKey);
};
onEdit = (targetKey, action) => {
this[action](targetKey);
};
add = targetKey => {
const {panes} = this.state;
let { activeKey } = this.state;
if (targetKey !== activeKey){
panes.push({title:'Tab 3', content:'Content of tab pane 3',key: activeKey+1})
this.setState({ panes, activeKey });
console.log(activeKey);
}
};
remove = targetKey => {
let { activeKey } = this.state;
let lastIndex;
this.state.panes.forEach((pane, i) => {
if (pane.key === targetKey) {
lastIndex = i - 1;
}
});
const panes = this.state.panes.filter(pane => pane.key !== targetKey);
if (panes.length && activeKey === targetKey) {
if (lastIndex >= 0) {
activeKey = panes[lastIndex].key;
} else {
activeKey = panes[0].key;
}
}
this.setState({ panes, activeKey });
};
render() {
return (
<div className="tab-section">
<div style={{ marginBottom: 16 }}>
<Button onClick={this.add}>ADD Tab-1</Button>
<Button onClick={this.add}>ADD Tab-2</Button>
<Button onClick={this.add}>ADD Tab-3</Button>
</div>
<Tabs
hideAdd
onChange={this.onChange}
activeKey={this.state.activeKey}
type="editable-card"
onEdit={this.onEdit}
>
{this.state.panes.map(pane => (
<TabPane tab={pane.title} key={pane.key}>
{pane.content}
</TabPane>
))}
</Tabs>
</div>
);
}
}
export default App;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
import React from 'react';
import { Tabs, Button } from 'antd';
import 'antd/dist/antd.css';
const { TabPane } = Tabs;
class App extends React.Component {
constructor(props) {
super(props);
this.newTabIndex = 0;
const panes = [
{ title: 'Tab 1', content: 'Content of Tab Pane 1', key: '1' },
{ title: 'Tab 2', content: 'Content of Tab Pane 2', key: '2' },
{ title: 'Tab 3', content: 'Content of Tab Pane 3', key: '3' },
];
this.state = {
activeKey: panes[0].key,
panes,
};
}
onChange = activeKey => {
this.setState({ activeKey });
console.log(activeKey);
};
onEdit = (targetKey, action) => {
this[action](targetKey);
};
add = targetKey => {
const {panes} = this.state;
let { activeKey } = this.state;
if (targetKey !== activeKey){
panes.push({title:'Tab 3', content:'Content of tab pane 3',key: activeKey+1})
this.setState({ panes, activeKey });
console.log(activeKey);
}
};
remove = targetKey => {
let { activeKey } = this.state;
let lastIndex;
this.state.panes.forEach((pane, i) => {
if (pane.key === targetKey) {
lastIndex = i - 1;
}
});
const panes = this.state.panes.filter(pane => pane.key !== targetKey);
if (panes.length && activeKey === targetKey) {
if (lastIndex >= 0) {
activeKey = panes[lastIndex].key;
} else {
activeKey = panes[0].key;
}
}
this.setState({ panes, activeKey });
};
render() {
return (
<div className="tab-section">
<div style={{ marginBottom: 16 }}>
<Button onClick={this.add}>ADD Tab-1</Button>
<Button onClick={this.add}>ADD Tab-2</Button>
<Button onClick={this.add}>ADD Tab-3</Button>
</div>
<Tabs
hideAdd
onChange={this.onChange}
activeKey={this.state.activeKey}
type="editable-card"
onEdit={this.onEdit}
>
{this.state.panes.map(pane => (
<TabPane tab={pane.title} key={pane.key}>
{pane.content}
</TabPane>
))}
</Tabs>
</div>
);
}
}
export default App;
Here is the code. Initially I should’nt see the tab. Onclick of the button, that particular tab should pop. I could just add a similar Tab everytime. If the tab is already present then it shouldn’t pop again.
Note- “antd”: “^4.16.13”
“react”: “^17.0.2”,
“react-dom”: “^17.0.2”,
“react-scripts”: “4.0.3”, are the packages used.
Here is the output that I get, everytime on Click of Add button, it pops the same tab-
https://i.stack.imgur.com/KhHMz.png
Please help me out in adding each tab on add button click.