function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Icon from '../icon';
import { obj, func } from '../util';
import Base from './base';
import Group from './group'; // preventDefault here can stop onBlur to keep focus state

function preventDefault(e) {
  e.preventDefault();
}
/** Input */


var Input =
/*#__PURE__*/
function (_Base) {
  _inherits(Input, _Base);

  function Input(props) {
    var _this;

    _classCallCheck(this, Input);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Input).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "togglePassword", function () {
      _this.setState({
        htmlType: _this.state.htmlType === 'password' ? undefined : 'password'
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleKeyDown", function (e) {
      if (e.keyCode === 13) {
        _this.props.onPressEnter(e);
      }

      _this.onKeyDown(e);
    });

    _defineProperty(_assertThisInitialized(_this), "handleKeyDownFromClear", function (e) {
      if (e.keyCode === 13) {
        _this.onClear(e);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onClear", function (e) {
      if (_this.props.disabled) {
        return;
      } // 非受控模式清空内部数据


      if (!('value' in _this.props)) {
        _this.setState({
          value: ''
        });
      }

      _this.props.onChange('', e, 'clear');

      _this.focus();
    });

    var value;

    if ('value' in props) {
      value = props.value;
    } else {
      value = props.defaultValue;
    }

    _this.state = {
      value: typeof value === 'undefined' ? '' : value,
      htmlType: props.htmlType
    };
    return _this;
  } // `Enter` was considered to be two chars in chrome , but one char in ie.
  // so we make all `Enter` to be two chars


  _createClass(Input, [{
    key: "getValueLength",
    value: function getValueLength(value) {
      var nv = "".concat(value);
      var strLen = this.props.getValueLength(nv);

      if (typeof strLen !== 'number') {
        strLen = nv.length;
      }

      return strLen;
    }
  }, {
    key: "renderControl",
    value: function renderControl() {
      var _this$props = this.props,
          hasClear = _this$props.hasClear,
          readOnly = _this$props.readOnly,
          state = _this$props.state,
          prefix = _this$props.prefix,
          hint = _this$props.hint,
          extra = _this$props.extra,
          locale = _this$props.locale,
          htmlType = _this$props.htmlType;
      var lenWrap = this.renderLength();
      var stateWrap = null; // if (state === 'success') {
      //   stateWrap = <Icon type="tick-o-fill" className={`${prefix}icon-success-filling`} />;
      // } else if (state === 'loading') {
      //   stateWrap = <Icon type="loading" />;
      // }

      if (state === 'loading') stateWrap = React.createElement(Icon, {
        type: "loading"
      });
      var clearWrap = null;
      var showClear = hasClear && !readOnly && !!"".concat(this.state.value);

      if (hint || showClear) {
        var hintIcon = null;

        if (hint) {
          hintIcon = React.createElement(Icon, {
            type: hint,
            className: "".concat(prefix, "input-hint")
          });
        } else {
          hintIcon = React.createElement(Icon, {
            type: "remove-o-fill",
            role: "button",
            tabIndex: "0",
            className: "".concat(prefix, "input-hint"),
            "aria-label": locale.clear,
            onClick: this.onClear,
            onMouseDown: preventDefault,
            onKeyDown: this.handleKeyDownFromClear
          });
        }

        clearWrap = React.createElement("span", {
          className: "".concat(prefix, "input-hint-wrap")
        }, showClear && hint ? React.createElement(Icon, {
          type: "remove-o-fill",
          role: "button",
          tabIndex: "0",
          className: "".concat(prefix, "input-clear"),
          "aria-label": locale.clear,
          onClick: this.onClear,
          onMouseDown: preventDefault,
          onKeyDown: this.handleKeyDownFromClear
        }) : null, hintIcon);
      }

      var passwordIcon = null;

      if (htmlType === 'password') {
        passwordIcon = React.createElement(Icon, {
          type: this.state.htmlType === 'password' ? 'eye-slash' : 'eye',
          onClick: this.togglePassword
        });
      }

      if (state === 'loading') {
        clearWrap = null;
      }

      return clearWrap || lenWrap || stateWrap || extra || passwordIcon ? React.createElement("span", {
        className: "".concat(prefix, "input-control")
      }, clearWrap, passwordIcon, lenWrap, stateWrap, extra) : null;
    }
  }, {
    key: "renderLabel",
    value: function renderLabel() {
      var _this$props2 = this.props,
          label = _this$props2.label,
          prefix = _this$props2.prefix,
          id = _this$props2.id;
      return label ? React.createElement("label", {
        className: "".concat(prefix, "input-label"),
        htmlFor: id
      }, label) : null;
    }
  }, {
    key: "renderInner",
    value: function renderInner(inner, cls) {
      return inner ? React.createElement("span", {
        className: cls
      }, inner) : null;
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames, _classNames2, _classNames3, _classNames4;

      var _this$props3 = this.props,
          size = _this$props3.size,
          htmlSize = _this$props3.htmlSize,
          autoComplete = _this$props3.autoComplete,
          autoFocus = _this$props3.autoFocus,
          disabled = _this$props3.disabled,
          style = _this$props3.style,
          innerBefore = _this$props3.innerBefore,
          innerAfter = _this$props3.innerAfter,
          innerBeforeClassName = _this$props3.innerBeforeClassName,
          innerAfterClassName = _this$props3.innerAfterClassName,
          className = _this$props3.className,
          hasBorder = _this$props3.hasBorder,
          prefix = _this$props3.prefix,
          addonBefore = _this$props3.addonBefore,
          addonAfter = _this$props3.addonAfter,
          addonTextBefore = _this$props3.addonTextBefore,
          addonTextAfter = _this$props3.addonTextAfter,
          inputRender = _this$props3.inputRender,
          rtl = _this$props3.rtl;
      var htmlType = this.state.htmlType;
      var hasAddon = addonBefore || addonAfter || addonTextBefore || addonTextAfter;
      var cls = classNames(this.getClass(), (_classNames = {}, _defineProperty(_classNames, "".concat(prefix).concat(size), true), _defineProperty(_classNames, "".concat(prefix, "hidden"), this.props.htmlType === 'hidden'), _defineProperty(_classNames, "".concat(prefix, "noborder"), !hasBorder || this.props.htmlType === 'file'), _defineProperty(_classNames, "".concat(prefix, "input-group-auto-width"), hasAddon), _defineProperty(_classNames, className, !!className && !hasAddon), _classNames));
      var innerCls = "".concat(prefix, "input-inner");
      var innerBeforeCls = classNames((_classNames2 = {}, _defineProperty(_classNames2, innerCls, true), _defineProperty(_classNames2, "".concat(prefix, "before"), true), _defineProperty(_classNames2, innerBeforeClassName, innerBeforeClassName), _classNames2));
      var innerAfterCls = classNames((_classNames3 = {}, _defineProperty(_classNames3, innerCls, true), _defineProperty(_classNames3, "".concat(prefix, "after"), true), _defineProperty(_classNames3, innerAfterClassName, innerAfterClassName), _classNames3));
      var props = this.getProps(); // custom data attributes are assigned to the top parent node
      // data-类自定义数据属性分配到顶层node节点

      var dataProps = obj.pickAttrsWith(this.props, 'data-'); // Custom props are transparently transmitted to the core input node by default
      // 自定义属性默认透传到核心node节点：input

      var others = obj.pickOthers(_extends({}, dataProps, Input.propTypes), this.props);
      var inputEl = React.createElement("input", _extends({}, others, props, {
        height: "100%",
        type: htmlType,
        size: htmlSize,
        autoFocus: autoFocus,
        autoComplete: autoComplete,
        onKeyDown: this.handleKeyDown,
        ref: this.saveRef
      }));
      var inputWrap = React.createElement("span", _extends({}, dataProps, {
        dir: rtl ? 'rtl' : undefined,
        className: cls,
        style: hasAddon ? undefined : style
      }), this.renderLabel(), this.renderInner(innerBefore, innerBeforeCls), inputRender(inputEl), this.renderInner(innerAfter, innerAfterCls), this.renderControl());
      var groupCls = classNames((_classNames4 = {}, _defineProperty(_classNames4, "".concat(prefix, "input-group-text"), true), _defineProperty(_classNames4, "".concat(prefix).concat(size), !!size), _defineProperty(_classNames4, "".concat(prefix, "disabled"), disabled), _classNames4));
      var addonBeforeCls = classNames(_defineProperty({}, groupCls, addonTextBefore));
      var addonAfterCls = classNames(_defineProperty({}, groupCls, addonTextAfter));

      if (hasAddon) {
        return React.createElement(Group, _extends({}, dataProps, {
          className: className,
          style: style,
          addonBefore: addonBefore || addonTextBefore,
          addonBeforeClassName: addonBeforeCls,
          addonAfter: addonAfter || addonTextAfter,
          addonAfterClassName: addonAfterCls
        }), inputWrap);
      }

      return inputWrap;
    }
  }]);

  return Input;
}(Base);

_defineProperty(Input, "displayName", 'Input');

_defineProperty(Input, "propTypes", _objectSpread({}, Base.propTypes, {
  /**
   * label
   */
  label: PropTypes.node,

  /**
   * 是否出现clear按钮
   */
  hasClear: PropTypes.bool,

  /**
   * 是否有边框
   */
  hasBorder: PropTypes.bool,

  /**
   * 状态
   * @enumdesc 错误, 校验中, 成功
   */
  state: PropTypes.oneOf(['error', 'loading', 'success']),

  /**
   * 尺寸
   * @enumdesc 小, 中
   */
  size: PropTypes.oneOf(['xs', 'small', 'medium', 'large']),

  /**
   * 按下回车的回调
   */
  onPressEnter: PropTypes.func,
  onClear: PropTypes.func,

  /**
   * 原生type
   */
  htmlType: PropTypes.string,
  htmlSize: PropTypes.string,

  /**
   * 水印 (Icon的type类型，和hasClear占用一个地方)
   */
  hint: PropTypes.string,

  /**
   * 文字前附加内容
   */
  innerBefore: PropTypes.node,

  /**
   * 文字后附加内容
   */
  innerAfter: PropTypes.node,

  /**
   * 输入框前附加内容
   */
  addonBefore: PropTypes.node,

  /**
   * 输入框后附加内容
   */
  addonAfter: PropTypes.node,

  /**
   * 输入框前附加文字
   */
  addonTextBefore: PropTypes.node,

  /**
   * 输入框后附加文字
   */
  addonTextAfter: PropTypes.node,

  /**
   * (原生input支持)
   */
  autoComplete: PropTypes.string,

  /**
   * 自动聚焦(原生input支持)
   */
  autoFocus: PropTypes.bool,
  inputRender: PropTypes.func,
  extra: PropTypes.node,
  innerBeforeClassName: PropTypes.string,
  innerAfterClassName: PropTypes.string
}));

_defineProperty(Input, "defaultProps", _objectSpread({}, Base.defaultProps, {
  size: 'medium',
  autoComplete: 'off',
  hasBorder: true,
  onPressEnter: func.noop,
  inputRender: function inputRender(el) {
    return el;
  }
}));

export { Input as default };