ant-design/components/tabs/demo/editable-card.md

62 lines
1.5 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 9
title: 新增和关闭页签
---
2015-12-15 16:34:02 +08:00
2015-12-17 12:33:34 +08:00
只有卡片样式的页签支持新增和关闭选项。
2015-12-15 16:34:02 +08:00
````jsx
import { Tabs } from 'antd';
const TabPane = Tabs.TabPane;
const Demo = React.createClass({
getInitialState() {
this.newTabIndex = 0;
const panes = [
<TabPane tab="选项卡" key="1">选项卡一内容</TabPane>,
<TabPane tab="选项卡" key="2">选项卡二内容</TabPane>,
];
return {
activeKey: panes[0].key,
panes,
2015-12-15 16:34:02 +08:00
};
},
onChange(activeKey) {
this.setState({ activeKey });
},
onEdit(targetKey, action) {
this[action](targetKey);
},
2015-12-27 16:20:59 +08:00
add() {
2015-12-15 16:34:02 +08:00
const panes = this.state.panes;
const activeKey = `newTab${this.newTabIndex++}`;
2015-12-15 16:38:53 +08:00
panes.push(<TabPane tab="新建页签" key={activeKey}>新页面</TabPane>);
2015-12-15 16:34:02 +08:00
this.setState({ panes, activeKey });
},
remove(targetKey) {
let activeKey = this.state.activeKey;
2016-03-02 20:25:19 +08:00
let lastIndex;
this.state.panes.forEach((pane, i) => {
if (pane.key === targetKey) {
lastIndex = i - 1;
}
});
2015-12-15 16:34:02 +08:00
const panes = this.state.panes.filter(pane => pane.key !== targetKey);
2016-03-02 20:25:19 +08:00
if (lastIndex >= 0 && activeKey === targetKey) {
activeKey = panes[lastIndex].key;
2015-12-15 16:34:02 +08:00
}
this.setState({ panes, activeKey });
},
render() {
return (
<Tabs onChange={this.onChange} activeKey={this.state.activeKey}
type="editable-card" onEdit={this.onEdit}>
{this.state.panes}
</Tabs>
);
2016-05-11 09:32:33 +08:00
},
2015-12-15 16:34:02 +08:00
});
ReactDOM.render(<Demo />, mountNode);
2015-12-15 16:34:02 +08:00
````