"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

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

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

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

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

var _omit = _interopRequireDefault(require("lodash/omit"));

var _inputNumber = _interopRequireDefault(require("../rc-components/input-number"));

var _Input = _interopRequireDefault(require("../input/Input"));

var _enum = require("../_util/enum");

var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));

var _excluded = ["allowThousandth", "className", "size", "prefixCls"];

function formatNumber(value) {
  var vs = String(value).split('.');
  var v = vs[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  return vs.length > 1 ? "".concat(v, ".").concat(vs[1]) : v;
}

function parseNumber(value) {
  if (value.indexOf(',') !== -1) {
    return value.replace(/,/g, '');
  }

  return value;
}

function renderInput(props) {
  return /*#__PURE__*/_react["default"].createElement(_Input["default"], (0, _extends2["default"])({}, props));
}

var InputNumber = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(InputNumber, _Component);

  var _super = (0, _createSuper2["default"])(InputNumber);

  function InputNumber() {
    var _this;

    (0, _classCallCheck2["default"])(this, InputNumber);
    _this = _super.apply(this, arguments);

    _this.numberFormatter = function (value) {
      var formatter = _this.props.formatter;
      var v = formatNumber(value);
      return formatter ? formatter(v) : v;
    };

    _this.numberParser = function (value) {
      var parser = _this.props.parser;
      var v = parseNumber(value);
      return parser ? parser(v) : v;
    };

    return _this;
  }

  (0, _createClass2["default"])(InputNumber, [{
    key: "render",
    value: function render() {
      var _classNames,
          _this2 = this;

      var _this$props = this.props,
          _this$props$allowThou = _this$props.allowThousandth,
          allowThousandth = _this$props$allowThou === void 0 ? false : _this$props$allowThou,
          className = _this$props.className,
          size = _this$props.size,
          customizePrefixCls = _this$props.prefixCls,
          others = (0, _objectWithoutProperties2["default"])(_this$props, _excluded);
      var getPrefixCls = this.context.getPrefixCls;
      var prefixCls = getPrefixCls('input-number', customizePrefixCls);
      var inputNumberClass = (0, _classnames["default"])((_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-lg"), size === _enum.Size.large), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-sm"), size === _enum.Size.small), _classNames), className);
      var otherProp = others;

      if (allowThousandth) {
        otherProp = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, otherProp), {}, {
          formatter: this.numberFormatter,
          parser: this.numberParser
        });
      }

      return /*#__PURE__*/_react["default"].createElement(_inputNumber["default"], (0, _extends2["default"])({
        ref: function ref(c) {
          return _this2.inputNumberRef = c;
        },
        className: inputNumberClass,
        prefixCls: prefixCls,
        renderInput: renderInput
      }, (0, _omit["default"])(otherProp, ['form'])));
    }
  }, {
    key: "focus",
    value: function focus() {
      this.inputNumberRef.focus();
    }
  }, {
    key: "blur",
    value: function blur() {
      this.inputNumberRef.blur();
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _ConfigContext["default"];
    }
  }]);
  return InputNumber;
}(_react.Component);

exports["default"] = InputNumber;
InputNumber.displayName = 'InputNumber';
InputNumber.defaultProps = {
  step: 1
};
//# sourceMappingURL=index.js.map
