DropdownItem.mjs 5.0 KB

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