mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-29 20:08:37 +08:00
86 lines
2.0 KiB
Markdown
86 lines
2.0 KiB
Markdown
---
|
|
order: 11
|
|
title:
|
|
zh-CN: 自定义新增页签触发器
|
|
en-US: Customized trigger of new tab
|
|
---
|
|
## zh-CN
|
|
|
|
隐藏默认的页签增加图标,给自定义触发器绑定事件。
|
|
|
|
## en-US
|
|
|
|
Hide default plus icon, and bind event for customized trigger.
|
|
|
|
````jsx
|
|
import { Tabs, Button } from 'antd';
|
|
|
|
const TabPane = Tabs.TabPane;
|
|
|
|
class Demo extends React.Component {
|
|
constructor(props) {
|
|
super(props);
|
|
this.newTabIndex = 0;
|
|
const panes = [
|
|
{ title: 'Tab 1', content: 'Content of Tab Pane 1', key: '1' },
|
|
{ title: 'Tab 2', content: 'Content of Tab Pane 2', key: '2' },
|
|
];
|
|
this.state = {
|
|
activeKey: panes[0].key,
|
|
panes,
|
|
};
|
|
}
|
|
|
|
onChange = (activeKey) => {
|
|
this.setState({ activeKey });
|
|
}
|
|
|
|
onEdit = (targetKey, action) => {
|
|
this[action](targetKey);
|
|
}
|
|
|
|
add = () => {
|
|
const panes = this.state.panes;
|
|
const activeKey = `newTab${this.newTabIndex++}`;
|
|
panes.push({ title: 'New Tab', content: 'New Tab Pane', key: activeKey });
|
|
this.setState({ panes, activeKey });
|
|
}
|
|
|
|
remove = (targetKey) => {
|
|
let activeKey = this.state.activeKey;
|
|
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);
|
|
if (lastIndex >= 0 && activeKey === targetKey) {
|
|
activeKey = panes[lastIndex].key;
|
|
}
|
|
this.setState({ panes, activeKey });
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<div>
|
|
<div style={{ marginBottom: 16 }}>
|
|
<Button onClick={this.add}>ADD</Button>
|
|
</div>
|
|
<Tabs
|
|
hideAdd
|
|
onChange={this.onChange}
|
|
activeKey={this.state.activeKey}
|
|
type="editable-card"
|
|
onEdit={this.onEdit}
|
|
>
|
|
{this.state.panes.map(pane => <TabPane tab={pane.title} key={pane.key}>{pane.content}</TabPane>)}
|
|
</Tabs>
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
|
|
ReactDOM.render(<Demo />, mountNode);
|
|
````
|