"use strict";

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

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

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

var _rcInputNumber = _interopRequireDefault(require("rc-input-number"));

var _style = require("./style");

var _Enums = require("./Enums");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _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); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); 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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

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

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

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

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

function getInputClassName(prefixCls, size, disabled, direction) {
  var _classNames;

  return (0, _classnames["default"])(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-sm"), size === "small"), _defineProperty(_classNames, "".concat(prefixCls, "-lg"), size === "large"), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === "rtl"), _classNames));
}
/**
 * precision 这个参数和业务逻辑耦合度就很大了
 * 这个参数现在默认就是可以取整，保留 props.precision 位小数r
 */


var InputNumber = /*#__PURE__*/function (_Component) {
  _inherits(InputNumber, _Component);

  var _super = _createSuper(InputNumber);

  function InputNumber(props) {
    var _this;

    _classCallCheck(this, InputNumber);

    _this = _super.call(this, props); // state 里的value不一定用

    _initialiseProps.call(_assertThisInitialized(_this));

    var value = _typeof(props.value) === undefined ? props.defaultValue : props.value;
    _this.state = {
      value: value,
      focused: false,
      rawInput: 0,
      precision: 15
    };
    _this.rcInputNumberRef = /*#__PURE__*/(0, _react.createRef)();
    return _this;
  }

  _createClass(InputNumber, [{
    key: "componentWillMount",
    value: function componentWillMount() {
      this.precisionHandler(this.props.value);
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(newProps) {
      if (newProps.value !== this.props.value) {
        this.precisionHandler(newProps.value);
        this.setState({
          value: newProps.value
        });
      }
    }
  }, {
    key: "setValue",
    value: function setValue(value, callback) {
      if (this.props.value === undefined) {
        this.setState({
          value: value
        }, callback);
      }
    }
  }, {
    key: "render",
    value: function render() {
      // HOC 转发Ref,取到RawInput倒不如直接在这一层把RawInput拿出来
      var _this$props = this.props,
          className = _this$props.className,
          style = _this$props.style,
          placeholder = _this$props.placeholder,
          max = _this$props.max,
          min = _this$props.min,
          defaultValue = _this$props.defaultValue,
          value = _this$props.value;
      return /*#__PURE__*/_react["default"].createElement(_style.InputContent, {
        className: className,
        style: style
      }, this.renderPrefix(), /*#__PURE__*/_react["default"].createElement(_style.InputInner, {
        className: (0, _classnames["default"])("tita-input-inner")
      }, /*#__PURE__*/_react["default"].createElement(_rcInputNumber["default"], {
        required: true,
        ref: this.rcInputNumberRef,
        value: value,
        defaultValue: defaultValue,
        placeholder: placeholder,
        max: max,
        min: min,
        precision: this.precisionHandler(value),
        onChange: this.handleChange,
        onFocus: this.onFocus,
        onBlur: this.onBlur
      })), this.renderSuffix());
    }
  }]);

  return InputNumber;
}(_react.Component);

exports["default"] = InputNumber;
InputNumber.defaultProps = {
  max: 9999,
  min: 0
};

var _initialiseProps = function _initialiseProps() {
  var _this2 = this;

  this.onFocus = function (e) {
    var onFocus = _this2.props.onFocus;

    _this2.setState({
      focused: true
    });

    if (onFocus) {
      onFocus(e);
    }
  };

  this.onBlur = function (e) {
    // const { value } = this.state;
    // this.handlePrecision(value);
    var onBlur = _this2.props.onBlur;

    _this2.setState({
      focused: false
    });

    if (onBlur) {
      onBlur(e);
    }
  };

  this.handleChange = function (value) {
    _this2.setValue(value);

    var node = _this2.rcInputNumberRef.current;

    if (node) {
      _this2.setState({
        rawInput: node.rawInput
      }, function () {
        return _this2.precisionHandler(node.rawInput);
      });
    }

    _this2.props.onChange && _this2.props.onChange(value);
    _this2.props.onRawInputChange && _this2.props.onRawInputChange(node.rawInput);
  };

  this.handleKeyDown = function (e) {
    var _this2$props = _this2.props,
        onPressEnter = _this2$props.onPressEnter,
        onKeyDown = _this2$props.onKeyDown;

    if (e.keyCode === _Enums.KeyCode.ENTER && onPressEnter) {
      onPressEnter(e);
    }

    if (onKeyDown) {
      onKeyDown(e);
    }
  };

  this.renderPrefix = function () {
    return (// 用TuIcon组件
      _this2.props.prefixIcon
    );
  };

  this.renderSuffix = function () {
    return _this2.props.suffixIcon && /*#__PURE__*/_react["default"].createElement(_style.SuffixIcon, null, _this2.props.suffixIcon);
  };

  this.precisionHandler = function (value) {
    // 如果是传值，就 设置精度为 1 否则设置为 15
    if (Number.isSafeInteger(Number(value))) {
      return 0;
    }

    return 15;
  };
};