CollapseItem.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { withDirectives as _withDirectives, vShow as _vShow, createVNode as _createVNode, mergeProps as _mergeProps } from "vue";
  2. import { ref, watch, computed, nextTick, defineComponent } from "vue";
  3. import { cellSharedProps } from "../cell/Cell.mjs";
  4. import { pick, extend, truthProp, numericProp, createNamespace } from "../utils/index.mjs";
  5. import { COLLAPSE_KEY } from "../collapse/Collapse.mjs";
  6. import { raf, doubleRaf, useParent } from "@vant/use";
  7. import { useExpose } from "../composables/use-expose.mjs";
  8. import { useLazyRender } from "../composables/use-lazy-render.mjs";
  9. import { Cell } from "../cell/index.mjs";
  10. const [name, bem] = createNamespace("collapse-item");
  11. const CELL_SLOTS = ["icon", "title", "value", "label", "right-icon"];
  12. const collapseItemProps = extend({}, cellSharedProps, {
  13. name: numericProp,
  14. isLink: truthProp,
  15. disabled: Boolean,
  16. readonly: Boolean,
  17. lazyRender: truthProp
  18. });
  19. var stdin_default = defineComponent({
  20. name,
  21. props: collapseItemProps,
  22. setup(props, {
  23. slots
  24. }) {
  25. const wrapperRef = ref();
  26. const contentRef = ref();
  27. const {
  28. parent,
  29. index
  30. } = useParent(COLLAPSE_KEY);
  31. if (!parent) {
  32. if (process.env.NODE_ENV !== "production") {
  33. console.error("[Vant] <CollapseItem> must be a child component of <Collapse>.");
  34. }
  35. return;
  36. }
  37. const name2 = computed(() => {
  38. var _a;
  39. return (_a = props.name) != null ? _a : index.value;
  40. });
  41. const expanded = computed(() => parent.isExpanded(name2.value));
  42. const show = ref(expanded.value);
  43. const lazyRender = useLazyRender(() => show.value || !props.lazyRender);
  44. const onTransitionEnd = () => {
  45. if (!expanded.value) {
  46. show.value = false;
  47. } else if (wrapperRef.value) {
  48. wrapperRef.value.style.height = "";
  49. }
  50. };
  51. watch(expanded, (value, oldValue) => {
  52. if (oldValue === null) {
  53. return;
  54. }
  55. if (value) {
  56. show.value = true;
  57. }
  58. const tick = value ? nextTick : raf;
  59. tick(() => {
  60. if (!contentRef.value || !wrapperRef.value) {
  61. return;
  62. }
  63. const {
  64. offsetHeight
  65. } = contentRef.value;
  66. if (offsetHeight) {
  67. const contentHeight = `${offsetHeight}px`;
  68. wrapperRef.value.style.height = value ? "0" : contentHeight;
  69. doubleRaf(() => {
  70. if (wrapperRef.value) {
  71. wrapperRef.value.style.height = value ? contentHeight : "0";
  72. }
  73. });
  74. } else {
  75. onTransitionEnd();
  76. }
  77. });
  78. });
  79. const toggle = (newValue = !expanded.value) => {
  80. parent.toggle(name2.value, newValue);
  81. };
  82. const onClickTitle = () => {
  83. if (!props.disabled && !props.readonly) {
  84. toggle();
  85. }
  86. };
  87. const renderTitle = () => {
  88. const {
  89. border,
  90. disabled,
  91. readonly
  92. } = props;
  93. const attrs = pick(props, Object.keys(cellSharedProps));
  94. if (readonly) {
  95. attrs.isLink = false;
  96. }
  97. if (disabled || readonly) {
  98. attrs.clickable = false;
  99. }
  100. return _createVNode(Cell, _mergeProps({
  101. "role": "button",
  102. "class": bem("title", {
  103. disabled,
  104. expanded: expanded.value,
  105. borderless: !border
  106. }),
  107. "aria-expanded": String(expanded.value),
  108. "onClick": onClickTitle
  109. }, attrs), pick(slots, CELL_SLOTS));
  110. };
  111. const renderContent = lazyRender(() => {
  112. var _a;
  113. return _withDirectives(_createVNode("div", {
  114. "ref": wrapperRef,
  115. "class": bem("wrapper"),
  116. "onTransitionend": onTransitionEnd
  117. }, [_createVNode("div", {
  118. "ref": contentRef,
  119. "class": bem("content")
  120. }, [(_a = slots.default) == null ? void 0 : _a.call(slots)])]), [[_vShow, show.value]]);
  121. });
  122. useExpose({
  123. toggle,
  124. expanded,
  125. itemName: name2
  126. });
  127. return () => _createVNode("div", {
  128. "class": [bem({
  129. border: index.value && props.border
  130. })]
  131. }, [renderTitle(), renderContent()]);
  132. }
  133. });
  134. export {
  135. stdin_default as default
  136. };