2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 9
|
2016-08-24 10:33:25 +08:00
|
|
|
title:
|
2016-08-23 14:15:34 +08:00
|
|
|
zh-CN: 新增和关闭页签
|
|
|
|
en-US: Add & close tab
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2015-12-15 16:34:02 +08:00
|
|
|
|
2016-08-03 10:39:20 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2015-12-17 12:33:34 +08:00
|
|
|
只有卡片样式的页签支持新增和关闭选项。
|
2015-12-15 16:34:02 +08:00
|
|
|
|
2016-08-03 10:39:20 +08:00
|
|
|
## en-US
|
|
|
|
|
|
|
|
Only card type Tabs support adding & closeable.
|
|
|
|
|
2017-02-13 10:55:53 +08:00
|
|
|
````jsx
|
2015-12-15 16:34:02 +08:00
|
|
|
import { Tabs } from 'antd';
|
|
|
|
const TabPane = Tabs.TabPane;
|
|
|
|
|
2017-02-19 22:07:45 +08:00
|
|
|
|
|
|
|
class Demo extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2015-12-15 16:34:02 +08:00
|
|
|
this.newTabIndex = 0;
|
|
|
|
const panes = [
|
2017-02-19 22:07:45 +08:00
|
|
|
{ title: 'Tab 1', content: 'Content of Tab Pane 1', key: '1' },
|
|
|
|
{ title: 'Tab 2', content: 'Content of Tab Pane 2', key: '2' },
|
2015-12-15 16:34:02 +08:00
|
|
|
];
|
2017-02-19 22:07:45 +08:00
|
|
|
this.state = {
|
2015-12-15 16:34:02 +08:00
|
|
|
activeKey: panes[0].key,
|
2016-02-17 15:57:33 +08:00
|
|
|
panes,
|
2015-12-15 16:34:02 +08:00
|
|
|
};
|
2017-02-19 22:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onChange = (activeKey) => {
|
2015-12-15 16:34:02 +08:00
|
|
|
this.setState({ activeKey });
|
2017-02-19 22:07:45 +08:00
|
|
|
}
|
|
|
|
onEdit = (targetKey, action) => {
|
2015-12-15 16:34:02 +08:00
|
|
|
this[action](targetKey);
|
2017-02-19 22:07:45 +08:00
|
|
|
}
|
|
|
|
add = () => {
|
2015-12-15 16:34:02 +08:00
|
|
|
const panes = this.state.panes;
|
2016-02-17 18:04:42 +08:00
|
|
|
const activeKey = `newTab${this.newTabIndex++}`;
|
2016-08-03 10:39:20 +08:00
|
|
|
panes.push({ title: 'New Tab', content: 'Content of new Tab', key: activeKey });
|
2015-12-15 16:34:02 +08:00
|
|
|
this.setState({ panes, activeKey });
|
2017-02-19 22:07:45 +08:00
|
|
|
}
|
|
|
|
remove = (targetKey) => {
|
2015-12-15 16:34:02 +08:00
|
|
|
let activeKey = this.state.activeKey;
|
2016-03-02 20:25:19 +08:00
|
|
|
let lastIndex;
|
|
|
|
this.state.panes.forEach((pane, i) => {
|
|
|
|
if (pane.key === targetKey) {
|
|
|
|
lastIndex = i - 1;
|
|
|
|
}
|
|
|
|
});
|
2015-12-15 16:34:02 +08:00
|
|
|
const panes = this.state.panes.filter(pane => pane.key !== targetKey);
|
2016-03-02 20:25:19 +08:00
|
|
|
if (lastIndex >= 0 && activeKey === targetKey) {
|
|
|
|
activeKey = panes[lastIndex].key;
|
2015-12-15 16:34:02 +08:00
|
|
|
}
|
|
|
|
this.setState({ panes, activeKey });
|
2017-02-19 22:07:45 +08:00
|
|
|
}
|
2015-12-15 16:34:02 +08:00
|
|
|
render() {
|
|
|
|
return (
|
2016-07-06 17:41:04 +08:00
|
|
|
<Tabs
|
|
|
|
onChange={this.onChange}
|
|
|
|
activeKey={this.state.activeKey}
|
|
|
|
type="editable-card"
|
|
|
|
onEdit={this.onEdit}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2016-07-06 17:41:04 +08:00
|
|
|
{this.state.panes.map(pane => <TabPane tab={pane.title} key={pane.key}>{pane.content}</TabPane>)}
|
2015-12-15 16:34:02 +08:00
|
|
|
</Tabs>
|
|
|
|
);
|
2017-02-19 22:07:45 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 16:34:02 +08:00
|
|
|
|
2015-12-29 12:08:58 +08:00
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
2015-12-15 16:34:02 +08:00
|
|
|
````
|