u-action-sheet.vue 9.6 KB

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