Collapse.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  3. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  4. import _typeof from "@babel/runtime/helpers/esm/typeof";
  5. import { createVNode as _createVNode } from "vue";
  6. import { isEmptyElement, initDefaultProps, flattenChildren, isValidElement } from '../_util/props-util';
  7. import { cloneElement } from '../_util/vnode';
  8. import { collapseProps } from './commonProps';
  9. import { getDataAndAriaProps } from '../_util/util';
  10. import { computed, defineComponent, ref, watch } from 'vue';
  11. import RightOutlined from "@ant-design/icons-vue/es/icons/RightOutlined";
  12. import firstNotUndefined from '../_util/firstNotUndefined';
  13. import classNames from '../_util/classNames';
  14. import useConfigInject from '../_util/hooks/useConfigInject';
  15. import collapseMotion from '../_util/collapseMotion';
  16. function getActiveKeysArray(activeKey) {
  17. var currentActiveKey = activeKey;
  18. if (!Array.isArray(currentActiveKey)) {
  19. var activeKeyType = _typeof(currentActiveKey);
  20. currentActiveKey = activeKeyType === 'number' || activeKeyType === 'string' ? [currentActiveKey] : [];
  21. }
  22. return currentActiveKey.map(function (key) {
  23. return String(key);
  24. });
  25. }
  26. export { collapseProps };
  27. export default defineComponent({
  28. compatConfig: {
  29. MODE: 3
  30. },
  31. name: 'ACollapse',
  32. inheritAttrs: false,
  33. props: initDefaultProps(collapseProps(), {
  34. accordion: false,
  35. destroyInactivePanel: false,
  36. bordered: true,
  37. openAnimation: collapseMotion('ant-motion-collapse', false),
  38. expandIconPosition: 'left'
  39. }),
  40. slots: ['expandIcon'],
  41. // emits: ['change', 'update:activeKey'],
  42. setup: function setup(props, _ref) {
  43. var attrs = _ref.attrs,
  44. slots = _ref.slots,
  45. emit = _ref.emit;
  46. var stateActiveKey = ref(getActiveKeysArray(firstNotUndefined([props.activeKey, props.defaultActiveKey])));
  47. watch(function () {
  48. return props.activeKey;
  49. }, function () {
  50. stateActiveKey.value = getActiveKeysArray(props.activeKey);
  51. }, {
  52. deep: true
  53. });
  54. var _useConfigInject = useConfigInject('collapse', props),
  55. prefixCls = _useConfigInject.prefixCls,
  56. direction = _useConfigInject.direction;
  57. var iconPosition = computed(function () {
  58. var expandIconPosition = props.expandIconPosition;
  59. if (expandIconPosition !== undefined) {
  60. return expandIconPosition;
  61. }
  62. return direction.value === 'rtl' ? 'right' : 'left';
  63. });
  64. var renderExpandIcon = function renderExpandIcon(panelProps) {
  65. var _props$expandIcon = props.expandIcon,
  66. expandIcon = _props$expandIcon === void 0 ? slots.expandIcon : _props$expandIcon;
  67. var icon = expandIcon ? expandIcon(panelProps) : _createVNode(RightOutlined, {
  68. "rotate": panelProps.isActive ? 90 : undefined
  69. }, null);
  70. return _createVNode("div", null, [isValidElement(Array.isArray(expandIcon) ? icon[0] : icon) ? cloneElement(icon, {
  71. class: "".concat(prefixCls.value, "-arrow")
  72. }, false) : icon]);
  73. };
  74. var setActiveKey = function setActiveKey(activeKey) {
  75. if (props.activeKey === undefined) {
  76. stateActiveKey.value = activeKey;
  77. }
  78. var newKey = props.accordion ? activeKey[0] : activeKey;
  79. emit('update:activeKey', newKey);
  80. emit('change', newKey);
  81. };
  82. var onClickItem = function onClickItem(key) {
  83. var activeKey = stateActiveKey.value;
  84. if (props.accordion) {
  85. activeKey = activeKey[0] === key ? [] : [key];
  86. } else {
  87. activeKey = _toConsumableArray(activeKey);
  88. var index = activeKey.indexOf(key);
  89. var isActive = index > -1;
  90. if (isActive) {
  91. // remove active state
  92. activeKey.splice(index, 1);
  93. } else {
  94. activeKey.push(key);
  95. }
  96. }
  97. setActiveKey(activeKey);
  98. };
  99. var getNewChild = function getNewChild(child, index) {
  100. var _child$key, _child$children, _child$children$heade;
  101. if (isEmptyElement(child)) return;
  102. var activeKey = stateActiveKey.value;
  103. var accordion = props.accordion,
  104. destroyInactivePanel = props.destroyInactivePanel,
  105. collapsible = props.collapsible,
  106. openAnimation = props.openAnimation;
  107. // If there is no key provide, use the panel order as default key
  108. var key = String((_child$key = child.key) !== null && _child$key !== void 0 ? _child$key : index);
  109. var _ref2 = child.props || {},
  110. _ref2$header = _ref2.header,
  111. header = _ref2$header === void 0 ? (_child$children = child.children) === null || _child$children === void 0 ? void 0 : (_child$children$heade = _child$children.header) === null || _child$children$heade === void 0 ? void 0 : _child$children$heade.call(_child$children) : _ref2$header,
  112. headerClass = _ref2.headerClass,
  113. childCollapsible = _ref2.collapsible,
  114. disabled = _ref2.disabled;
  115. var isActive = false;
  116. if (accordion) {
  117. isActive = activeKey[0] === key;
  118. } else {
  119. isActive = activeKey.indexOf(key) > -1;
  120. }
  121. var mergeCollapsible = childCollapsible !== null && childCollapsible !== void 0 ? childCollapsible : collapsible;
  122. // legacy 2.x
  123. if (disabled || disabled === '') {
  124. mergeCollapsible = 'disabled';
  125. }
  126. var newProps = {
  127. key: key,
  128. panelKey: key,
  129. header: header,
  130. headerClass: headerClass,
  131. isActive: isActive,
  132. prefixCls: prefixCls.value,
  133. destroyInactivePanel: destroyInactivePanel,
  134. openAnimation: openAnimation,
  135. accordion: accordion,
  136. onItemClick: mergeCollapsible === 'disabled' ? null : onClickItem,
  137. expandIcon: renderExpandIcon,
  138. collapsible: mergeCollapsible
  139. };
  140. return cloneElement(child, newProps);
  141. };
  142. var getItems = function getItems() {
  143. var _slots$default;
  144. return flattenChildren((_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots)).map(getNewChild);
  145. };
  146. return function () {
  147. var _classNames;
  148. var accordion = props.accordion,
  149. bordered = props.bordered,
  150. ghost = props.ghost;
  151. var collapseClassName = classNames((_classNames = {}, _defineProperty(_classNames, prefixCls.value, true), _defineProperty(_classNames, "".concat(prefixCls.value, "-borderless"), !bordered), _defineProperty(_classNames, "".concat(prefixCls.value, "-icon-position-").concat(iconPosition.value), true), _defineProperty(_classNames, "".concat(prefixCls.value, "-rtl"), direction.value === 'rtl'), _defineProperty(_classNames, "".concat(prefixCls.value, "-ghost"), !!ghost), _defineProperty(_classNames, attrs.class, !!attrs.class), _classNames));
  152. return _createVNode("div", _objectSpread(_objectSpread({
  153. "class": collapseClassName
  154. }, getDataAndAriaProps(attrs)), {}, {
  155. "style": attrs.style,
  156. "role": accordion ? 'tablist' : null
  157. }), [getItems()]);
  158. };
  159. }
  160. });