u-action-sheet.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <view>
  3. <view v-if="$slots.trigger || $slots.$trigger" class="u-action-sheet-trigger" @click="open(true)">
  4. <slot name="trigger"></slot>
  5. </view>
  6. <u-popup :show="showPopup"
  7. mode="bottom"
  8. :title="title"
  9. :round="round"
  10. :closeable="closeable"
  11. :titleStyle="Object.assign({ fontWeight: 'bold' }, $u.addStyle(titleStyle))"
  12. :safeAreaInsetBottom="safeAreaInsetBottom"
  13. @close="cancel"
  14. >
  15. <view class="u-action-sheet">
  16. <text class="u-action-sheet__description" :style="[{
  17. marginTop: `${title && description ? 0 : '18px'}`
  18. }]" v-if="description">{{ description }}</text>
  19. <slot>
  20. <u-line v-if="description"></u-line>
  21. <scroll-view class="u-action-sheet__item-wrap" :scroll-y="height !='' || height > 0" :style="[{ maxHeight: $u.addUnit(height) }]">
  22. <view v-for="(item, index) in actions" :key="index">
  23. <!-- #ifdef MP -->
  24. <button
  25. class="u-reset-button"
  26. :key="index"
  27. :openType="item.openType"
  28. :lang="lang"
  29. :session-from="sessionFrom"
  30. :send-message-title="sendMessageTitle"
  31. :send-message-path="sendMessagePath"
  32. :send-message-img="sendMessageImg"
  33. :show-message-card="showMessageCard"
  34. :app-parameter="appParameter"
  35. @tap="selectHandler(index)"
  36. @getuserinfo="onGetUserInfo"
  37. @contact="onContact"
  38. @getphonenumber="onGetPhoneNumber"
  39. @error="onError"
  40. @launchapp="onLaunchApp"
  41. @opensetting="onOpenSetting"
  42. @chooseavatar="onChooseAvatar"
  43. :hover-class="!item.disabled && !item.loading ? 'u-action-sheet--hover' : ''"
  44. >
  45. <!-- #endif -->
  46. <view class="u-action-sheet__item-wrap__item" @tap.stop="selectHandler(index)"
  47. :hover-class="!item.disabled && !item.loading ? 'u-action-sheet--hover' : ''"
  48. :hover-stay-time="150">
  49. <template v-if="!item.loading">
  50. <text class="u-action-sheet__item-wrap__item__name" :style="[itemStyle(index)]">{{
  51. item.name }}</text>
  52. <text v-if="item.subname" class="u-action-sheet__item-wrap__item__subname">{{
  53. item.subname }}</text>
  54. </template>
  55. <u-loading-icon v-else custom-class="van-action-sheet__loading" size="18" mode="circle" />
  56. </view>
  57. <!-- #ifdef MP -->
  58. </button>
  59. <!-- #endif -->
  60. <u-line v-if="index !== actions.length - 1"></u-line>
  61. </view>
  62. </scroll-view>
  63. </slot>
  64. <u-gap bgColor="#eaeaec" height="6" v-if="cancelText"></u-gap>
  65. <view hover-class="u-action-sheet--hover" v-if="cancelText" @tap="cancel">
  66. <text
  67. @touchmove.stop.prevent
  68. :hover-stay-time="150"
  69. class="u-action-sheet__cancel-text"
  70. >{{ cancelText }}</text>
  71. </view>
  72. </view>
  73. </u-popup>
  74. </view>
  75. </template>
  76. <script>
  77. import openType from '../../libs/mixin/openType';
  78. import button from '../../libs/mixin/button';
  79. import props from './props.js';
  80. import mixin from '../../libs/mixin/mixin'
  81. import mpMixin from '../../libs/mixin/mpMixin';
  82. /**
  83. * ActionSheet 操作菜单
  84. * @description 本组件用于从底部弹出一个操作菜单,供用户选择并返回结果。本组件功能类似于uni的uni.showActionSheetAPI,配置更加灵活,所有平台都表现一致。
  85. * @tutorial https://uview.d3u.cn/components/actionSheet.html
  86. *
  87. * @property {Boolean} show 操作菜单是否展示 (默认 false )
  88. * @property {String} title 操作菜单标题
  89. * @property {String} description 选项上方的描述信息
  90. * @property {Array<Object>} actions 按钮的文字数组,见官方文档示例
  91. * @property {String} cancelText 取消按钮的提示文字,不为空时显示按钮
  92. * @property {Boolean} closeOnClickAction 点击某个菜单项时是否关闭弹窗 (默认 true )
  93. * @property {Boolean} safeAreaInsetBottom 处理底部安全区 (默认 true )
  94. * @property {String} openType 小程序的打开方式 (contact | launchApp | getUserInfo | openSetting |getPhoneNumber |error )
  95. * @property {Boolean} closeOnClickOverlay 点击遮罩是否允许关闭 (默认 true )
  96. * @property {Number|String} round 圆角值,默认无圆角 (默认 0 )
  97. * @property {Number|String} height 高度,默认不限制 (默认 '' )
  98. * @property {String} lang 指定返回用户信息的语言,zh_CN 简体中文,zh_TW 繁体中文,en 英文
  99. * @property {String} sessionFrom 会话来源,openType="contact"时有效
  100. * @property {String} sendMessageTitle 会话内消息卡片标题,openType="contact"时有效
  101. * @property {String} sendMessagePath 会话内消息卡片点击跳转小程序路径,openType="contact"时有效
  102. * @property {String} sendMessageImg 会话内消息卡片图片,openType="contact"时有效
  103. * @property {Boolean} showMessageCard 是否显示会话内消息卡片,设置此参数为 true,用户进入客服会话会在右下角显示"可能要发送的小程序"提示,用户点击后可以快速发送小程序消息,openType="contact"时有效 (默认 false )
  104. * @property {String} appParameter 打开 APP 时,向 APP 传递的参数,openType=launchApp 时有效
  105. *
  106. * @event {Function} select 点击ActionSheet列表项时触发
  107. * @event {Function} close 点击取消按钮时触发
  108. * @event {Function} getuserinfo 用户点击该按钮时,会返回获取到的用户信息,回调的 detail 数据与 wx.getUserInfo 返回的一致,openType="getUserInfo"时有效
  109. * @event {Function} contact 客服消息回调,openType="contact"时有效
  110. * @event {Function} getphonenumber 获取用户手机号回调,openType="getPhoneNumber"时有效
  111. * @event {Function} chooseavatar 选择头像回调,openType="chooseAvatar"时有效
  112. * @event {Function} error 当使用开放能力时,发生错误的回调,openType="error"时有效
  113. * @event {Function} launchapp 打开 APP 成功的回调,openType="launchApp"时有效
  114. * @event {Function} opensetting 在打开授权设置页后回调,openType="openSetting"时有效
  115. * @example <u-action-sheet :actions="list" :title="title" :show="show"></u-action-sheet>
  116. */
  117. export default {
  118. name: "u-action-sheet",
  119. // 一些props参数和methods方法,通过mixin混入,因为其他文件也会用到
  120. mixins: [openType, button, mixin,mpMixin, props],
  121. data() {
  122. return {
  123. isTrigger: false,
  124. showPopup: false,
  125. };
  126. },
  127. computed: {
  128. // 操作项目的样式
  129. itemStyle() {
  130. return (index) => {
  131. let style = {};
  132. if (this.actions[index].color) style.color = this.actions[index].color;
  133. if (this.actions[index].fontSize) style.fontSize = uni.$u.addUnit(this.actions[index].fontSize);
  134. // 选项被禁用的样式
  135. if (this.actions[index].disabled) style.color = '#c0c4cc';
  136. return style;
  137. };
  138. },
  139. },
  140. watch: {
  141. show: {
  142. immediate: true,
  143. handler(n) {
  144. this.isTrigger = false
  145. this.showPopup = n
  146. }
  147. }
  148. },
  149. // #ifdef VUE3
  150. emits: ["select", "close"],
  151. // #endif
  152. methods: {
  153. open(isTrigger) {
  154. this.isTrigger = isTrigger
  155. this.showPopup = true
  156. },
  157. // 点击取消按钮
  158. cancel() {
  159. if(this.isTrigger){
  160. this.showPopup = false
  161. }
  162. this.$emit('close');
  163. },
  164. close() {
  165. this.showPopup = false
  166. },
  167. selectHandler(index) {
  168. const item = this.actions[index];
  169. if (item && !item.disabled && !item.loading) {
  170. this.$emit('select', item);
  171. if(this.isTrigger || this.closeOnClickAction){
  172. this.showPopup = false
  173. }
  174. if (this.closeOnClickOverlay) {
  175. this.$emit('close');
  176. }
  177. }
  178. },
  179. }
  180. };
  181. </script>
  182. <style lang="scss" scoped>
  183. @import "../../libs/css/components.scss";
  184. $u-action-sheet-reset-button-width: 100% !default;
  185. $u-action-sheet-description-font-size: 13px !default;
  186. $u-action-sheet-description-color: 14px !default;
  187. $u-action-sheet-description-margin: 18px 15px !default;
  188. $u-action-sheet-item-wrap-item-padding: 15px !default;
  189. $u-action-sheet-item-wrap-name-font-size: 16px !default;
  190. $u-action-sheet-item-wrap-subname-font-size: 13px !default;
  191. $u-action-sheet-item-wrap-subname-color: #c0c4cc !default;
  192. $u-action-sheet-item-wrap-subname-margin-top: 10px !default;
  193. $u-action-sheet-cancel-text-font-size: 16px !default;
  194. $u-action-sheet-cancel-text-color: $u-content-color !default;
  195. $u-action-sheet-cancel-text-font-size: 15px !default;
  196. $u-action-sheet-cancel-text-hover-background-color: rgb(242, 243, 245) !default;
  197. .u-reset-button {
  198. width: $u-action-sheet-reset-button-width;
  199. }
  200. .u-action-sheet {
  201. text-align: center;
  202. &__description {
  203. font-size: $u-action-sheet-description-font-size;
  204. color: $u-tips-color;
  205. margin: $u-action-sheet-description-margin;
  206. text-align: center;
  207. }
  208. &__item-wrap {
  209. &__item {
  210. padding: $u-action-sheet-item-wrap-item-padding;
  211. @include flex;
  212. align-items: center;
  213. justify-content: center;
  214. flex-direction: column;
  215. &__name {
  216. font-size: $u-action-sheet-item-wrap-name-font-size;
  217. color: $u-main-color;
  218. text-align: center;
  219. }
  220. &__subname {
  221. font-size: $u-action-sheet-item-wrap-subname-font-size;
  222. color: $u-action-sheet-item-wrap-subname-color;
  223. margin-top: $u-action-sheet-item-wrap-subname-margin-top;
  224. text-align: center;
  225. }
  226. }
  227. }
  228. &__cancel-text {
  229. font-size: $u-action-sheet-cancel-text-font-size;
  230. color: $u-action-sheet-cancel-text-color;
  231. text-align: center;
  232. padding: $u-action-sheet-cancel-text-font-size;
  233. }
  234. &--hover {
  235. background-color: $u-action-sheet-cancel-text-hover-background-color;
  236. }
  237. }
  238. </style>