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

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 InputText = (_temp = _class = function (_Component) {
  _inherits(InputText, _Component);

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

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

    _initialiseProps.call(_this);

    return _this;
  }
  // 点击容器

  // 自动扩充功能

  // 文本框事件

  // 点击清除


  // 过滤已经回调的属性


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

    var _props = this.props,
        type = _props.type,
        pre = _props.pre,
        onClick = _props.onClick,
        _props$inputAttribute = _props.inputAttribute,
        inputAttribute = _props$inputAttribute === undefined ? {} : _props$inputAttribute,
        autoFocus = _props.autoFocus,
        maxLength = _props.maxLength,
        max = _props.max,
        min = _props.min,
        digits = _props.digits,
        readOnly = _props.readOnly,
        disabled = _props.disabled,
        value = _props.value,
        defaultValue = _props.defaultValue,
        placeholder = _props.placeholder,
        onChange = _props.onChange,
        onBlur = _props.onBlur,
        onFocus = _props.onFocus,
        liconAttribute = _props.liconAttribute,
        licon = _props.licon,
        riconAttribute = _props.riconAttribute,
        ricon = _props.ricon,
        clear = _props.clear,
        _props$clearAttribute = _props.clearAttribute,
        clearAttribute = _props$clearAttribute === undefined ? {} : _props$clearAttribute,
        rcaption = _props.rcaption,
        othres = _objectWithoutProperties(_props, ['type', 'pre', 'onClick', 'inputAttribute', 'autoFocus', 'maxLength', 'max', 'min', 'digits', 'readOnly', 'disabled', 'value', 'defaultValue', 'placeholder', 'onChange', 'onBlur', 'onFocus', 'liconAttribute', 'licon', 'riconAttribute', 'ricon', 'clear', 'clearAttribute', 'rcaption']);
    // 剔除掉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;
        } }, othres, { className: 'input-text-box' + (othres.className ? ' ' + othres.className : ''), onClick: this.onClick }),
      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,
      rcaption && rcaption
    );
  };

  return InputText;
}(_react.Component), _class.defaultProps = {
  type: 'text',
  value: '',
  readOnly: false,
  disabled: false
}, _initialiseProps = function _initialiseProps() {
  var _this3 = this;

  this.onClick = function (e) {
    var _props2 = _this3.props,
        onClick = _props2.onClick,
        disabled = _props2.disabled,
        _props2$inputAttribut = _props2.inputAttribute,
        inputAttribute = _props2$inputAttribut === undefined ? {} : _props2$inputAttribut,
        _props2$liconAttribut = _props2.liconAttribute,
        liconAttribute = _props2$liconAttribut === undefined ? {} : _props2$liconAttribut,
        _props2$riconAttribut = _props2.riconAttribute,
        riconAttribute = _props2$riconAttribut === undefined ? {} : _props2$riconAttribut;

    if (disabled) return;
    var target = e.target;
    if (target.classList.contains('clearicon')) {
      _this3.onClear(e);
      e.stopPropagation();
      return;
    }
    if (target.classList.contains('licon') && liconAttribute.onClick) {
      liconAttribute.onClick(e, _this3.$input.value);
      e.stopPropagation();
      return;
    }
    if (target.classList.contains('ricon') && riconAttribute.onClick) {
      riconAttribute.onClick(e, _this3.$input.value);
      e.stopPropagation();
      return;
    }
    if (target.classList.contains('input-text') && inputAttribute.onClick) {
      inputAttribute.onClick(e, _this3.$input.value);
      e.stopPropagation();
      return;
    }
    if (onClick) {
      onClick(e, _this3.$input.value);
      e.stopPropagation();
    }
  };

  this.preAutoSize = function () {
    _this3.$input.style.height = _this3.$pre.clientHeight + 'px';
  };

  this.onChange = function (e) {
    var target = e.target;
    var value = target.value;
    var _props3 = _this3.props,
        pre = _props3.pre,
        type = _props3.type,
        onChange = _props3.onChange,
        maxLength = _props3.maxLength;
    // 自动扩充功能

    if (pre) {
      _this3.$pre.children[0].innerText = value;
      _this3.preAutoSize();
    }
    // 最大长度
    if (maxLength && value && value.length > maxLength) {
      value = value.substring(0, maxLength);
    }
    // 输入时只校验最大值、小数点、最大长度、返回错误
    if (type === 'number') {
      var _props4 = _this3.props,
          max = _props4.max,
          digits = _props4.digits,
          fail = _props4.fail;

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

  this.onBlur = function (e) {
    var target = e.target;
    var value = target.value;
    var _props5 = _this3.props,
        min = _props5.min,
        type = _props5.type,
        onBlur = _props5.onBlur,
        onChange = _props5.onChange;

    if (type === 'number') {
      // 失去焦点时只校验非空、最小值
      value = Math.Calc.correctNumber(value, { min: min });
      if (onChange) onChange(e, value);
    }
    if (onBlur) onBlur(e, value);
  };

  this.onFocus = function (e) {
    var target = e.target;
    var value = target.value;
    var _props6 = _this3.props,
        onFocus = _props6.onFocus,
        readOnly = _props6.readOnly;

    if (onFocus) {
      onFocus(e, value);
      e.stopPropagation();
    }
    if (readOnly) {
      target.blur();
    }
  };

  this.onClear = function (e) {
    _this3.$input.focus();
    // 赋值
    var _props7 = _this3.props,
        clear = _props7.clear,
        pre = _props7.pre,
        onChange = _props7.onChange;

    if (clear && typeof clear === 'function') clear(e, '');
    if (onChange) {
      onChange(e, '');
    }
    // 自动扩充功能
    if (pre) {
      _this3.preAutoSize();
    }
    e.stopPropagation();
  };

  this.getInputDOM = function () {
    var _props8 = _this3.props,
        max = _props8.max,
        min = _props8.min,
        pre = _props8.pre,
        type = _props8.type,
        autoFocus = _props8.autoFocus,
        _props8$inputAttribut = _props8.inputAttribute,
        inputAttribute = _props8$inputAttribut === undefined ? {} : _props8$inputAttribut,
        maxLength = _props8.maxLength,
        value = _props8.value,
        placeholder = _props8.placeholder,
        readOnly = _props8.readOnly,
        disabled = _props8.disabled;
    // 剔除掉onClick事件, 因为在容器onClick已经回调了

    inputAttribute = _this3.filterProps(inputAttribute);
    // pre类型
    if (pre) {
      // pre的左右padding
      var preLeft = 0;
      var preRight = 0;
      if (inputAttribute.style) {
        if (inputAttribute.style.padding) {
          var paddingValues = inputAttribute.style.padding.split(' ');
          if (paddingValues.length === 1) {
            preLeft = paddingValues[0];
            preRight = paddingValues[0];
          } else if (paddingValues.length === 2) {
            preLeft = paddingValues[1];
            preRight = paddingValues[1];
          } else if (paddingValues.length === 4) {
            preLeft = paddingValues[1];
            preRight = paddingValues[3];
          }
        } else if (inputAttribute.style.paddingLeft || inputAttribute.style.paddingRight) {
          preLeft = inputAttribute.style.paddingLeft || '0';
          preRight = inputAttribute.style.paddingRight || '0';
        }
      }
      return _react2.default.createElement(
        'div',
        _extends({}, inputAttribute, { className: 'input-pre-box' + (inputAttribute.className ? ' ' + inputAttribute.className : '') }),
        _react2.default.createElement('textarea', { ref: function ref(el) {
            _this3.$input = el;
          }, autoFocus: autoFocus, value: value, maxLength: maxLength, readOnly: readOnly, disabled: disabled, className: 'input-pre', placeholder: placeholder, onChange: _this3.onChange, onBlur: _this3.onBlur, onFocus: _this3.onFocus }),
        _react2.default.createElement(
          'pre',
          { ref: function ref(el) {
              _this3.$pre = el;
            }, style: { left: preLeft, right: preRight } },
          _react2.default.createElement(
            'span',
            null,
            value
          )
        )
      );
    }
    // textarea类型
    if (type === 'textarea') {
      // 如果值绑定属性,则只有通过父组件的prop来改变值
      return _react2.default.createElement('textarea', _extends({ ref: function ref(el) {
          _this3.$input = el;
        } }, inputAttribute, { autoFocus: autoFocus, value: value, maxLength: maxLength, readOnly: readOnly, disabled: disabled, placeholder: placeholder, onChange: _this3.onChange, onBlur: _this3.onBlur, onFocus: _this3.onFocus, className: 'input-area' + (inputAttribute.className ? ' ' + inputAttribute.className : '') }));
    }
    // 其它类型
    return _react2.default.createElement('input', _extends({ ref: function ref(el) {
        _this3.$input = el;
      } }, inputAttribute, { max: max, min: min, autoFocus: autoFocus, type: type, value: value, maxLength: maxLength, readOnly: readOnly, disabled: disabled, placeholder: placeholder, onChange: _this3.onChange, onBlur: _this3.onBlur, onFocus: _this3.onFocus, className: 'input-text' + (inputAttribute.className ? ' ' + inputAttribute.className : '') }));
  };

  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 = InputText;
InputText.propTypes = process.env.NODE_ENV !== "production" ? {
  type: _propTypes2.default.string, // 类型: text | number | tel | password
  pre: _propTypes2.default.bool, // 自动扩充功能
  // 容器
  onClick: _propTypes2.default.func,
  // 文本框
  inputAttribute: _propTypes2.default.object,
  autoFocus: _propTypes2.default.bool,
  maxLength: _propTypes2.default.oneOfType([_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]),
  digits: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.string, _propTypes2.default.number]),
  readOnly: _propTypes2.default.bool,
  disabled: _propTypes2.default.bool,

  value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]),
  defaultValue: _propTypes2.default.oneOfType([// 为了过滤此属性,防止开发者使用此属性而出错
  _propTypes2.default.string, _propTypes2.default.number]),
  placeholder: _propTypes2.default.string,
  // 文本框事件
  onChange: _propTypes2.default.func,
  onBlur: _propTypes2.default.func,
  onFocus: _propTypes2.default.func,
  // 左右图标
  liconAttribute: _propTypes2.default.object,
  licon: _propTypes2.default.node,
  riconAttribute: _propTypes2.default.object,
  ricon: _propTypes2.default.node,
  // 清除按键
  clear: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.func]),
  clearAttribute: _propTypes2.default.object,
  // 右侧内容
  rcaption: _propTypes2.default.node,
  fail: _propTypes2.default.func
} : {};
module.exports = exports['default'];