add.md 1.9 KB
Newer Older
Y
yiminghe 已提交
1 2
# 添加

A
afc163 已提交
3
- order: 4
Y
yiminghe 已提交
4

A
afc163 已提交
5
演示添加删除功能。
Y
yiminghe 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

---

````jsx
var Tabs = antd.Tabs;
var TabPane = Tabs.TabPane;
var Button = antd.Button;
var Icon = antd.Icon;

var index = 0;
var closeStyle = {
  position: 'absolute',
  top: 0,
  right: -10,
};
var addStyle = {
  pointerEvents: 'auto',
  color: 'black',
};

var Test = React.createClass({
  getInitialState() {
    return {
      tabs: [{
        title: 'title ' + index,
        content: 'content ' + index,
      }],
      activeKey: 'title ' + index
    };
  },
  remove(title, e) {
    e.stopPropagation();
    if(this.state.tabs.length === 1) {
      antd.message.error('仅剩一个,不能删除');
      return;
    }
    var foundIndex = 0;
    var tabs = this.state.tabs.filter(function (t, index) {
      if (t.title !== title) {
        return true;
      } else {
        foundIndex = index;
        return false;
      }
    });
    var activeKey = this.state.activeKey;
    if (activeKey === title) {
      if (foundIndex) {
        foundIndex--;
      }
      activeKey = tabs[foundIndex].title;
    }
    this.setState({tabs, activeKey})
  },
  add() {
    index++;
    this.setState({
      tabs: this.state.tabs.concat({
        title: 'title '+index,
        content: 'content '+index,
      })
    });
  },
  onChange(activeKey) {
    this.setState({activeKey});
  },
  render() {
    return <Tabs
    onChange={this.onChange}
    activeKey={this.state.activeKey}
Y
yiminghe 已提交
76
    tabBarExtraContent={<Button type="primary">其它操作</Button>}>
Y
yiminghe 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89
    {
      this.state.tabs.map((t)=>{
        return <TabPane key={t.title} tab={<div>{t.title} <Icon type="cross" style={closeStyle} onClick={this.remove.bind(this,t.title)}></Icon></div>}>
          {t.content}
        </TabPane>;
      }).concat(<TabPane key="__add" disabled tab={<Icon style={addStyle} type="plus-circle" onClick={this.add}></Icon>} />)
    }
    </Tabs>
  }
})

ReactDOM.render(<Test />, document.getElementById('components-tabs-demo-add'));
````