2015-10-29 15:23:58 +08:00
|
|
|
# 动态的页签
|
2015-10-22 17:27:53 +08:00
|
|
|
|
2015-10-22 23:54:56 +08:00
|
|
|
- order: 4
|
2015-10-22 17:27:53 +08:00
|
|
|
|
2015-10-29 15:23:58 +08:00
|
|
|
演示添加删除和附加操作。
|
2015-10-22 17:27:53 +08:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
````jsx
|
2015-10-29 15:23:58 +08:00
|
|
|
import { Tabs, Button, Icon, message } from 'antd';
|
2015-10-28 20:55:49 +08:00
|
|
|
const TabPane = Tabs.TabPane;
|
2015-10-22 17:27:53 +08:00
|
|
|
|
2015-10-29 15:23:58 +08:00
|
|
|
let index = 0;
|
2015-10-28 20:55:49 +08:00
|
|
|
const closeStyle = {
|
2015-10-22 17:27:53 +08:00
|
|
|
position: 'absolute',
|
2015-10-29 15:23:58 +08:00
|
|
|
top: 8,
|
|
|
|
right: -9,
|
2015-10-22 17:27:53 +08:00
|
|
|
};
|
2015-10-29 15:23:58 +08:00
|
|
|
|
|
|
|
const addStyle = {
|
2015-10-22 17:27:53 +08:00
|
|
|
pointerEvents: 'auto',
|
2015-10-29 15:23:58 +08:00
|
|
|
color: '#2db7f5',
|
2015-10-29 15:27:43 +08:00
|
|
|
position: 'absolute',
|
|
|
|
top: 8,
|
|
|
|
left: 0,
|
|
|
|
marginLeft: -8,
|
2015-10-22 17:27:53 +08:00
|
|
|
};
|
|
|
|
|
2015-10-29 15:23:58 +08:00
|
|
|
const Test = React.createClass({
|
2015-10-22 17:27:53 +08:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
tabs: [{
|
|
|
|
title: 'title ' + index,
|
|
|
|
content: 'content ' + index,
|
2015-10-29 15:23:58 +08:00
|
|
|
index: index
|
2015-10-22 17:27:53 +08:00
|
|
|
}],
|
2015-10-29 15:23:58 +08:00
|
|
|
activeKey: index.toString()
|
2015-10-22 17:27:53 +08:00
|
|
|
};
|
|
|
|
},
|
2015-10-29 15:23:58 +08:00
|
|
|
remove(index, e) {
|
2015-10-22 17:27:53 +08:00
|
|
|
e.stopPropagation();
|
2015-10-29 15:23:58 +08:00
|
|
|
let tabs = this.state.tabs;
|
|
|
|
let activeKey = this.state.activeKey;
|
|
|
|
let foundIndex = 0;
|
|
|
|
|
|
|
|
if(tabs.length === 1) {
|
|
|
|
message.error('仅剩一个,不能删除');
|
2015-10-22 17:27:53 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-10-29 15:23:58 +08:00
|
|
|
|
|
|
|
const newTabs = tabs.filter(tab => {
|
|
|
|
if (tab.index !== index) {
|
2015-10-22 17:27:53 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
foundIndex = index;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2015-10-29 15:23:58 +08:00
|
|
|
|
|
|
|
if (activeKey === index) {
|
|
|
|
activeKey = tabs[foundIndex - 1].index;
|
2015-10-22 17:27:53 +08:00
|
|
|
}
|
2015-10-29 15:23:58 +08:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
tabs: newTabs, activeKey
|
|
|
|
});
|
2015-10-22 17:27:53 +08:00
|
|
|
},
|
|
|
|
add() {
|
2015-10-29 15:23:58 +08:00
|
|
|
index += 1;
|
2015-10-22 17:27:53 +08:00
|
|
|
this.setState({
|
|
|
|
tabs: this.state.tabs.concat({
|
2015-10-29 15:23:58 +08:00
|
|
|
title: 'title ' + index,
|
|
|
|
content: 'content ' + index,
|
|
|
|
index: index,
|
|
|
|
}),
|
|
|
|
activeKey: index.toString(),
|
2015-10-22 17:27:53 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
onChange(activeKey) {
|
2015-10-29 15:23:58 +08:00
|
|
|
console.log(activeKey);
|
|
|
|
this.setState({ activeKey });
|
2015-10-22 17:27:53 +08:00
|
|
|
},
|
|
|
|
render() {
|
2015-10-29 15:23:58 +08:00
|
|
|
const addBtn = <Icon style={addStyle} type="plus-circle" onClick={this.add} />;
|
2015-10-31 17:35:50 +08:00
|
|
|
const operations = <Button style={{ marginTop: 2 }}>操作</Button>;
|
2015-10-29 15:23:58 +08:00
|
|
|
return (
|
|
|
|
<Tabs onChange={this.onChange}
|
|
|
|
activeKey={this.state.activeKey}
|
|
|
|
tabBarExtraContent={operations}>
|
|
|
|
{
|
|
|
|
this.state.tabs.map(tab => (
|
|
|
|
<TabPane key={tab.index} tab={
|
|
|
|
<div>
|
|
|
|
{tab.title}
|
|
|
|
<Icon type="cross" style={closeStyle} onClick={this.remove.bind(this, tab.index)} />
|
|
|
|
</div>
|
|
|
|
}>{tab.content}</TabPane>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
<TabPane key="__add" disabled tab={addBtn} />
|
|
|
|
</Tabs>
|
|
|
|
);
|
2015-10-22 17:27:53 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ReactDOM.render(<Test />, document.getElementById('components-tabs-demo-add'));
|
|
|
|
````
|