uv-action-sheet.vue 13 KB

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