DropdownItem.js 6.0 KB

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