index.jsx 2.3 KB
Newer Older
1
import RcTabs from 'rc-tabs';
A
afc163 已提交
2
import React, { cloneElement } from 'react';
A
afc163 已提交
3
import classNames from 'classnames';
A
afc163 已提交
4
import Icon from '../icon';
Y
yiminghe 已提交
5

6
export default class Tabs extends React.Component {
A
afc163 已提交
7 8 9 10 11 12 13 14
  constructor(props) {
    super(props);
    [
      'createNewTab',
      'removeTab',
      'handleChange',
    ].forEach((method) => this[method] = this[method].bind(this));
  }
A
afc163 已提交
15 16
  createNewTab(targetKey) {
    this.props.onEdit(targetKey, 'add');
A
afc163 已提交
17
  }
A
afc163 已提交
18
  removeTab(targetKey, e) {
A
afc163 已提交
19
    e.stopPropagation();
A
afc163 已提交
20 21
    if (!targetKey) {
      return;
A
afc163 已提交
22
    }
A
afc163 已提交
23
    this.props.onEdit(targetKey, 'remove');
A
afc163 已提交
24 25 26 27
  }
  handleChange(activeKey) {
    this.props.onChange(activeKey);
  }
Y
yiminghe 已提交
28
  render() {
A
afc163 已提交
29
    let { prefixCls, size, tabPosition, animation, type,
A
afc163 已提交
30
          children, tabBarExtraContent } = this.props;
A
afc163 已提交
31
    let className = classNames({
32
      [this.props.className]: !!this.props.className,
33 34 35
      [`${prefixCls}-mini`]: size === 'small' || size === 'mini',
      [`${prefixCls}-vertical`]: tabPosition === 'left' || tabPosition === 'right',
      [`${prefixCls}-card`]: type.indexOf('card') >= 0,
A
afc163 已提交
36
      [`${prefixCls}-${type}`]: true,
A
afc163 已提交
37
    });
A
afc163 已提交
38
    if (tabPosition === 'left' || tabPosition === 'right' || type.indexOf('card') >= 0) {
39
      animation = null;
A
afc163 已提交
40
    }
A
afc163 已提交
41
    // only card type tabs can be added and closed
A
afc163 已提交
42
    if (type === 'editable-card') {
A
afc163 已提交
43 44 45 46 47 48 49
      children = children.map((child, index) => {
        return cloneElement(child, {
          tab: <div>
            {child.props.tab}
            <Icon type="cross" onClick={this.removeTab.bind(this, child.key)} />
          </div>,
          key: child.key || index,
A
afc163 已提交
50
        });
A
afc163 已提交
51
      });
A
afc163 已提交
52
      // Add new tab handler
53 54
      tabBarExtraContent = (
        <span>
55
          <Icon type="plus" className={`${prefixCls}-new-tab`} onClick={this.createNewTab} />
56 57 58
          {tabBarExtraContent}
        </span>
      );
A
afc163 已提交
59
    }
A
afc163 已提交
60

61
    return (
62
      <RcTabs {...this.props}
63
        className={className}
A
afc163 已提交
64 65 66 67 68
        tabBarExtraContent={
          <div className={`${prefixCls}-extra-content`}>
            {tabBarExtraContent}
          </div>
        }
69 70
        onChange={this.handleChange}
        animation={animation}>
71
        {children}
72
      </RcTabs>
73
    );
Y
yiminghe 已提交
74 75 76
  }
}

77
Tabs.defaultProps = {
A
afc163 已提交
78
  prefixCls: 'ant-tabs',
79
  animation: 'slide-horizontal',
A
afc163 已提交
80
  type: 'line', // or 'card' 'editable-card'
A
afc163 已提交
81
  onChange() {},
A
afc163 已提交
82
  onEdit() {},
Y
yiminghe 已提交
83 84
};

85
Tabs.TabPane = RcTabs.TabPane;