mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-04 17:09:46 +08:00
7fd093bd0a
* docs: add general components TS demo * docs: add layout components TS demo * docs: add navigation components TS demo * docs: add data entry components TS demo * chore(deps): add types for qs * docs: add data display TS demo * docs: add feedback components TS demo * docs: add other components TS demo * chore(deps): add types * docs: unified demo code style * docs: fix lint error * docs: add demo TS type * docs: fix demo TS type * test: update snapshot * docs: fix TS demo * feat: update Rate character type * docs: fix lint error * feat: update Rate character type * feat: update Rate character type
91 lines
2.1 KiB
Markdown
91 lines
2.1 KiB
Markdown
---
|
|
order: 9
|
|
title:
|
|
zh-CN: 新增和关闭页签
|
|
en-US: Add & close tab
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
只有卡片样式的页签支持新增和关闭选项。使用 `closable={false}` 禁止关闭。
|
|
|
|
## en-US
|
|
|
|
Only card type Tabs support adding & closable. +Use `closable={false}` to disable close.
|
|
|
|
```tsx
|
|
import React, { useRef, useState } from 'react';
|
|
import { Tabs } from 'antd';
|
|
|
|
const { TabPane } = Tabs;
|
|
|
|
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,
|
|
},
|
|
];
|
|
|
|
const App: React.FC = () => {
|
|
const [activeKey, setActiveKey] = useState(initialPanes[0].key);
|
|
const [panes, setPanes] = useState(initialPanes);
|
|
const newTabIndex = useRef(0);
|
|
|
|
const onChange = (newActiveKey: string) => {
|
|
setActiveKey(newActiveKey);
|
|
};
|
|
|
|
const add = () => {
|
|
const newActiveKey = `newTab${newTabIndex.current++}`;
|
|
const newPanes = [...panes];
|
|
newPanes.push({ title: 'New Tab', content: 'Content of new Tab', key: newActiveKey });
|
|
setPanes(newPanes);
|
|
setActiveKey(newActiveKey);
|
|
};
|
|
|
|
const remove = (targetKey: string) => {
|
|
let newActiveKey = activeKey;
|
|
let lastIndex = -1;
|
|
panes.forEach((pane, i) => {
|
|
if (pane.key === targetKey) {
|
|
lastIndex = i - 1;
|
|
}
|
|
});
|
|
const newPanes = panes.filter(pane => pane.key !== targetKey);
|
|
if (newPanes.length && newActiveKey === targetKey) {
|
|
if (lastIndex >= 0) {
|
|
newActiveKey = newPanes[lastIndex].key;
|
|
} else {
|
|
newActiveKey = newPanes[0].key;
|
|
}
|
|
}
|
|
setPanes(newPanes);
|
|
setActiveKey(newActiveKey);
|
|
};
|
|
|
|
const onEdit = (targetKey: string, action: 'add' | 'remove') => {
|
|
if (action === 'add') {
|
|
add();
|
|
} else {
|
|
remove(targetKey);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Tabs type="editable-card" onChange={onChange} activeKey={activeKey} onEdit={onEdit}>
|
|
{panes.map(pane => (
|
|
<TabPane tab={pane.title} key={pane.key} closable={pane.closable}>
|
|
{pane.content}
|
|
</TabPane>
|
|
))}
|
|
</Tabs>
|
|
);
|
|
};
|
|
|
|
export default App;
|
|
```
|