'use strict';

exports.__esModule = true;
exports.default = undefined;

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

var _class, _temp, _initialiseProps; // require PrototypeMath.js, 用于解决加减法精度丢失的问题


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var NumBox = (_temp = _class = function (_Component) {
  _inherits(NumBox, _Component);

  function NumBox(props) {
    _classCallCheck(this, NumBox);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

    _initialiseProps.call(_this);

    return _this;
  }

  NumBox.prototype.componentDidMount = function componentDidMount() {
    if (this.props.autoFocus) {
      this.autoFocus();
    }
  };
  // 失去焦点

  // 获取焦点

  // 点击加减号清除时获取焦点

  // 修改值

  // 点击文本框, 逢0清空

  // 点击减

  // 点击加

  // 点击容器

  // 点击清除

  // render

  // 过滤已经回调的属性


  NumBox.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        disabled = _props.disabled,
        _props$plusAttribute = _props.plusAttribute,
        plusAttribute = _props$plusAttribute === undefined ? {} : _props$plusAttribute,
        _props$minusAttribute = _props.minusAttribute,
        minusAttribute = _props$minusAttribute === undefined ? {} : _props$minusAttribute,
        inputAttribute = _props.inputAttribute,
        value = _props.value,
        digits = _props.digits,
        max = _props.max,
        min = _props.min,
        placeholder = _props.placeholder,
        maxLength = _props.maxLength,
        readOnly = _props.readOnly,
        required = _props.required,
        autoFocus = _props.autoFocus,
        autoSelect = _props.autoSelect,
        licon = _props.licon,
        liconAttribute = _props.liconAttribute,
        ricon = _props.ricon,
        riconAttribute = _props.riconAttribute,
        clear = _props.clear,
        clearAttribute = _props.clearAttribute,
        onClick = _props.onClick,
        onChange = _props.onChange,
        onBlur = _props.onBlur,
        onFocus = _props.onFocus,
        fail = _props.fail,
        others = _objectWithoutProperties(_props, ['disabled', 'plusAttribute', 'minusAttribute', 'inputAttribute', 'value', 'digits', 'max', 'min', 'placeholder', 'maxLength', 'readOnly', 'required', 'autoFocus', 'autoSelect', 'licon', 'liconAttribute', 'ricon', 'riconAttribute', 'clear', 'clearAttribute', 'onClick', 'onChange', 'onBlur', 'onFocus', 'fail']);
    // 剔除掉onClick事件, 因为在容器onClick已经回调了


    liconAttribute = this.filterProps(liconAttribute);
    riconAttribute = this.filterProps(riconAttribute);
    clearAttribute = this.filterProps(clearAttribute);
    return _react2.default.createElement(
      'div',
      _extends({ ref: function ref(el) {
          _this2.$el = el;
        } }, others, { disabled: min >= max || disabled, className: 'numbox' + (others.className ? ' ' + others.className : ''), onClick: this.onClick }),
      _react2.default.createElement('input', _extends({
        ref: function ref(el) {
          _this2.$minus = el;
        },
        value: '-',
        disabled: !isNaN(min) ? min - value >= 0 : false
      }, plusAttribute, {
        type: 'button',
        className: 'numbox-button numbox-button-minus' + (plusAttribute.className ? ' ' + plusAttribute.className : '')
      })),
      licon && licon,
      liconAttribute && _react2.default.createElement('i', _extends({}, liconAttribute, { className: 'licon icon' + (liconAttribute.className ? ' ' + liconAttribute.className : '') })),
      this.getInputDOM(),
      clear && value && !readOnly && !disabled && _react2.default.createElement('i', _extends({}, clearAttribute, { className: 'icon clearicon' + (clearAttribute.className ? ' ' + clearAttribute.className : 'ricon close-icon-clear size18') })),
      riconAttribute && _react2.default.createElement('i', _extends({}, riconAttribute, { className: 'ricon icon' + (riconAttribute.className ? ' ' + riconAttribute.className : '') })),
      ricon && ricon,
      _react2.default.createElement('input', _extends({
        ref: function ref(el) {
          _this2.$plus = el;
        },
        value: '+',
        disabled: !isNaN(max) ? max - value <= 0 : false
      }, minusAttribute, {
        type: 'button',
        className: 'numbox-button numbox-button-plus' + (minusAttribute.className ? ' ' + minusAttribute.className : '')
      }))
    );
  };

  return NumBox;
}(_react.Component), _class.defaultProps = {
  maxLength: '16'
}, _initialiseProps = function _initialiseProps() {
  var _this3 = this;

  this.onBlur = function (e) {
    var _props2 = _this3.props,
        readOnly = _props2.readOnly,
        disabled = _props2.disabled,
        required = _props2.required,
        value = _props2.value,
        min = _props2.min,
        onChange = _props2.onChange,
        onBlur = _props2.onBlur;

    if (readOnly || disabled) {
      return;
    }
    // 如果value和框内值不等, 设置为相等
    if (_this3.$input && _this3.$input.value !== value) {
      _this3.$input.value = value;
    }
    // 失去焦点时只校验非空、最小值
    var val = Math.Calc.correctNumber(value, { required: required, min: min });
    if (onChange && '' + val !== '' + value) onChange(e, val);
    if (onBlur) onBlur(e, val);
  };

  this.onFocus = function (e) {
    var _props3 = _this3.props,
        readOnly = _props3.readOnly,
        disabled = _props3.disabled,
        onFocus = _props3.onFocus;

    if (readOnly || disabled) {
      e.target.blur();
      return;
    }
    if (onFocus) onFocus(e, _this3.props.value);
  };

  this.autoFocus = function () {
    if (_this3.props.disabled || _this3.props.readOnly || !_this3.$input) return;
    _this3.$input.focus();
    if (_this3.props.autoSelect) _this3.$input.select();
    // 修复兼容ios12的bug, 与全局的回弹并不冲突, 这里主要解决点击加减号时获取焦点, 不回弹的问题
    var iosExp = navigator.userAgent.toLowerCase().match(/cpu iphone os (.*?) like mac os/);
    if (iosExp && iosExp[1] && iosExp[1] > '12') {
      // 兼容输入法把页面顶上去, 不回弹的问题
      if (window.inputToggleTimeout) {
        window.clearTimeout(window.inputToggleTimeout);
      }
      if (!_this3.$input.getAttribute('ios-bug-blur')) {
        _this3.$input.setAttribute('ios-bug-blur', '1');
        _this3.$input.addEventListener('blur', function () {
          window.inputToggleTimeout = window.setTimeout(function () {
            document.getElementById('root').scrollIntoView();
          }, 100);
        }, false);
      }
    }
  };

  this.onChange = function (e) {
    if (e.target.validity.badInput) {
      e.target.value = '';
    }
    // 输入时只校验最大值、小数点、最大长度、返回错误
    var _props4 = _this3.props,
        max = _props4.max,
        digits = _props4.digits,
        maxLength = _props4.maxLength,
        fail = _props4.fail;

    var value = Math.Calc.correctNumber(e.target.value, { max: max, digits: digits, maxLength: maxLength, fail: fail });
    if (_this3.props.onChange) _this3.props.onChange(e, value);
  };

  this.onClickInput = function (e) {
    var _props$inputAttribute = _this3.props.inputAttribute,
        inputAttribute = _props$inputAttribute === undefined ? {} : _props$inputAttribute;

    var value = e.target.value;
    if (value - 0 === 0) {
      e.target.value = '';
    }
    if (inputAttribute.onClick) inputAttribute.onClick(e, value);
  };

  this.onClickMinus = function (e) {
    var _props5 = _this3.props,
        onChange = _props5.onChange,
        _props5$minusAttribut = _props5.minusAttribute,
        minusAttribute = _props5$minusAttribut === undefined ? {} : _props5$minusAttribut;

    var value = Math.Calc.correctNumber(Math.Calc.subtract(_this3.$input.value, 1), _this3.props);
    // Callback
    if (onChange) onChange(e, value);
    if (minusAttribute.onClick) minusAttribute.onClick(e, value);
    _this3.autoFocus();
  };

  this.onClickPlus = function (e) {
    var _props6 = _this3.props,
        onChange = _props6.onChange,
        _props6$plusAttribute = _props6.plusAttribute,
        plusAttribute = _props6$plusAttribute === undefined ? {} : _props6$plusAttribute;

    var value = Math.Calc.correctNumber(Math.Calc.add(_this3.$input.value, 1), _this3.props);
    // Callback
    if (onChange) onChange(e, value);
    if (plusAttribute.onClick) plusAttribute.onClick(e, value);
    _this3.autoFocus();
  };

  this.onClick = function (e) {
    e.stopPropagation();
    var _props7 = _this3.props,
        clear = _props7.clear,
        onClick = _props7.onClick,
        _props7$liconAttribut = _props7.liconAttribute,
        liconAttribute = _props7$liconAttribut === undefined ? {} : _props7$liconAttribut,
        _props7$riconAttribut = _props7.riconAttribute,
        riconAttribute = _props7$riconAttribut === undefined ? {} : _props7$riconAttribut;

    if (_this3.props.disabled) return;
    var target = e.target;
    if (clear && target.classList.contains('clearicon')) {
      _this3.onClear(e);
    }
    if (liconAttribute.onClick && target.classList.contains('licon')) {
      liconAttribute.onClick(e, _this3.$input.value);
      return;
    }
    if (riconAttribute.onClick && target.classList.contains('ricon')) {
      riconAttribute.onClick(e, _this3.$input.value);
      return;
    }
    if (target.classList.contains('numbox-input')) {
      _this3.onClickInput(e);
      return;
    }
    if (target.classList.contains('numbox-button-plus')) {
      _this3.onClickPlus(e);
      return;
    }
    if (target.classList.contains('numbox-button-minus')) {
      _this3.onClickMinus(e);
      return;
    }
    if (onClick) onClick(e, _this3.$input.value);
  };

  this.onClear = function (e) {
    _this3.autoFocus();
    // 赋值
    if (_this3.props.clear && typeof _this3.props.clear === 'function') _this3.props.clear(e, '');
    if (_this3.props.onChange) {
      _this3.props.onChange(e, '');
    }
    e.stopPropagation();
  };

  this.getInputDOM = function () {
    var _props8 = _this3.props,
        disabled = _props8.disabled,
        readOnly = _props8.readOnly,
        _props8$inputAttribut = _props8.inputAttribute,
        inputAttribute = _props8$inputAttribut === undefined ? {} : _props8$inputAttribut,
        value = _props8.value,
        placeholder = _props8.placeholder,
        max = _props8.max,
        min = _props8.min;

    return _react2.default.createElement('input', _extends({
      ref: function ref(el) {
        _this3.$input = el;
      },
      type: 'number'
    }, inputAttribute, {
      className: 'numbox-input' + (inputAttribute.className ? ' ' + inputAttribute.className : ''),
      value: value,
      min: min,
      max: max,
      disabled: disabled,
      readOnly: readOnly,
      placeholder: placeholder,
      onChange: _this3.onChange,
      onFocus: _this3.onFocus,
      onBlur: _this3.onBlur
    }));
  };

  this.filterProps = function (props) {
    if (!props) return props;
    var propsed = {};
    for (var n in props) {
      if (n !== 'onClick') {
        propsed[n] = props[n];
      }
    }
    return propsed;
  };
}, _temp);
exports.default = NumBox;
NumBox.propTypes = process.env.NODE_ENV !== "production" ? {
  // 容器
  disabled: _propTypes2.default.bool,
  // 加减号
  plusAttribute: _propTypes2.default.object,
  minusAttribute: _propTypes2.default.object,
  // 文本框
  inputAttribute: _propTypes2.default.object,
  value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  digits: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.string, _propTypes2.default.number]),
  max: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  min: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  placeholder: _propTypes2.default.string,
  maxLength: _propTypes2.default.string,
  readOnly: _propTypes2.default.bool,
  required: _propTypes2.default.bool, // 设置非空时, 会自动补一个合法值
  // 自动获取焦点
  autoFocus: _propTypes2.default.bool, // 渲染时自动获取焦点
  autoSelect: _propTypes2.default.bool, // 渲染时自动选中
  // 左右图标
  licon: _propTypes2.default.node,
  liconAttribute: _propTypes2.default.object,
  ricon: _propTypes2.default.node,
  riconAttribute: _propTypes2.default.object,
  // 清除按键
  clear: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.func]),
  clearAttribute: _propTypes2.default.object,
  // events
  onClick: _propTypes2.default.func,
  onChange: _propTypes2.default.func,
  onBlur: _propTypes2.default.func,
  onFocus: _propTypes2.default.func,
  fail: _propTypes2.default.func
} : {};
module.exports = exports['default'];