ActionSheet.mjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { nextTick, defineComponent, createVNode as _createVNode, mergeProps as _mergeProps } from "vue";
  2. import { pick, extend, truthProp, makeArrayProp, makeStringProp, createNamespace, HAPTICS_FEEDBACK } from "../utils/index.mjs";
  3. import { Icon } from "../icon/index.mjs";
  4. import { Popup } from "../popup/index.mjs";
  5. import { Loading } from "../loading/index.mjs";
  6. import { popupSharedProps, popupSharedPropKeys } from "../popup/shared.mjs";
  7. const [name, bem] = createNamespace("action-sheet");
  8. const actionSheetProps = extend({}, popupSharedProps, {
  9. title: String,
  10. round: truthProp,
  11. actions: makeArrayProp(),
  12. closeIcon: makeStringProp("cross"),
  13. closeable: truthProp,
  14. cancelText: String,
  15. description: String,
  16. closeOnPopstate: truthProp,
  17. closeOnClickAction: Boolean,
  18. safeAreaInsetBottom: truthProp
  19. });
  20. const popupInheritKeys = [...popupSharedPropKeys, "round", "closeOnPopstate", "safeAreaInsetBottom"];
  21. var stdin_default = defineComponent({
  22. name,
  23. props: actionSheetProps,
  24. emits: ["select", "cancel", "update:show"],
  25. setup(props, {
  26. slots,
  27. emit
  28. }) {
  29. const updateShow = (show) => emit("update:show", show);
  30. const onCancel = () => {
  31. updateShow(false);
  32. emit("cancel");
  33. };
  34. const renderHeader = () => {
  35. if (props.title) {
  36. return _createVNode("div", {
  37. "class": bem("header")
  38. }, [props.title, props.closeable && _createVNode(Icon, {
  39. "name": props.closeIcon,
  40. "class": [bem("close"), HAPTICS_FEEDBACK],
  41. "onClick": onCancel
  42. }, null)]);
  43. }
  44. };
  45. const renderCancel = () => {
  46. if (slots.cancel || props.cancelText) {
  47. return [_createVNode("div", {
  48. "class": bem("gap")
  49. }, null), _createVNode("button", {
  50. "type": "button",
  51. "class": bem("cancel"),
  52. "onClick": onCancel
  53. }, [slots.cancel ? slots.cancel() : props.cancelText])];
  54. }
  55. };
  56. const renderIcon = (action) => {
  57. if (action.icon) {
  58. return _createVNode(Icon, {
  59. "class": bem("item-icon"),
  60. "name": action.icon
  61. }, null);
  62. }
  63. };
  64. const renderActionContent = (action, index) => {
  65. if (action.loading) {
  66. return _createVNode(Loading, {
  67. "class": bem("loading-icon")
  68. }, null);
  69. }
  70. if (slots.action) {
  71. return slots.action({
  72. action,
  73. index
  74. });
  75. }
  76. return [_createVNode("span", {
  77. "class": bem("name")
  78. }, [action.name]), action.subname && _createVNode("div", {
  79. "class": bem("subname")
  80. }, [action.subname])];
  81. };
  82. const renderAction = (action, index) => {
  83. const {
  84. color,
  85. loading,
  86. callback,
  87. disabled,
  88. className
  89. } = action;
  90. const onClick = () => {
  91. if (disabled || loading) {
  92. return;
  93. }
  94. if (callback) {
  95. callback(action);
  96. }
  97. if (props.closeOnClickAction) {
  98. updateShow(false);
  99. }
  100. nextTick(() => emit("select", action, index));
  101. };
  102. return _createVNode("button", {
  103. "type": "button",
  104. "style": {
  105. color
  106. },
  107. "class": [bem("item", {
  108. loading,
  109. disabled
  110. }), className],
  111. "onClick": onClick
  112. }, [renderIcon(action), renderActionContent(action, index)]);
  113. };
  114. const renderDescription = () => {
  115. if (props.description || slots.description) {
  116. const content = slots.description ? slots.description() : props.description;
  117. return _createVNode("div", {
  118. "class": bem("description")
  119. }, [content]);
  120. }
  121. };
  122. return () => _createVNode(Popup, _mergeProps({
  123. "class": bem(),
  124. "position": "bottom",
  125. "onUpdate:show": updateShow
  126. }, pick(props, popupInheritKeys)), {
  127. default: () => {
  128. var _a;
  129. return [renderHeader(), renderDescription(), _createVNode("div", {
  130. "class": bem("content")
  131. }, [props.actions.map(renderAction), (_a = slots.default) == null ? void 0 : _a.call(slots)]), renderCancel()];
  132. }
  133. });
  134. }
  135. });
  136. export {
  137. actionSheetProps,
  138. stdin_default as default
  139. };