Collapse.js 6.0 KB

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