You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
9.9 KiB

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