u-action-sheet.vue 10 KB

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