u-action-sheet.vue 10 KB

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