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

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

import React, { forwardRef, useRef, useImperativeHandle } from 'react';

var InputText = forwardRef(function (_ref, ref) {
  var _ref$type = _ref.type,
      type = _ref$type === undefined ? 'text' : _ref$type,
      pre = _ref.pre,
      onClick = _ref.onClick,
      _ref$inputAttribute = _ref.inputAttribute,
      inputAttribute = _ref$inputAttribute === undefined ? {} : _ref$inputAttribute,
      autoFocus = _ref.autoFocus,
      maxLength = _ref.maxLength,
      max = _ref.max,
      min = _ref.min,
      digits = _ref.digits,
      _ref$readOnly = _ref.readOnly,
      readOnly = _ref$readOnly === undefined ? false : _ref$readOnly,
      _ref$disabled = _ref.disabled,
      disabled = _ref$disabled === undefined ? false : _ref$disabled,
      _ref$value = _ref.value,
      value = _ref$value === undefined ? '' : _ref$value,
      _ref$defaultValue = _ref.defaultValue,
      defaultValue = _ref$defaultValue === undefined ? '' : _ref$defaultValue,
      _ref$placeholder = _ref.placeholder,
      placeholder = _ref$placeholder === undefined ? '' : _ref$placeholder,
      onChange = _ref.onChange,
      onBlur = _ref.onBlur,
      onFocus = _ref.onFocus,
      liconAttribute = _ref.liconAttribute,
      licon = _ref.licon,
      riconAttribute = _ref.riconAttribute,
      ricon = _ref.ricon,
      clear = _ref.clear,
      _ref$clearAttribute = _ref.clearAttribute,
      clearAttribute = _ref$clearAttribute === undefined ? {} : _ref$clearAttribute,
      rcaption = _ref.rcaption,
      children = _ref.children,
      fail = _ref.fail,
      others = _objectWithoutProperties(_ref, ['type', 'pre', 'onClick', 'inputAttribute', 'autoFocus', 'maxLength', 'max', 'min', 'digits', 'readOnly', 'disabled', 'value', 'defaultValue', 'placeholder', 'onChange', 'onBlur', 'onFocus', 'liconAttribute', 'licon', 'riconAttribute', 'ricon', 'clear', 'clearAttribute', 'rcaption', 'children', 'fail']);

  var refEl = useRef(null);
  useImperativeHandle(ref, function () {
    return refEl.current;
  });
  // 点击容器
  function click(e) {
    var elInput = refEl.current.querySelector('.input-text') || refEl.current.querySelector('.input-pre');
    if (!elInput) return;
    if (disabled) return;
    var target = e.target;
    if (target.classList.contains('clearicon')) {
      onClear(e);
      e.stopPropagation();
      return;
    }
    if (target.classList.contains('licon') && liconAttribute && liconAttribute.onClick) {
      liconAttribute.onClick(e, elInput.value);
      e.stopPropagation();
      return;
    }
    if (target.classList.contains('ricon') && riconAttribute && riconAttribute.onClick) {
      riconAttribute.onClick(e, elInput.value);
      e.stopPropagation();
      return;
    }
    if (target.classList.contains('input-text') && inputAttribute.onClick) {
      inputAttribute.onClick(e, elInput.value);
      e.stopPropagation();
      return;
    }
    if (onClick) {
      onClick(e, elInput.value);
      e.stopPropagation();
    }
  }
  // 自动扩充功能
  function preAutoSize(target, preTarget) {
    target.style.height = preTarget.clientHeight + 'px';
  }
  // 文本框事件
  function change(e) {
    var target = e.target;
    var val = target.value;
    // 自动扩充功能
    if (pre) {
      var elPre = target.nextElementSibling;
      if (elPre.tagName === 'PRE') {
        elPre.children[0].innerText = val;
        preAutoSize(target, elPre);
      }
    }
    // 最大长度
    if (maxLength && val && val.length > maxLength) {
      val = val.substring(0, maxLength);
    }
    // 输入时只校验最大值、小数点、最大长度、返回错误
    if (type === 'number') {
      val = Math.Calc.correctNumber(e.target.value, { max: max, digits: digits, maxLength: maxLength, fail: fail });
    }
    // onChange
    if (onChange) onChange(e, val);
  }
  function blur(e) {
    var target = e.target;
    var value = target.value;
    if (type === 'number') {
      // 失去焦点时只校验非空、最小值
      value = Math.Calc.correctNumber(value, { min: min });
      if (onChange) onChange(e, value);
    }
    if (onBlur) onBlur(e, value);
  }
  function focus(e) {
    var target = e.target;
    var value = target.value;
    if (onFocus) {
      onFocus(e, value);
      e.stopPropagation();
    }
    if (readOnly) {
      target.blur();
    }
  }
  // 点击清除
  function onClear(e) {
    var elInput = refEl.current.querySelector('.input-text') || refEl.current.querySelector('.input-pre');
    if (!elInput) return;
    elInput.focus();
    // 赋值
    if (clear && typeof clear === 'function') clear(e, '');
    if (onChange) {
      onChange(e, '');
    }
    // 自动扩充功能
    if (pre) {
      preAutoSize();
    }
    e.stopPropagation();
  }
  function getInputDOM() {
    // 剔除掉onClick事件, 因为在容器onClick已经回调了
    inputAttribute = 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 React.createElement(
        'div',
        _extends({}, inputAttribute, { className: 'input-pre-box' + (inputAttribute.className ? ' ' + inputAttribute.className : '') }),
        React.createElement('textarea', { autoFocus: autoFocus, value: value, maxLength: maxLength, readOnly: readOnly, disabled: disabled, className: 'input-pre', placeholder: placeholder, onChange: change, onBlur: blur, onFocus: focus }),
        React.createElement(
          'pre',
          { style: { left: preLeft, right: preRight } },
          React.createElement(
            'span',
            null,
            value
          )
        )
      );
    }
    // textarea类型
    if (type === 'textarea') {
      // 如果值绑定属性,则只有通过父组件的prop来改变值
      return React.createElement('textarea', _extends({}, inputAttribute, { autoFocus: autoFocus, value: value, maxLength: maxLength, readOnly: readOnly, disabled: disabled, placeholder: placeholder, onChange: change, onBlur: blur, onFocus: focus, className: 'input-area' + (inputAttribute.className ? ' ' + inputAttribute.className : '') }));
    }
    // 其它类型
    return React.createElement('input', _extends({}, inputAttribute, { max: max, min: min, autoFocus: autoFocus, type: type, value: value, maxLength: maxLength, readOnly: readOnly, disabled: disabled, placeholder: placeholder, onChange: change, onBlur: blur, onFocus: focus, className: 'input-text' + (inputAttribute.className ? ' ' + inputAttribute.className : '') }));
  }

  // 过滤已经回调的属性
  function filterProps(props) {
    if (!props) return {};

    var onClick = props.onClick,
        otherProps = _objectWithoutProperties(props, ['onClick']);

    return _extends({}, otherProps);
  }

  // 剔除掉onClick事件, 因为在容器onClick已经回调了
  var otherLiconAttribute = filterProps(liconAttribute);
  var otherRiconAttribute = filterProps(riconAttribute);
  var otherClearAttribute = filterProps(clearAttribute);

  return React.createElement(
    'div',
    _extends({ ref: refEl }, others, { className: 'input-text-box' + (others.className ? ' ' + others.className : ''), onClick: click }),
    licon && licon,
    liconAttribute && React.createElement('i', _extends({}, otherLiconAttribute, { className: 'licon icon' + (otherLiconAttribute.className ? ' ' + otherLiconAttribute.className : '') })),
    getInputDOM(),
    children && children,
    clear && value && !readOnly && !disabled && React.createElement('i', _extends({}, otherClearAttribute, { className: 'icon clearicon' + (otherClearAttribute.className ? ' ' + otherClearAttribute.className : ' ricon close-icon-clear size18') })),
    riconAttribute && React.createElement('i', _extends({}, otherRiconAttribute, { className: 'ricon icon' + (otherRiconAttribute.className ? ' ' + otherRiconAttribute.className : '') })),
    ricon && ricon,
    rcaption && rcaption
  );
});

export default InputText;