"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _util = require("../util");

var _zhCn = _interopRequireDefault(require("../locale/zh-cn"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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 _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; }

var Base =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Base, _React$Component);

  function Base() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Base);

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

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

    _defineProperty(_assertThisInitialized(_this), "saveRef", function (input) {
      _this.inputRef = input;
    });

    return _this;
  }

  _createClass(Base, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if ('value' in nextProps) {
        this.setState({
          value: typeof nextProps.value === 'undefined' ? '' : nextProps.value
        });
      }
    }
  }, {
    key: "ieHack",
    value: function ieHack(value) {
      return value;
    }
  }, {
    key: "onChange",
    value: function onChange(e) {
      var value = e.target.value;

      if (this.props.trim) {
        value = value.trim();
      }

      value = this.ieHack(value); // not controlled

      if (!('value' in this.props)) {
        this.setState({
          value: value
        });
      } // Number('') = 0


      if (value && this.props.htmlType === 'number') {
        value = Number(value);
      }

      this.props.onChange(value, e);
    }
  }, {
    key: "onKeyDown",
    value: function onKeyDown(e) {
      var value = e.target.value;
      var maxLength = this.props.maxLength;
      var len = maxLength > 0 && value ? this.getValueLength(value) : 0;
      var opts = {}; // has enable trim and has input whitespace

      if (this.props.trim && e.keyCode === 32) {
        opts.beTrimed = true;
      } // has defined maxLength and has over max length and has not input backspace and delete


      if (maxLength > 0 && (len > maxLength + 1 || (len === maxLength || len === maxLength + 1) && e.keyCode !== 8 && e.keyCode !== 46)) {
        opts.overMaxLength = true;
      }

      this.props.onKeyDown(e, opts);
    }
  }, {
    key: "onFocus",
    value: function onFocus(e) {
      this.setState({
        focus: true
      });
      this.props.onFocus(e);
    }
  }, {
    key: "onBlur",
    value: function onBlur(e) {
      this.setState({
        focus: false
      });
      this.props.onBlur(e);
    }
  }, {
    key: "renderLength",
    value: function renderLength() {
      var _classNames;

      var _this$props = this.props,
          maxLength = _this$props.maxLength,
          hasLimitHint = _this$props.hasLimitHint,
          prefix = _this$props.prefix,
          rtl = _this$props.rtl;
      var len = maxLength > 0 && this.state.value ? this.getValueLength(this.state.value) : 0;
      var classesLenWrap = (0, _classnames["default"])((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "input-len"), true), _defineProperty(_classNames, "".concat(prefix, "error"), len > maxLength), _classNames));
      var content = rtl ? "".concat(maxLength, "/").concat(len) : "".concat(len, "/").concat(maxLength);
      return maxLength && hasLimitHint ? _react["default"].createElement("span", {
        className: classesLenWrap
      }, content) : null;
    }
  }, {
    key: "renderControl",
    value: function renderControl() {
      var lenWrap = this.renderLength();
      return lenWrap ? _react["default"].createElement("span", {
        className: "".concat(this.props.prefix, "input-control")
      }, lenWrap) : null;
    }
  }, {
    key: "getClass",
    value: function getClass() {
      var _classNames2;

      var _this$props2 = this.props,
          disabled = _this$props2.disabled,
          state = _this$props2.state,
          prefix = _this$props2.prefix;
      return (0, _classnames["default"])((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefix, "input"), true), _defineProperty(_classNames2, "".concat(prefix, "disabled"), !!disabled), _defineProperty(_classNames2, "".concat(prefix, "error"), state === 'error'), _defineProperty(_classNames2, "".concat(prefix, "success"), state === 'success'), _defineProperty(_classNames2, "".concat(prefix, "focus"), this.state.focus), _classNames2));
    }
  }, {
    key: "getProps",
    value: function getProps() {
      var _this$props3 = this.props,
          placeholder = _this$props3.placeholder,
          inputStyle = _this$props3.inputStyle,
          disabled = _this$props3.disabled,
          readOnly = _this$props3.readOnly,
          cutString = _this$props3.cutString,
          maxLength = _this$props3.maxLength,
          name = _this$props3.name;
      var props = {
        style: inputStyle,
        placeholder: placeholder,
        disabled: disabled,
        readOnly: readOnly,
        name: name,
        maxLength: cutString ? maxLength : undefined,
        value: this.state.value,
        onChange: this.onChange.bind(this),
        onBlur: this.onBlur.bind(this),
        onFocus: this.onFocus.bind(this)
      }; // fix accessibility：auto process status of aria disabled

      if (disabled) {
        props['aria-disabled'] = disabled;
      }

      return props;
    }
  }, {
    key: "getInputNode",
    value: function getInputNode() {
      return this.inputRef;
    }
  }, {
    key: "focus",
    value: function focus(start, end) {
      this.inputRef.focus();

      if (typeof start !== 'undefined') {
        this.inputRef.selectionStart = start;
      }

      if (typeof end !== 'undefined') {
        this.inputRef.selectionEnd = end;
      }
    }
  }]);

  return Base;
}(_react["default"].Component);

_defineProperty(Base, "propTypes", {
  prefix: _propTypes["default"].string,

  /**
   * 当前值
   */
  value: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]),

  /**
   * 初始化值
   */
  defaultValue: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]),

  /**
   * 发生改变的时候触发的回调
   * @param {String} value 数据
   * @param {Event} e DOM事件对象
   */
  onChange: _propTypes["default"].func,

  /**
   * 键盘按下的时候触发的回调
   * @param {Event} e DOM事件对象
   * @param {Object} opts 可扩展的附加信息：<br> - opts.overMaxLength: {Boolean} 已超出最大长度<br> - opts.beTrimed: {Boolean} 输入的空格被清理
   */
  onKeyDown: _propTypes["default"].func,

  /**
   * 禁用状态
   */
  disabled: _propTypes["default"].bool,

  /**
   * 最大长度
   */
  maxLength: _propTypes["default"].number,

  /**
   * 是否展现最大长度样式
   */
  hasLimitHint: _propTypes["default"].bool,

  /**
   * 当设置了maxLength时，是否截断超出字符串
   */
  cutString: _propTypes["default"].bool,

  /**
   * 只读
   */
  readOnly: _propTypes["default"].bool,

  /**
   * onChange返回会自动去除头尾空字符
   */
  trim: _propTypes["default"].bool,

  /**
   * 输入提示
   */
  placeholder: _propTypes["default"].string,

  /**
   * 获取焦点时候触发的回调
   * @param {Event} e DOM事件对象
   */
  onFocus: _propTypes["default"].func,

  /**
   * 失去焦点时候触发的回调
   * @param {Event} e DOM事件对象
   */
  onBlur: _propTypes["default"].func,

  /**
   * 自定义字符串计算长度方式
   * @param {String} value 数据
   * @returns {Number} 自定义长度
   */
  getValueLength: _propTypes["default"].func,
  inputStyle: _propTypes["default"].object,

  /**
   * 自定义class
   */
  className: _propTypes["default"].string,

  /**
   * 自定义内联样式
   */
  style: _propTypes["default"].object,

  /**
   * 原生type
   */
  htmlType: _propTypes["default"].string,

  /**
   * name
   */
  name: _propTypes["default"].string,
  rtl: _propTypes["default"].bool,
  state: _propTypes["default"].oneOf(['error', 'loading', 'success']),
  locale: _propTypes["default"].object
});

_defineProperty(Base, "defaultProps", {
  disabled: false,
  prefix: 'next-',
  maxLength: null,
  hasLimitHint: false,
  cutString: true,
  readOnly: false,
  trim: false,
  onFocus: _util.func.noop,
  onBlur: _util.func.noop,
  onChange: _util.func.noop,
  onKeyDown: _util.func.noop,
  getValueLength: _util.func.noop,
  locale: _zhCn["default"].Input
});

var _default = Base;
exports["default"] = _default;