u-action-sheet.vue 11 KB

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