Collapse.js 6.6 KB

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