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
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2015-12-15 16:34:02 +08:00
|
|
|
import { Tabs } from 'antd';
|
2022-05-23 14:37:16 +08:00
|
|
|
import React, { useRef, useState } from 'react';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-08-05 10:49:08 +08:00
|
|
|
const initialItems = [
|
|
|
|
{ label: 'Tab 1', children: 'Content of Tab 1', key: '1' },
|
|
|
|
{ label: 'Tab 2', children: 'Content of Tab 2', key: '2' },
|
2020-06-05 20:11:25 +08:00
|
|
|
{
|
2022-08-05 10:49:08 +08:00
|
|
|
label: 'Tab 3',
|
|
|
|
children: 'Content of Tab 3',
|
2020-06-05 20:11:25 +08:00
|
|
|
key: '3',
|
|
|
|
closable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
2022-08-05 10:49:08 +08:00
|
|
|
const [activeKey, setActiveKey] = useState(initialItems[0].key);
|
|
|
|
const [items, setItems] = useState(initialItems);
|
2022-05-19 09:46:26 +08:00
|
|
|
const newTabIndex = useRef(0);
|
2020-06-05 20:11:25 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const onChange = (newActiveKey: string) => {
|
|
|
|
setActiveKey(newActiveKey);
|
2020-06-05 20:11:25 +08:00
|
|
|
};
|
2017-02-19 22:07:45 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const add = () => {
|
|
|
|
const newActiveKey = `newTab${newTabIndex.current++}`;
|
2022-08-05 10:49:08 +08:00
|
|
|
const newPanes = [...items];
|
|
|
|
newPanes.push({ label: 'New Tab', children: 'Content of new Tab', key: newActiveKey });
|
|
|
|
setItems(newPanes);
|
2022-05-19 09:46:26 +08:00
|
|
|
setActiveKey(newActiveKey);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const remove = (targetKey: string) => {
|
2020-06-05 20:11:25 +08:00
|
|
|
let newActiveKey = activeKey;
|
2022-05-19 09:46:26 +08:00
|
|
|
let lastIndex = -1;
|
2022-08-05 10:49:08 +08:00
|
|
|
items.forEach((item, i) => {
|
|
|
|
if (item.key === targetKey) {
|
2016-03-02 20:25:19 +08:00
|
|
|
lastIndex = i - 1;
|
|
|
|
}
|
|
|
|
});
|
2022-08-05 10:49:08 +08:00
|
|
|
const newPanes = items.filter(item => item.key !== targetKey);
|
2020-06-05 20:11:25 +08:00
|
|
|
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
|
|
|
}
|
2022-08-05 10:49:08 +08:00
|
|
|
setItems(newPanes);
|
2022-05-19 09:46:26 +08:00
|
|
|
setActiveKey(newActiveKey);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onEdit = (targetKey: string, action: 'add' | 'remove') => {
|
|
|
|
if (action === 'add') {
|
|
|
|
add();
|
|
|
|
} else {
|
|
|
|
remove(targetKey);
|
|
|
|
}
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
return (
|
2022-08-05 10:49:08 +08:00
|
|
|
<Tabs
|
|
|
|
type="editable-card"
|
|
|
|
onChange={onChange}
|
|
|
|
activeKey={activeKey}
|
|
|
|
onEdit={onEdit}
|
|
|
|
items={items}
|
|
|
|
/>
|
2022-05-19 09:46:26 +08:00
|
|
|
);
|
|
|
|
};
|
2015-12-15 16:34:02 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|