DropdownItem.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. default: () => stdin_default,
  21. dropdownItemProps: () => dropdownItemProps
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_vue2 = require("vue");
  26. var import_utils = require("../utils");
  27. var import_DropdownMenu = require("../dropdown-menu/DropdownMenu");
  28. var import_use = require("@vant/use");
  29. var import_use_expose = require("../composables/use-expose");
  30. var import_cell = require("../cell");
  31. var import_icon = require("../icon");
  32. var import_popup = require("../popup");
  33. const [name, bem] = (0, import_utils.createNamespace)("dropdown-item");
  34. const dropdownItemProps = {
  35. title: String,
  36. options: (0, import_utils.makeArrayProp)(),
  37. disabled: Boolean,
  38. teleport: [String, Object],
  39. lazyRender: import_utils.truthProp,
  40. modelValue: import_utils.unknownProp,
  41. titleClass: import_utils.unknownProp
  42. };
  43. var stdin_default = (0, import_vue2.defineComponent)({
  44. name,
  45. inheritAttrs: false,
  46. props: dropdownItemProps,
  47. emits: ["open", "opened", "close", "closed", "change", "update:modelValue"],
  48. setup(props, {
  49. emit,
  50. slots,
  51. attrs
  52. }) {
  53. const state = (0, import_vue2.reactive)({
  54. showPopup: false,
  55. transition: true,
  56. showWrapper: false
  57. });
  58. const {
  59. parent,
  60. index
  61. } = (0, import_use.useParent)(import_DropdownMenu.DROPDOWN_KEY);
  62. if (!parent) {
  63. if (process.env.NODE_ENV !== "production") {
  64. console.error("[Vant] <DropdownItem> must be a child component of <DropdownMenu>.");
  65. }
  66. return;
  67. }
  68. const getEmitter = (name2) => () => emit(name2);
  69. const onOpen = getEmitter("open");
  70. const onClose = getEmitter("close");
  71. const onOpened = getEmitter("opened");
  72. const onClosed = () => {
  73. state.showWrapper = false;
  74. emit("closed");
  75. };
  76. const onClickWrapper = (event) => {
  77. if (props.teleport) {
  78. event.stopPropagation();
  79. }
  80. };
  81. const toggle = (show = !state.showPopup, options = {}) => {
  82. if (show === state.showPopup) {
  83. return;
  84. }
  85. state.showPopup = show;
  86. state.transition = !options.immediate;
  87. if (show) {
  88. state.showWrapper = true;
  89. }
  90. };
  91. const renderTitle = () => {
  92. if (slots.title) {
  93. return slots.title();
  94. }
  95. if (props.title) {
  96. return props.title;
  97. }
  98. const match = props.options.find((option) => option.value === props.modelValue);
  99. return match ? match.text : "";
  100. };
  101. const renderOption = (option) => {
  102. const {
  103. activeColor
  104. } = parent.props;
  105. const active = option.value === props.modelValue;
  106. const onClick = () => {
  107. state.showPopup = false;
  108. if (option.value !== props.modelValue) {
  109. emit("update:modelValue", option.value);
  110. emit("change", option.value);
  111. }
  112. };
  113. const renderIcon = () => {
  114. if (active) {
  115. return (0, import_vue.createVNode)(import_icon.Icon, {
  116. "class": bem("icon"),
  117. "color": activeColor,
  118. "name": "success"
  119. }, null);
  120. }
  121. };
  122. return (0, import_vue.createVNode)(import_cell.Cell, {
  123. "role": "menuitem",
  124. "key": option.value,
  125. "icon": option.icon,
  126. "title": option.text,
  127. "class": bem("option", {
  128. active
  129. }),
  130. "style": {
  131. color: active ? activeColor : ""
  132. },
  133. "tabindex": active ? 0 : -1,
  134. "clickable": true,
  135. "onClick": onClick
  136. }, {
  137. value: renderIcon
  138. });
  139. };
  140. const renderContent = () => {
  141. const {
  142. offset
  143. } = parent;
  144. const {
  145. zIndex,
  146. overlay,
  147. duration,
  148. direction,
  149. closeOnClickOverlay
  150. } = parent.props;
  151. const style = (0, import_utils.getZIndexStyle)(zIndex);
  152. if (direction === "down") {
  153. style.top = `${offset.value}px`;
  154. } else {
  155. style.bottom = `${offset.value}px`;
  156. }
  157. return (0, import_vue.withDirectives)((0, import_vue.createVNode)("div", (0, import_vue.mergeProps)({
  158. "style": style,
  159. "class": bem([direction]),
  160. "onClick": onClickWrapper
  161. }, attrs), [(0, import_vue.createVNode)(import_popup.Popup, {
  162. "show": state.showPopup,
  163. "onUpdate:show": ($event) => state.showPopup = $event,
  164. "role": "menu",
  165. "class": bem("content"),
  166. "overlay": overlay,
  167. "position": direction === "down" ? "top" : "bottom",
  168. "duration": state.transition ? duration : 0,
  169. "lazyRender": props.lazyRender,
  170. "overlayStyle": {
  171. position: "absolute"
  172. },
  173. "aria-labelledby": `${parent.id}-${index.value}`,
  174. "closeOnClickOverlay": closeOnClickOverlay,
  175. "onOpen": onOpen,
  176. "onClose": onClose,
  177. "onOpened": onOpened,
  178. "onClosed": onClosed
  179. }, {
  180. default: () => {
  181. var _a;
  182. return [props.options.map(renderOption), (_a = slots.default) == null ? void 0 : _a.call(slots)];
  183. }
  184. })]), [[import_vue.vShow, state.showWrapper]]);
  185. };
  186. (0, import_use_expose.useExpose)({
  187. state,
  188. toggle,
  189. renderTitle
  190. });
  191. return () => {
  192. if (props.teleport) {
  193. return (0, import_vue.createVNode)(import_vue2.Teleport, {
  194. "to": props.teleport
  195. }, {
  196. default: () => [renderContent()]
  197. });
  198. }
  199. return renderContent();
  200. };
  201. }
  202. });