var _excluded = ["value", "onClick", "onChange", "type", "pickerProps"];

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React, { useState, forwardRef, Fragment } from 'react';
import InputText from './../InputText';
import PickerDistrict from './../PickerDistrict'; // 函数组件因为没有实例, 所以也没有ref, 必须通过forwardRef回调ref

var InputDistrict = forwardRef(function (_ref, ref) {
  var value = _ref.value,
      onClick = _ref.onClick,
      onChange = _ref.onChange,
      _ref$type = _ref.type,
      type = _ref$type === void 0 ? '' : _ref$type,
      _ref$pickerProps = _ref.pickerProps,
      pickerProps = _ref$pickerProps === void 0 ? {} : _ref$pickerProps,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  var _useState = useState(false),
      show = _useState[0],
      setShow = _useState[1]; // 点击文本框


  function onClickInput() {
    if (onClick) onClick.apply(void 0, arguments);
    if (others.readOnly) return;
    setShow(!show);
  } // 点击遮罩


  function onClickMask(e) {
    if (pickerProps && pickerProps.maskAttribute && pickerProps.maskAttribute.onClick) {
      pickerProps.maskAttribute.onClick(e);
    }

    setShow(!show);
  } // 点击确定按钮


  function onClickSubmit(e, value, options) {
    if (pickerProps && pickerProps.submitAttribute && pickerProps.submitAttribute.onClick) {
      // 允许确定错误时中断隐藏
      if (pickerProps.submitAttribute.onClick(e, value, options) === false) return;
    } else if (onChange) {
      onChange(e, value, options);
    } // 隐藏框


    setShow(!show);
  } // 点击取消按钮


  function onClickCancel(e) {
    if (pickerProps && pickerProps.cancelAttribute && pickerProps.cancelAttribute.onClick) {
      pickerProps.cancelAttribute.onClick(e);
    }

    setShow(!show);
  }

  return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(InputText, _extends({
    ref: ref,
    value: value
  }, others, {
    type: "text",
    readOnly: true,
    onClick: onClickInput
  })), /*#__PURE__*/React.createElement(PickerDistrict, _extends({
    type: type
  }, pickerProps, {
    maskAttribute: _extends({}, pickerProps.maskAttribute, {
      onClick: onClickMask
    }),
    submitAttribute: _extends({}, pickerProps.submitAttribute, {
      onClick: onClickSubmit
    }),
    cancelAttribute: _extends({}, pickerProps.cancelAttribute, {
      onClick: onClickCancel
    }),
    value: value,
    show: pickerProps.show === undefined ? show : pickerProps.show
  })));
});
export default InputDistrict;