CollapsePanel.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _extends from "@babel/runtime/helpers/esm/extends";
  3. import { withDirectives as _withDirectives, resolveDirective as _resolveDirective, vShow as _vShow, createVNode as _createVNode } from "vue";
  4. import PanelContent from './PanelContent';
  5. import { initDefaultProps } from '../_util/props-util';
  6. import { collapsePanelProps } from './commonProps';
  7. import { defineComponent } from 'vue';
  8. import Transition from '../_util/transition';
  9. import classNames from '../_util/classNames';
  10. import devWarning from '../vc-util/devWarning';
  11. import useConfigInject from '../config-provider/hooks/useConfigInject';
  12. export { collapsePanelProps };
  13. export default defineComponent({
  14. compatConfig: {
  15. MODE: 3
  16. },
  17. name: 'ACollapsePanel',
  18. inheritAttrs: false,
  19. props: initDefaultProps(collapsePanelProps(), {
  20. showArrow: true,
  21. isActive: false,
  22. onItemClick() {},
  23. headerClass: '',
  24. forceRender: false
  25. }),
  26. slots: Object,
  27. // emits: ['itemClick'],
  28. setup(props, _ref) {
  29. let {
  30. slots,
  31. emit,
  32. attrs
  33. } = _ref;
  34. devWarning(props.disabled === undefined, 'Collapse.Panel', '`disabled` is deprecated. Please use `collapsible="disabled"` instead.');
  35. const {
  36. prefixCls
  37. } = useConfigInject('collapse', props);
  38. const handleItemClick = () => {
  39. emit('itemClick', props.panelKey);
  40. };
  41. const handleKeyPress = e => {
  42. if (e.key === 'Enter' || e.keyCode === 13 || e.which === 13) {
  43. handleItemClick();
  44. }
  45. };
  46. return () => {
  47. var _a, _b;
  48. const {
  49. header = (_a = slots.header) === null || _a === void 0 ? void 0 : _a.call(slots),
  50. headerClass,
  51. isActive,
  52. showArrow,
  53. destroyInactivePanel,
  54. accordion,
  55. forceRender,
  56. openAnimation,
  57. expandIcon = slots.expandIcon,
  58. extra = (_b = slots.extra) === null || _b === void 0 ? void 0 : _b.call(slots),
  59. collapsible
  60. } = props;
  61. const disabled = collapsible === 'disabled';
  62. const prefixClsValue = prefixCls.value;
  63. const headerCls = classNames(`${prefixClsValue}-header`, {
  64. [headerClass]: headerClass,
  65. [`${prefixClsValue}-header-collapsible-only`]: collapsible === 'header',
  66. [`${prefixClsValue}-icon-collapsible-only`]: collapsible === 'icon'
  67. });
  68. const itemCls = classNames({
  69. [`${prefixClsValue}-item`]: true,
  70. [`${prefixClsValue}-item-active`]: isActive,
  71. [`${prefixClsValue}-item-disabled`]: disabled,
  72. [`${prefixClsValue}-no-arrow`]: !showArrow,
  73. [`${attrs.class}`]: !!attrs.class
  74. });
  75. let icon = _createVNode("i", {
  76. "class": "arrow"
  77. }, null);
  78. if (showArrow && typeof expandIcon === 'function') {
  79. icon = expandIcon(props);
  80. }
  81. const panelContent = _withDirectives(_createVNode(PanelContent, {
  82. "prefixCls": prefixClsValue,
  83. "isActive": isActive,
  84. "forceRender": forceRender,
  85. "role": accordion ? 'tabpanel' : null
  86. }, {
  87. default: slots.default
  88. }), [[_vShow, isActive]]);
  89. const transitionProps = _extends({
  90. appear: false,
  91. css: false
  92. }, openAnimation);
  93. return _createVNode("div", _objectSpread(_objectSpread({}, attrs), {}, {
  94. "class": itemCls
  95. }), [_createVNode("div", {
  96. "class": headerCls,
  97. "onClick": () => !['header', 'icon'].includes(collapsible) && handleItemClick(),
  98. "role": accordion ? 'tab' : 'button',
  99. "tabindex": disabled ? -1 : 0,
  100. "aria-expanded": isActive,
  101. "onKeypress": handleKeyPress
  102. }, [showArrow && icon, _createVNode("span", {
  103. "onClick": () => collapsible === 'header' && handleItemClick(),
  104. "class": `${prefixClsValue}-header-text`
  105. }, [header]), extra && _createVNode("div", {
  106. "class": `${prefixClsValue}-extra`
  107. }, [extra])]), _createVNode(Transition, transitionProps, {
  108. default: () => [!destroyInactivePanel || isActive ? panelContent : null]
  109. })]);
  110. };
  111. }
  112. });