DropdownMenu.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. DROPDOWN_KEY: () => DROPDOWN_KEY,
  21. default: () => stdin_default,
  22. dropdownMenuProps: () => dropdownMenuProps
  23. });
  24. module.exports = __toCommonJS(stdin_exports);
  25. var import_vue = require("vue");
  26. var import_utils = require("../utils");
  27. var import_use_id = require("../composables/use-id");
  28. var import_use_expose = require("../composables/use-expose");
  29. var import_use = require("@vant/use");
  30. const [name, bem] = (0, import_utils.createNamespace)("dropdown-menu");
  31. const dropdownMenuProps = {
  32. overlay: import_utils.truthProp,
  33. zIndex: import_utils.numericProp,
  34. duration: (0, import_utils.makeNumericProp)(0.2),
  35. direction: (0, import_utils.makeStringProp)("down"),
  36. activeColor: String,
  37. autoLocate: Boolean,
  38. closeOnClickOutside: import_utils.truthProp,
  39. closeOnClickOverlay: import_utils.truthProp,
  40. swipeThreshold: import_utils.numericProp
  41. };
  42. const DROPDOWN_KEY = Symbol(name);
  43. var stdin_default = (0, import_vue.defineComponent)({
  44. name,
  45. props: dropdownMenuProps,
  46. setup(props, {
  47. slots
  48. }) {
  49. const id = (0, import_use_id.useId)();
  50. const root = (0, import_vue.ref)();
  51. const barRef = (0, import_vue.ref)();
  52. const offset = (0, import_vue.ref)(0);
  53. const {
  54. children,
  55. linkChildren
  56. } = (0, import_use.useChildren)(DROPDOWN_KEY);
  57. const scrollParent = (0, import_use.useScrollParent)(root);
  58. const opened = (0, import_vue.computed)(() => children.some((item) => item.state.showWrapper));
  59. const scrollable = (0, import_vue.computed)(() => props.swipeThreshold && children.length > +props.swipeThreshold);
  60. const barStyle = (0, import_vue.computed)(() => {
  61. if (opened.value && (0, import_utils.isDef)(props.zIndex)) {
  62. return {
  63. zIndex: +props.zIndex + 1
  64. };
  65. }
  66. });
  67. const close = () => {
  68. children.forEach((item) => {
  69. item.toggle(false);
  70. });
  71. };
  72. const onClickAway = () => {
  73. if (props.closeOnClickOutside) {
  74. close();
  75. }
  76. };
  77. const updateOffset = () => {
  78. if (barRef.value) {
  79. const rect = (0, import_use.useRect)(barRef);
  80. if (props.direction === "down") {
  81. offset.value = rect.bottom;
  82. } else {
  83. offset.value = import_utils.windowHeight.value - rect.top;
  84. }
  85. }
  86. };
  87. const onScroll = () => {
  88. if (opened.value) {
  89. updateOffset();
  90. }
  91. };
  92. const toggleItem = (active) => {
  93. children.forEach((item, index) => {
  94. if (index === active) {
  95. item.toggle();
  96. } else if (item.state.showPopup) {
  97. item.toggle(false, {
  98. immediate: true
  99. });
  100. }
  101. });
  102. };
  103. const renderTitle = (item, index) => {
  104. const {
  105. showPopup
  106. } = item.state;
  107. const {
  108. disabled,
  109. titleClass
  110. } = item;
  111. return (0, import_vue.createVNode)("div", {
  112. "id": `${id}-${index}`,
  113. "role": "button",
  114. "tabindex": disabled ? void 0 : 0,
  115. "data-allow-mismatch": "attribute",
  116. "class": [bem("item", {
  117. disabled,
  118. grow: scrollable.value
  119. }), {
  120. [import_utils.HAPTICS_FEEDBACK]: !disabled
  121. }],
  122. "onClick": () => {
  123. if (!disabled) {
  124. toggleItem(index);
  125. }
  126. }
  127. }, [(0, import_vue.createVNode)("span", {
  128. "class": [bem("title", {
  129. down: showPopup === (props.direction === "down"),
  130. active: showPopup
  131. }), titleClass],
  132. "style": {
  133. color: showPopup ? props.activeColor : ""
  134. }
  135. }, [(0, import_vue.createVNode)("div", {
  136. "class": "van-ellipsis"
  137. }, [item.renderTitle()])])]);
  138. };
  139. (0, import_use_expose.useExpose)({
  140. close
  141. });
  142. linkChildren({
  143. id,
  144. props,
  145. offset,
  146. updateOffset
  147. });
  148. (0, import_use.useClickAway)(root, onClickAway);
  149. (0, import_use.useEventListener)("scroll", onScroll, {
  150. target: scrollParent,
  151. passive: true
  152. });
  153. return () => {
  154. var _a;
  155. return (0, import_vue.createVNode)("div", {
  156. "ref": root,
  157. "class": bem()
  158. }, [(0, import_vue.createVNode)("div", {
  159. "ref": barRef,
  160. "style": barStyle.value,
  161. "class": bem("bar", {
  162. opened: opened.value,
  163. scrollable: scrollable.value
  164. })
  165. }, [children.map(renderTitle)]), (_a = slots.default) == null ? void 0 : _a.call(slots)]);
  166. };
  167. }
  168. });