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
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
只有卡片样式的页签支持新增和关闭选项。使用 `closable={false}` 禁止关闭。
|
2015-12-15 16:34:02 +08:00
|
|
|
|
2016-08-03 10:39:20 +08:00
|
|
|
## en-US
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
Only card type Tabs support adding & closable. +Use `closable={false}` to disable close.
|
2016-08-03 10:39:20 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
2015-12-15 16:34:02 +08:00
|
|
|
import { Tabs } from 'antd';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-05-27 21:32:45 +08:00
|
|
|
const { TabPane } = Tabs;
|
2015-12-15 16:34:02 +08:00
|
|
|
|
2020-06-05 20:11:25 +08:00
|
|
|
const initialPanes = [
|
|
|
|
{ title: 'Tab 1', content: 'Content of Tab 1', key: '1' },
|
|
|
|
{ title: 'Tab 2', content: 'Content of Tab 2', key: '2' },
|
|
|
|
{
|
|
|
|
title: 'Tab 3',
|
|
|
|
content: 'Content of Tab 3',
|
|
|
|
key: '3',
|
|
|
|
closable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2017-02-19 22:07:45 +08:00
|
|
|
class Demo extends React.Component {
|
2020-06-05 20:11:25 +08:00
|
|
|
newTabIndex = 0;
|
|
|
|
|
|
|
|
state = {
|
|
|
|
activeKey: initialPanes[0].key,
|
|
|
|
panes: initialPanes,
|
|
|
|
};
|
2017-02-19 22:07:45 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
onChange = activeKey => {
|
2015-12-15 16:34:02 +08:00
|
|
|
this.setState({ activeKey });
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2017-02-19 22:07:45 +08:00
|
|
|
onEdit = (targetKey, action) => {
|
2015-12-15 16:34:02 +08:00
|
|
|
this[action](targetKey);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2017-02-19 22:07:45 +08:00
|
|
|
add = () => {
|
2019-06-19 19:09:08 +08:00
|
|
|
const { panes } = this.state;
|
2016-02-17 18:04:42 +08:00
|
|
|
const activeKey = `newTab${this.newTabIndex++}`;
|
2020-06-05 20:11:25 +08:00
|
|
|
const newPanes = [...panes];
|
|
|
|
newPanes.push({ title: 'New Tab', content: 'Content of new Tab', key: activeKey });
|
|
|
|
this.setState({
|
|
|
|
panes: newPanes,
|
|
|
|
activeKey,
|
|
|
|
});
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
remove = targetKey => {
|
2020-06-05 20:11:25 +08:00
|
|
|
const { panes, activeKey } = this.state;
|
|
|
|
let newActiveKey = activeKey;
|
2016-03-02 20:25:19 +08:00
|
|
|
let lastIndex;
|
2020-06-05 20:11:25 +08:00
|
|
|
panes.forEach((pane, i) => {
|
2016-03-02 20:25:19 +08:00
|
|
|
if (pane.key === targetKey) {
|
|
|
|
lastIndex = i - 1;
|
|
|
|
}
|
|
|
|
});
|
2020-06-05 20:11:25 +08:00
|
|
|
const newPanes = panes.filter(pane => pane.key !== targetKey);
|
|
|
|
if (newPanes.length && newActiveKey === targetKey) {
|
2019-02-04 11:29:28 +08:00
|
|
|
if (lastIndex >= 0) {
|
2020-06-05 20:11:25 +08:00
|
|
|
newActiveKey = newPanes[lastIndex].key;
|
2019-02-04 11:29:28 +08:00
|
|
|
} else {
|
2020-06-05 20:11:25 +08:00
|
|
|
newActiveKey = newPanes[0].key;
|
2019-02-04 11:29:28 +08:00
|
|
|
}
|
2015-12-15 16:34:02 +08:00
|
|
|
}
|
2020-06-05 20:11:25 +08:00
|
|
|
this.setState({
|
|
|
|
panes: newPanes,
|
|
|
|
activeKey: newActiveKey,
|
|
|
|
});
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2015-12-15 16:34:02 +08:00
|
|
|
render() {
|
2020-06-05 20:11:25 +08:00
|
|
|
const { panes, activeKey } = this.state;
|
2015-12-15 16:34:02 +08:00
|
|
|
return (
|
2016-07-06 17:41:04 +08:00
|
|
|
<Tabs
|
|
|
|
type="editable-card"
|
2020-06-05 20:11:25 +08:00
|
|
|
onChange={this.onChange}
|
|
|
|
activeKey={activeKey}
|
2016-07-06 17:41:04 +08:00
|
|
|
onEdit={this.onEdit}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2020-06-05 20:11:25 +08:00
|
|
|
{panes.map(pane => (
|
|
|
|
<TabPane tab={pane.title} key={pane.key} closable={pane.closable}>
|
2019-05-07 14:57:32 +08:00
|
|
|
{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
|
|
|
|
2022-04-03 23:27:45 +08:00
|
|
|
export default () => <Demo />;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|