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.
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
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
|
|
|
|
2017-02-19 22:07:45 +08:00
|
|
|
class Demo extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-05-24 14:35:21 +08:00
|
|
|
this.newTabIndex = 0;
|
2021-11-17 22:35:27 +08:00
|
|
|
const panes = new Array(2).fill(null).map((_, index) => {
|
|
|
|
const id = String(index + 1);
|
|
|
|
return { title: `Tab ${id}`, content: `Content of Tab Pane ${id}`, key: id };
|
|
|
|
});
|
2017-02-19 22:07:45 +08:00
|
|
|
this.state = {
|
2016-05-24 14:35:21 +08:00
|
|
|
activeKey: panes[0].key,
|
|
|
|
panes,
|
|
|
|
};
|
2017-02-19 22:07:45 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
onChange = activeKey => {
|
2016-05-24 14:35:21 +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) => {
|
2016-05-24 14:35:21 +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-05-24 14:35:21 +08:00
|
|
|
const activeKey = `newTab${this.newTabIndex++}`;
|
2016-08-03 10:39:20 +08:00
|
|
|
panes.push({ title: 'New Tab', content: 'New Tab Pane', key: activeKey });
|
2016-05-24 14:35:21 +08:00
|
|
|
this.setState({ panes, 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 => {
|
2019-06-19 19:09:08 +08:00
|
|
|
let { activeKey } = this.state;
|
2016-05-24 14:35:21 +08:00
|
|
|
let lastIndex;
|
|
|
|
this.state.panes.forEach((pane, i) => {
|
|
|
|
if (pane.key === targetKey) {
|
|
|
|
lastIndex = i - 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const panes = this.state.panes.filter(pane => pane.key !== targetKey);
|
2019-02-04 11:29:28 +08:00
|
|
|
if (panes.length && activeKey === targetKey) {
|
|
|
|
if (lastIndex >= 0) {
|
|
|
|
activeKey = panes[lastIndex].key;
|
|
|
|
} else {
|
|
|
|
activeKey = panes[0].key;
|
|
|
|
}
|
2016-05-24 14:35:21 +08:00
|
|
|
}
|
|
|
|
this.setState({ panes, activeKey });
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2016-05-24 14:35:21 +08:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div style={{ marginBottom: 16 }}>
|
2017-02-04 22:35:33 +08:00
|
|
|
<Button onClick={this.add}>ADD</Button>
|
2016-05-24 14:35:21 +08:00
|
|
|
</div>
|
2016-07-06 17:41:04 +08:00
|
|
|
<Tabs
|
|
|
|
hideAdd
|
|
|
|
onChange={this.onChange}
|
|
|
|
activeKey={this.state.activeKey}
|
|
|
|
type="editable-card"
|
|
|
|
onEdit={this.onEdit}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2019-05-07 14:57:32 +08:00
|
|
|
{this.state.panes.map(pane => (
|
|
|
|
<TabPane tab={pane.title} key={pane.key}>
|
|
|
|
{pane.content}
|
|
|
|
</TabPane>
|
|
|
|
))}
|
2016-05-24 14:35:21 +08:00
|
|
|
</Tabs>
|
|
|
|
</div>
|
|
|
|
);
|
2017-02-19 22:07:45 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-24 14:35:21 +08:00
|
|
|
|
2022-04-03 23:27:45 +08:00
|
|
|
export default () => <Demo />;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|