You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.1 KiB

9 years ago
---
order: 11
title:
zh-CN: 自定义新增页签触发器
en-US: Customized trigger of new tab
9 years ago
---
## zh-CN
9 years ago
隐藏默认的页签增加图标,给自定义触发器绑定事件。
## en-US
Hide default plus icon, and bind event for customized trigger.
```jsx
9 years ago
import { Tabs, Button } from 'antd';
const { TabPane } = Tabs;
9 years ago
class Demo extends React.Component {
constructor(props) {
super(props);
9 years ago
this.newTabIndex = 0;
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 };
});
this.state = {
9 years ago
activeKey: panes[0].key,
panes,
};
}
onChange = activeKey => {
9 years ago
this.setState({ activeKey });
};
onEdit = (targetKey, action) => {
9 years ago
this[action](targetKey);
};
add = () => {
const { panes } = this.state;
9 years ago
const activeKey = `newTab${this.newTabIndex++}`;
panes.push({ title: 'New Tab', content: 'New Tab Pane', key: activeKey });
9 years ago
this.setState({ panes, activeKey });
};
remove = targetKey => {
let { activeKey } = this.state;
9 years ago
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 (panes.length && activeKey === targetKey) {
if (lastIndex >= 0) {
activeKey = panes[lastIndex].key;
} else {
activeKey = panes[0].key;
}
9 years ago
}
this.setState({ panes, activeKey });
};
9 years ago
render() {
return (
<div>
<div style={{ marginBottom: 16 }}>
<Button onClick={this.add}>ADD</Button>
9 years ago
</div>
9 years ago
<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>
))}
9 years ago
</Tabs>
</div>
);
}
}
9 years ago
export default Demo;
```