import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
/**
 * Tab
 * */

var Tab =
/*#__PURE__*/
function (_PureComponent) {
  _inherits(Tab, _PureComponent);

  function Tab() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Tab);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Tab)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "getEl", function (el) {
      _this.el = el;
    });

    _defineProperty(_assertThisInitialized(_this), "onClick", function (e) {
      var _this$props = _this.props,
          disabled = _this$props.disabled,
          value = _this$props.value,
          onSelect = _this$props.onSelect,
          onClick = _this$props.onClick,
          index = _this$props.index;

      if (!disabled) {
        onSelect && onSelect(e, value != null ? value : index);
      }

      onClick && onClick(e);
    });

    _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (event) {
      var onKeyDown = _this.props.onKeyDown;
      var key = event.key;

      if (!_this.keyDown && key === ' ') {
        _this.keyDown = true;
        event.persist();
      }

      if (onKeyDown) {
        onKeyDown(event);
      }

      if (event.target === _this.el && (key === ' ' || key === 'Enter')) {
        event.preventDefault();

        _this.onClick(event);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onKeyUp", function (event) {
      if (event === ' ') {
        _this.keyDown = false;
        event.persist();
      }

      var onKeyUp = _this.props.onKeyUp;

      if (onKeyUp) {
        onKeyUp(event);
      }
    });

    return _this;
  }

  _createClass(Tab, [{
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props2 = this.props,
          className = _this$props2.className,
          index = _this$props2.index,
          disabled = _this$props2.disabled,
          prefixCls = _this$props2.prefixCls,
          value = _this$props2.value,
          selected = _this$props2.selected,
          fullWidth = _this$props2.fullWidth,
          size = _this$props2.size,
          children = _this$props2.children,
          other = _objectWithoutProperties(_this$props2, ["className", "index", "disabled", "prefixCls", "value", "selected", "fullWidth", "size", "children"]);

      var classes = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-full-width"), fullWidth), _defineProperty(_classNames, "".concat(prefixCls, "-selected"), !disabled && selected), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(size), size), _classNames), className);
      return React.createElement("div", _extends({}, other, {
        onKeyDown: this.onKeyDown,
        onKeyUp: this.onKeyUp,
        ref: this.getEl,
        tabIndex: disabled ? -1 : 0,
        role: "tab",
        className: classes,
        onClick: this.onClick,
        "aria-selected": selected
      }), React.createElement("div", {
        className: "".concat(prefixCls, "-text")
      }, children));
    }
  }]);

  return Tab;
}(PureComponent);

_defineProperty(Tab, "propTypes", {
  prefixCls: PropTypes.string,
  className: PropTypes.string,

  /** tab值 */
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),

  /** 序号 */
  index: PropTypes.number,

  /** 选中回调 */
  onSelect: PropTypes.func,

  /** 是否选中 */
  selected: PropTypes.bool,

  /** 是否禁用 */
  disabled: PropTypes.bool,

  /** 是否撑满宽度 */
  fullWidth: PropTypes.bool,

  /** 尺寸 */
  size: PropTypes.oneOf(['large', 'medium', 'small'])
});

_defineProperty(Tab, "defaultProps", {
  prefixCls: 'ne-tab',
  selected: false,
  disabled: false,
  fullWidth: false,
  size: 'medium'
});

export { Tab as default };