"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; 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); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _rcTrigger = _interopRequireDefault(require("rc-trigger"));

var _mkuiFd = require("mkui-fd");

var _lodash = require("lodash");

var _Panel = _interopRequireDefault(require("../fee-type-input/Panel"));

var _util = require("./util");

var _constant = require("./constant");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": 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 _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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

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

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

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

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

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

var Select = /*#__PURE__*/function (_React$Component) {
  _inherits(Select, _React$Component);

  var _super = _createSuper(Select);

  /** *******************life cycle************************** */
  function Select(props) {
    var _this;

    _classCallCheck(this, Select);

    _this = _super.call(this, props);
    _this.state = _this.getInitState(props);
    _this.panelRef = /*#__PURE__*/_react["default"].createRef();
    _this.observeParams = ['subsidiaryCode', 'formSubTypeCode'];
    return _this;
  }

  _createClass(Select, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var params = this.props.params;
      var prevParams = prevProps.params;

      if (!(0, _lodash.isEqual)((0, _lodash.pick)(params, this.observeParams), (0, _lodash.pick)(prevParams, this.observeParams))) {
        this.paramsDirty();
      }
    }
    /** *******************handle************************** */

    /**
     * 下拉箭头点击触发
     * @param e
     */

  }, {
    key: "iconClickHandle",
    value: function iconClickHandle(e) {
      e.stopPropagation();
      e.preventDefault();
      var _this$props = this.props,
          disabled = _this$props.disabled,
          open = _this$props.open;

      if (!disabled) {
        this.toggleDropdown(!open);
      }
    }
    /**
     * 下拉面板中选择选项后触发
     * @param items Array
     */

  }, {
    key: "selectHandle",
    value: function selectHandle(items) {
      this.setState({
        value: items[0],
        open: false
      });
      this.fireChange(items[0]);
    }
  }, {
    key: "onDropdownVisibleChangeHandle",
    value: function onDropdownVisibleChangeHandle(open) {
      this.toggleDropdown(open);
    }
    /** *******************inner method************************** */

  }, {
    key: "getInitState",
    value: function getInitState(props) {
      var value = props.value;
      return {
        open: false,
        loaded: false,
        value: value,
        data: (0, _util.getDataSource)(props)
      };
    }
  }, {
    key: "toggleDropdown",
    value: function toggleDropdown(open) {
      var loaded = this.state.loaded;
      var lazyLoad = this.props.lazyLoad;

      if (!lazyLoad) {
        this.setOpenState(open);
      } else {
        if (!loaded) {
          this.fetchData();
          this.setState({
            open: open,
            loading: true
          });
        } else {
          this.setOpenState(open);
        }
      }
    }
  }, {
    key: "paramsDirty",
    value: function paramsDirty() {
      this.setState({
        loaded: false,
        loading: true
      });
    }
  }, {
    key: "fetchData",
    value: function fetchData() {
      var _this2 = this;

      var _this$props2 = this.props,
          api = _this$props2.api,
          params = _this$props2.params;
      api(params).then(function (data) {
        _this2.setState({
          data: data,
          loaded: true,
          loading: false
        });
      });
    }
  }, {
    key: "setOpenState",
    value: function setOpenState(open) {
      this.setState({
        open: open
      });
    }
  }, {
    key: "fireChange",
    value: function fireChange(value) {
      var onChange = this.props.onChange;
      onChange(value);
    }
  }, {
    key: "getPlaceholderElement",
    value: function getPlaceholderElement() {
      var _this$props3 = this.props,
          placeholder = _this$props3.placeholder,
          prefixCls = _this$props3.prefixCls;

      if (placeholder) {
        return /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-selection__placeholder"),
          unselectable: "on"
        }, placeholder);
      }

      return null;
    }
  }, {
    key: "getDropdownElement",
    value: function getDropdownElement() {
      var _this3 = this;

      var _this$props4 = this.props,
          prefixCls = _this$props4.prefixCls,
          value = _this$props4.value;
      var _this$state = this.state,
          data = _this$state.data,
          loading = _this$state.loading;
      var selected;

      if (value) {
        selected = [value.code];
      } else {
        selected = [];
      }

      if (loading) {
        return /*#__PURE__*/_react["default"].createElement(_mkuiFd.Icon, {
          type: "loading",
          className: "".concat(prefixCls, "-loading")
        });
      } else {
        return /*#__PURE__*/_react["default"].createElement(_Panel["default"], {
          data: data,
          selected: selected,
          ref: this.panelRef,
          onSelect: function onSelect(item) {
            _this3.selectHandle(item);
          }
        });
      }
    }
    /** *******************render method************************** */

  }, {
    key: "renderTopControlNode",
    value: function renderTopControlNode() {
      var value = this.state.value;
      var _this$props5 = this.props,
          disabled = _this$props5.disabled,
          prefixCls = _this$props5.prefixCls;
      var className = "".concat(prefixCls, "-selection__rendered");
      var innerNode = null;

      if (value) {
        var text = value.name;

        var selectedValue = /*#__PURE__*/_react["default"].createElement("div", {
          key: "value",
          className: "".concat(prefixCls, "-selection-selected-value"),
          title: text
        }, text);

        innerNode = [selectedValue];
      }

      return /*#__PURE__*/_react["default"].createElement("div", {
        className: className
      }, !innerNode ? this.getPlaceholderElement() : innerNode);
    }
  }, {
    key: "renderDropdownIcon",
    value: function renderDropdownIcon() {
      var _this4 = this;

      var prefixCls = this.props.prefixCls;
      return /*#__PURE__*/_react["default"].createElement(_mkuiFd.Icon, {
        type: "down",
        className: "".concat(prefixCls, "-selection-icon"),
        onClick: function onClick(event) {
          return _this4.iconClickHandle(event);
        }
      });
    }
  }, {
    key: "renderIcon",
    value: function renderIcon() {
      var prefixCls = this.props.prefixCls;
      var value = this.state.value;
      var icon = value && value.icon ? (0, _util.getIconName)(value.icon) : 'question-circle';
      return /*#__PURE__*/_react["default"].createElement(_mkuiFd.Icon, {
        className: "".concat(prefixCls, "-icon"),
        type: icon
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _rootCls,
          _this5 = this;

      var _this$props6 = this.props,
          disabled = _this$props6.disabled,
          prefixCls = _this$props6.prefixCls,
          placement = _this$props6.placement;
      var _this$state2 = this.state,
          open = _this$state2.open,
          value = _this$state2.value;
      var ctrlNode = this.renderTopControlNode();
      var popupElement = this.getDropdownElement();
      var rootCls = (_rootCls = {}, _defineProperty(_rootCls, prefixCls, 1), _defineProperty(_rootCls, "".concat(prefixCls, "-open"), open), _defineProperty(_rootCls, "".concat(prefixCls, "-disabled"), disabled), _rootCls);
      return /*#__PURE__*/_react["default"].createElement(_rcTrigger["default"], {
        showAction: disabled ? [] : ['click'],
        hideAction: ['click'],
        popupPlacement: placement,
        builtinPlacements: _constant.BUILT_IN_PLACEMENTS,
        prefixCls: "mkui-popedit-dropdown-wrapper",
        popup: popupElement,
        mask: true,
        getPopupContainer: Select.getPopupContainer,
        onPopupVisibleChange: function onPopupVisibleChange(visible) {
          return _this5.onDropdownVisibleChangeHandle(visible);
        },
        popupVisible: open
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: rootCls
      }, " ", this.renderIcon(), /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-selection")
      }, ctrlNode, this.renderDropdownIcon())));
    }
  }], [{
    key: "getPopupContainer",
    value:
    /** *******************static************************** */
    function getPopupContainer() {
      return document.body;
    }
  }, {
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(nextProps) {
      if ('value' in nextProps) {
        return {
          value: nextProps.value
        };
      }

      return null;
    }
  }]);

  return Select;
}(_react["default"].Component);

_defineProperty(Select, "propTypes", {
  prefixCls: _propTypes["default"].string,
  disabled: _propTypes["default"].bool,
  placeholder: _propTypes["default"].string,
  onChange: _propTypes["default"].func,
  data: _propTypes["default"].array,
  value: _propTypes["default"].object,
  lazyLoad: _propTypes["default"].bool,
  api: _propTypes["default"].func,
  // 异步加载时会用到
  placement: _propTypes["default"].string
});

_defineProperty(Select, "defaultProps", {
  prefixCls: 'mkui-fee-type-input-icon',
  placeholder: '',
  onChange: null,
  disabled: false,
  data: [],
  lazyLoad: true,
  api: null,
  placement: _constant.POPUP_PLACEMENT
});

var _default = Select;
exports["default"] = _default;
//# sourceMappingURL=Select.js.map
