ant-design/components/tabs/demo/custom-add-trigger.md

80 lines
1.9 KiB
Markdown
Raw Normal View History

2016-05-24 14:35:21 +08:00
---
order: 11
2017-02-04 01:13:35 +08:00
title:
zh-CN: 自定义新增页签触发器
en-US: Customized trigger of new tab
2016-05-24 14:35:21 +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.
2017-01-19 15:19:03 +08:00
````__react
2016-05-24 14:35:21 +08:00
import { Tabs, Button } from 'antd';
const TabPane = Tabs.TabPane;
const Demo = React.createClass({
getInitialState() {
this.newTabIndex = 0;
const panes = [
2016-08-03 10:39:20 +08:00
{ title: 'Tab 1', content: 'Content of Tab Pane 1', key: '1' },
{ title: 'Tab 2', content: 'Content of Tab Pane 2', key: '2' },
2016-05-24 14:35:21 +08:00
];
return {
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++}`;
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 });
},
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 }}>
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-09-20 13:49:38 +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>
);
},
});
ReactDOM.render(<Demo />, mountNode);
````