"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

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

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _classnames = _interopRequireDefault(require("classnames"));

var _enum = require("choerodon-ui/dataset/data-set/enum");

var _configProvider = _interopRequireDefault(require("choerodon-ui/lib/config-provider"));

var _PanelContent = _interopRequireDefault(require("./PanelContent"));

var _animate = _interopRequireDefault(require("../../animate"));

var CollapsePanel = function CollapsePanel(props) {
  var _classNames2, _classNames3;

  var className = props.className,
      id = props.id,
      style = props.style,
      prefixCls = props.prefixCls,
      header = props.header,
      headerClass = props.headerClass,
      children = props.children,
      isActive = props.isActive,
      showArrow = props.showArrow,
      destroyInactivePanel = props.destroyInactivePanel,
      disabled = props.disabled,
      accordion = props.accordion,
      forceRender = props.forceRender,
      expandIcon = props.expandIcon,
      expandIconPosition = props.expandIconPosition,
      extra = props.extra,
      trigger = props.trigger,
      openAnimation = props.openAnimation,
      onItemClick = props.onItemClick,
      eventKey = props.eventKey,
      dataSet = props.dataSet,
      hidden = props.hidden;

  if (hidden) {
    return null;
  }

  var headerCls = (0, _classnames["default"])("".concat(prefixCls, "-header"), headerClass, (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-item-expand-renderer"), showArrow && typeof expandIcon === 'function'));
  var itemCls = (0, _classnames["default"])("".concat(prefixCls, "-item"), (_classNames2 = {}, (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-item-active"), isActive), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-item-disabled"), disabled), _classNames2), className);
  var iconCls = (0, _classnames["default"])("".concat(prefixCls, "-expand-icon"), (_classNames3 = {}, (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-expanded"), isActive), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-collapsed"), !isActive), _classNames3));
  var handleItemClick = (0, _react.useCallback)(function () {
    if (onItemClick) {
      onItemClick(eventKey);
    }
  }, [onItemClick, eventKey]);
  var handleKeyPress = (0, _react.useCallback)(function (e) {
    if (e.key === 'Enter' || e.keyCode === 13 || e.which === 13) {
      handleItemClick();
    }
  }, [handleItemClick]);
  var icon = showArrow ? /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefixCls, "-expand-icon-wrapper"),
    onClick: trigger === 'icon' ? handleItemClick : undefined
  }, typeof expandIcon === 'function' ? expandIcon(props) : /*#__PURE__*/_react["default"].createElement("i", {
    className: iconCls
  })) : null;
  var wrapRef = (0, _react.useRef)(null);
  var handleValidationReport = (0, _react.useCallback)(function (_ref) {
    var showInvalid = _ref.showInvalid;

    if (showInvalid && !isActive && !disabled) {
      handleItemClick();
      var current = wrapRef.current;

      if (current) {
        current.focus();
      }
    }
  }, [handleItemClick, isActive, disabled]);
  var handleValidate = (0, _react.useCallback)(function (_ref2) {
    var valid = _ref2.valid,
        dataSet = _ref2.dataSet;
    handleValidationReport({
      showInvalid: !valid,
      component: dataSet
    });
  }, [handleValidationReport]);
  var dsList = dataSet ? [].concat(dataSet) : [];
  var length = dsList.length;
  (0, _react.useEffect)(function () {
    if (length && !disabled && !isActive) {
      dsList.forEach(function (ds) {
        return ds.addEventListener(_enum.DataSetEvents.validate, handleValidate).addEventListener(_enum.DataSetEvents.validateSelf, handleValidate);
      });
      return function () {
        return dsList.forEach(function (ds) {
          return ds.removeEventListener(_enum.DataSetEvents.validate, handleValidate).removeEventListener(_enum.DataSetEvents.validateSelf, handleValidate);
        });
      };
    }
  }, [isActive, disabled, handleValidate, length].concat((0, _toConsumableArray2["default"])(dsList)));
  var childrenWithProvider = length ? children : /*#__PURE__*/_react["default"].createElement(_configProvider["default"], {
    onComponentValidationReport: handleValidationReport
  }, children);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: itemCls,
    style: style,
    id: id,
    ref: wrapRef,
    tabIndex: -1
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: headerCls,
    onClick: trigger === 'header' ? handleItemClick : undefined,
    role: accordion ? 'tab' : 'button',
    tabIndex: disabled ? -1 : 0,
    "aria-expanded": "".concat(isActive),
    onKeyPress: handleKeyPress
  }, showArrow && expandIconPosition !== 'text-right' && icon, header, showArrow && expandIconPosition === 'text-right' && icon, extra && /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefixCls, "-extra")
  }, extra)), /*#__PURE__*/_react["default"].createElement(_animate["default"], {
    hiddenProp: "isInactive",
    exclusive: true,
    component: "",
    animation: openAnimation
  }, /*#__PURE__*/_react["default"].createElement(_PanelContent["default"], {
    prefixCls: prefixCls,
    isInactive: !isActive,
    destroyInactivePanel: destroyInactivePanel,
    forceRender: forceRender,
    role: accordion ? 'tabpanel' : null
  }, childrenWithProvider)));
};

CollapsePanel.displayName = 'RcCollapsePanel';
CollapsePanel.defaultProps = {
  showArrow: true,
  isActive: false,
  destroyInactivePanel: false,
  forceRender: false,
  hidden: false
};
var _default = CollapsePanel;
exports["default"] = _default;
//# sourceMappingURL=Panel.js.map
