function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React, { Component } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import closest from '../_util/closest';

var PlateNumberItem =
/*#__PURE__*/
function (_Component) {
  _inherits(PlateNumberItem, _Component);

  function PlateNumberItem(props) {
    var _this;

    _classCallCheck(this, PlateNumberItem);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(PlateNumberItem).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "onInputBlur", function () {
      var isHideKeyboard = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
      var changePropsFunc = _this.props.changePropsFunc;
      var focus = _this.state.focus;

      if (isHideKeyboard) {
        changePropsFunc({
          keyboardVisible: false
        });
      }

      if (focus) {
        _this.setState({
          focus: false
        });

        setTimeout(function () {
          _this.removeBlurListener();
        }, 50);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "doBlur", function (ev) {
      var prefixCls = _this.props.prefixCls;
      var itemNode = closest(ev.target, ".".concat(prefixCls, "-item"));
      var keyboardNode = closest(ev.target, '.parking-keyboard'); // 点击区域如果不是键盘或者车牌输入框则收起键盘

      if (ev.target !== _this.fakeInputRef && !keyboardNode) {
        _this.onInputBlur(!itemNode);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "addBlurListener", function () {
      document.addEventListener('click', _this.doBlur, false);
    });

    _defineProperty(_assertThisInitialized(_this), "removeBlurListener", function () {
      document.removeEventListener('click', _this.doBlur, false);
    });

    _defineProperty(_assertThisInitialized(_this), "onInputFocus", function () {
      var _this$props = _this.props,
          changePropsFunc = _this$props.changePropsFunc,
          index = _this$props.index;
      changePropsFunc({
        focusIndex: index,
        keyboardVisible: true,
        keyboardType: index === 0 ? 'province' : 'ABC'
      });

      _this.setState({
        focus: true
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onFakeInputClick", function () {
      // this focus may invoked by users page button click, so this click may trigger blurEventListener at the same time
      _this.removeBlurListener();

      var focus = _this.state.focus;

      if (!focus) {
        _this.onInputFocus();
      }

      setTimeout(function () {
        _this.addBlurListener();
      }, 50);
    });

    _defineProperty(_assertThisInitialized(_this), "saveRef", function (el) {
      _this.fakeInputRef = el;
    });

    _this.fakeInputRef = null;
    _this.state = {
      focus: false,
      value: props.value || ''
    };
    return _this;
  }

  _createClass(PlateNumberItem, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      if ('value' in nextProps) {
        this.setState({
          value: nextProps.value
        });
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.removeBlurListener();
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props2 = this.props,
          prefixCls = _this$props2.prefixCls,
          className = _this$props2.className,
          itemClassName = _this$props2.itemClassName,
          children = _this$props2.children;
      var _this$state = this.state,
          focus = _this$state.focus,
          value = _this$state.value;
      var itemWrapClassName = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-item-box"), true), _defineProperty(_classNames, className, true), _defineProperty(_classNames, "activated", focus), _classNames), itemClassName);
      return React.createElement("div", {
        className: "".concat(prefixCls, "-item"),
        ref: this.saveRef,
        onClick: this.onFakeInputClick
      }, React.createElement("div", {
        className: "".concat(prefixCls, "-item-container")
      }, React.createElement("div", {
        className: "".concat(prefixCls, "-item-layout")
      }, React.createElement("div", {
        className: itemWrapClassName
      }, value || children))));
    }
  }]);

  return PlateNumberItem;
}(Component);

_defineProperty(PlateNumberItem, "propTypes", {
  prefixCls: PropTypes.string,
  className: PropTypes.string,
  itemClassName: PropTypes.string,
  children: PropTypes.node,
  value: PropTypes.string,
  // 当前组件的 index
  index: PropTypes.number,
  // 修改父组件 state
  changePropsFunc: PropTypes.func
});

_defineProperty(PlateNumberItem, "defaultProps", {
  prefixCls: 'eh-plate-number'
});

export default PlateNumberItem;