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, useEffect, useState } from 'react';

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

  var refEl = useRef(null);
  var refElInput = useRef(null);
  var refElPre = useRef(null);
  useImperativeHandle(ref, function () {
    return refEl.current;
  });

  useEffect(function () {
    var val = refElInput.current.value;
    // 更新清除按钮
    updateState(val);
    // 更新高度
    if (pre) {
      preAutoSize(val);
    }
    if (autoFocus) {
      focus();
    }
  }, []); // eslint-disable-line

  useEffect(function () {
    if ((defaultValue || defaultValue === '') && refElInput.current) {
      if (refElInput.current.value !== defaultValue) {
        console.log(defaultValue);
        refElInput.current.value = defaultValue;
      }
    }
  }, [defaultValue]);

  var _useState = useState(false),
      clearShow = _useState[0],
      setClearShow = _useState[1];

  // 更新清除按钮


  function updateState(val) {
    if (typeof val === 'number') {
      val = String(val);
    }
    if (val) {
      setClearShow(true);
    } else {
      setClearShow(false);
    }
  }
  // 自动扩充功能
  function preAutoSize(val) {
    refElPre.current.children[0].innerText = val;
    refElInput.current.style.height = refElPre.current.clientHeight + 'px';
  }
  // 点击加减号清除时获取焦点
  function focus() {
    if (disabled || readOnly || !refElInput.current) return;
    refElInput.current.focus();
    if (autoSelect) refElInput.current.select();
  }
  // 修改值回调
  function change(e, val) {
    if (defaultValue || defaultValue === '') {
      e.target.value = val;
    }
    // 更新清空按钮
    updateState(val);
    // 更新自动扩充功能
    if (pre) {
      preAutoSize(val);
    }
    if (onChange) onChange(e, val);
  }

  // 点击容器
  function clickHandler(e) {
    var elInput = refElInput.current;
    if (!elInput) return;
    if (disabled) return;
    var target = e.target;
    if (target.classList.contains('clearicon')) {
      clickClear(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 correctNumber(originVal) {
    var val = originVal;
    if (typeof val === 'number') val = String(val);
    // 最大长度
    if (maxLength && val && val.length > maxLength) {
      val = val.substring(0, maxLength);
    }
    // 小数位截取
    if (!isNaN(digits)) {
      if (val.indexOf('.') !== -1) {
        val = val.substring(0, val.indexOf('.') + Number(digits) + 1);
      }
    }
    // 输入时只校验最大值、小数点、最大长度、返回错误
    if (!isNaN(max) && !isNaN(val)) {
      if (val > max) val = max;
    }
    if (!isNaN(min) && !isNaN(val)) {
      if (val < min) val = min;
    }
    return val;
  }
  // 文本框事件
  function changeHandler(e) {
    var target = e.target;
    var val = target.value;
    if (val !== '') {
      val = correctNumber(target.value);
    }
    // onChange
    change(e, val);
  }
  // 点击清除
  function clickClear(e) {
    focus();
    var event = { target: refElInput.current };
    if (clear && typeof clear === 'function') clear(event, '');
    // Callback
    change(event, '');
    e.stopPropagation();
  }
  // render
  function getInputDOM() {
    // 剔除掉onClick事件, 因为在容器onClick已经回调了
    var otherInputAttribute = filterProps(inputAttribute);
    // pre类型
    if (pre) {
      // pre的左右padding
      var preLeft = 0;
      var preRight = 0;
      if (otherInputAttribute.style) {
        if (otherInputAttribute.style.padding) {
          var paddingValues = otherInputAttribute.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 (otherInputAttribute.style.paddingLeft || otherInputAttribute.style.paddingRight) {
          preLeft = otherInputAttribute.style.paddingLeft || '0';
          preRight = otherInputAttribute.style.paddingRight || '0';
        }
      }
      return React.createElement(
        'div',
        _extends({}, otherInputAttribute, { className: 'input-pre-box' + (otherInputAttribute.className ? ' ' + otherInputAttribute.className : '') }),
        React.createElement('textarea', {
          ref: refElInput,
          autoFocus: autoFocus,
          value: value,
          defaultValue: defaultValue,
          maxLength: maxLength,
          readOnly: readOnly,
          disabled: disabled,
          className: 'input-pre',
          placeholder: placeholder,
          onChange: changeHandler,
          onBlur: onBlur,
          onFocus: onFocus
        }),
        React.createElement(
          'pre',
          { ref: refElPre, style: { left: preLeft, right: preRight } },
          React.createElement('span', null)
        )
      );
    }
    // textarea类型
    if (type === 'textarea') {
      // 如果值绑定属性,则只有通过父组件的prop来改变值
      return React.createElement('textarea', _extends({
        ref: refElInput
      }, otherInputAttribute, {
        autoFocus: autoFocus,
        value: value,
        defaultValue: defaultValue,
        maxLength: maxLength,
        readOnly: readOnly,
        disabled: disabled,
        placeholder: placeholder,
        onChange: changeHandler,
        onBlur: onBlur,
        onFocus: onFocus,
        className: 'input-area' + (otherInputAttribute.className ? ' ' + otherInputAttribute.className : '')
      }));
    }
    // 其它类型
    return React.createElement('input', _extends({
      ref: refElInput,
      type: type
    }, otherInputAttribute, {
      className: 'input-text' + (otherInputAttribute.className ? ' ' + otherInputAttribute.className : ''),
      defaultValue: defaultValue,
      value: value,
      min: min,
      max: max,
      maxLength: maxLength,
      disabled: disabled,
      readOnly: readOnly,
      placeholder: placeholder,
      onChange: changeHandler,
      autoFocus: autoFocus,
      onBlur: onBlur, onFocus: onFocus }));
  }

  // 过滤已经回调的属性
  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: clickHandler }),
    licon && licon,
    liconAttribute && React.createElement('i', _extends({}, otherLiconAttribute, { className: 'licon icon' + (otherLiconAttribute.className ? ' ' + otherLiconAttribute.className : '') })),
    getInputDOM(),
    children && children,
    clearShow && clear && !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;