u-action-sheet.vue 9.5 KB

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