2016-05-24 14:35:21 +08:00
|
|
|
---
|
|
|
|
order: 11
|
2017-02-04 01:13:35 +08:00
|
|
|
title:
|
2016-08-23 14:15:34 +08:00
|
|
|
zh-CN: 自定义新增页签触发器
|
|
|
|
en-US: Customized trigger of new tab
|
2016-05-24 14:35:21 +08:00
|
|
|
---
|
2019-05-07 14:57:32 +08:00
|
|
|
|
2016-08-03 10:39:20 +08:00
|
|
|
## zh-CN
|
2016-05-24 14:35:21 +08:00
|
|
|
|
|
|
|
隐藏默认的页签增加图标,给自定义触发器绑定事件。
|
|
|
|
|
2016-08-03 10:39:20 +08:00
|
|
|
## en-US
|
|
|
|
|
|
|
|
Hide default plus icon, and bind event for customized trigger.
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
|
|
|
import React, { useRef, useState } from 'react';
|
2016-05-24 14:35:21 +08:00
|
|
|
import { Tabs, Button } from 'antd';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-05-27 21:32:45 +08:00
|
|
|
const { TabPane } = Tabs;
|
2016-05-24 14:35:21 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const defaultPanes = Array.from({ length: 2 }).map((_, index) => {
|
|
|
|
const id = String(index + 1);
|
|
|
|
return { title: `Tab ${id}`, content: `Content of Tab Pane ${index + 1}`, key: id };
|
|
|
|
});
|
2017-02-19 22:07:45 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
|
|
|
const [activeKey, setActiveKey] = useState(defaultPanes[0].key);
|
|
|
|
const [panes, setPanes] = useState(defaultPanes);
|
|
|
|
const newTabIndex = useRef(0);
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const onChange = (key: string) => {
|
|
|
|
setActiveKey(key);
|
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 add = () => {
|
|
|
|
const newActiveKey = `newTab${newTabIndex.current++}`;
|
|
|
|
setPanes([...panes, { title: 'New Tab', content: 'New Tab Pane', key: newActiveKey }]);
|
|
|
|
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) => {
|
|
|
|
let lastIndex = -1;
|
|
|
|
panes.forEach((pane, i) => {
|
2016-05-24 14:35:21 +08:00
|
|
|
if (pane.key === targetKey) {
|
|
|
|
lastIndex = i - 1;
|
|
|
|
}
|
|
|
|
});
|
2019-02-04 11:29:28 +08:00
|
|
|
if (panes.length && activeKey === targetKey) {
|
2022-05-19 09:46:26 +08:00
|
|
|
let newActiveKey: string;
|
2019-02-04 11:29:28 +08:00
|
|
|
if (lastIndex >= 0) {
|
2022-05-19 09:46:26 +08:00
|
|
|
newActiveKey = panes[lastIndex].key;
|
2019-02-04 11:29:28 +08:00
|
|
|
} else {
|
2022-05-19 09:46:26 +08:00
|
|
|
newActiveKey = panes[0].key;
|
2019-02-04 11:29:28 +08:00
|
|
|
}
|
2022-05-19 09:46:26 +08:00
|
|
|
setActiveKey(newActiveKey);
|
|
|
|
}
|
|
|
|
const newPanes = panes.filter(pane => pane.key !== targetKey);
|
|
|
|
setPanes(newPanes);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onEdit = (targetKey: string, action: 'add' | 'remove') => {
|
|
|
|
if (action === 'add') {
|
|
|
|
add();
|
|
|
|
} else {
|
|
|
|
remove(targetKey);
|
2016-05-24 14:35:21 +08:00
|
|
|
}
|
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 (
|
|
|
|
<div>
|
|
|
|
<div style={{ marginBottom: 16 }}>
|
|
|
|
<Button onClick={add}>ADD</Button>
|
2016-05-24 14:35:21 +08:00
|
|
|
</div>
|
2022-05-19 09:46:26 +08:00
|
|
|
<Tabs hideAdd onChange={onChange} activeKey={activeKey} type="editable-card" onEdit={onEdit}>
|
|
|
|
{panes.map(pane => (
|
|
|
|
<TabPane tab={pane.title} key={pane.key}>
|
|
|
|
{pane.content}
|
|
|
|
</TabPane>
|
|
|
|
))}
|
|
|
|
</Tabs>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2016-05-24 14:35:21 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|