# 动态的页签
- order: 4
演示添加删除和附加操作。
---
````jsx
import { Tabs, Button, Icon, message } from 'antd';
const TabPane = Tabs.TabPane;
let index = 0;
const closeStyle = {
position: 'absolute',
top: 8,
right: -9,
};
const addStyle = {
pointerEvents: 'auto',
color: '#2db7f5',
position: 'absolute',
top: 8,
left: 0,
marginLeft: -8,
};
const Test = React.createClass({
getInitialState() {
return {
tabs: [{
title: 'title ' + index,
content: 'content ' + index,
index: index
}],
activeKey: index.toString()
};
},
remove(index, e) {
e.stopPropagation();
let tabs = this.state.tabs;
let activeKey = this.state.activeKey;
let foundIndex = 0;
if(tabs.length === 1) {
message.error('仅剩一个,不能删除');
return;
}
const newTabs = tabs.filter(tab => {
if (tab.index !== index) {
return true;
} else {
foundIndex = index;
return false;
}
});
if (activeKey === index) {
activeKey = tabs[foundIndex - 1].index;
}
this.setState({
tabs: newTabs, activeKey
});
},
add() {
index += 1;
this.setState({
tabs: this.state.tabs.concat({
title: 'title ' + index,
content: 'content ' + index,
index: index,
}),
activeKey: index.toString(),
});
},
onChange(activeKey) {
console.log(activeKey);
this.setState({ activeKey });
},
render() {
const addBtn = ;
const operations = ;
return (
{
this.state.tabs.map(tab => (
{tab.title}
}>{tab.content}
))
}
);
}
})
ReactDOM.render(, document.getElementById('components-tabs-demo-add'));
````