u-action-sheet.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <u-popup v-model="value" :border-radius="borderRadius" :maskCloseAble="maskCloseAble" :popup="false" :safeAreaInsetBottom="safeAreaInsetBottom"
  3. :z-index="uZIndex" length="auto" mode="bottom" @close="popupClose">
  4. <view v-if="tips.text" :style="[tipsStyle]" class="u-tips u-border-bottom">
  5. {{ tips.text }}
  6. </view>
  7. <block v-for="(item, index) in list" :key="index">
  8. <view
  9. :class="[index < list.length - 1 ? 'u-border-bottom' : '']"
  10. :hover-stay-time="150"
  11. :style="[itemStyle(index)]"
  12. class="u-action-sheet-item u-line-1"
  13. @tap="itemClick(index)"
  14. @touchmove.stop.prevent
  15. >
  16. <text>{{ item.text }}</text>
  17. <text v-if="item.subText" class="u-action-sheet-item__subtext u-line-1">{{ item.subText }}</text>
  18. </view>
  19. </block>
  20. <view v-if="cancelBtn" class="u-gab">
  21. </view>
  22. <view v-if="cancelBtn" :hover-stay-time="150" class="u-actionsheet-cancel u-action-sheet-item"
  23. hover-class="u-hover-class" @tap="close" @touchmove.stop.prevent>{{ cancelText }}
  24. </view>
  25. </u-popup>
  26. </template>
  27. <script>
  28. /**
  29. * actionSheet 操作菜单
  30. * @description 本组件用于从底部弹出一个操作菜单,供用户选择并返回结果。本组件功能类似于uni的uni.showActionSheetAPI,配置更加灵活,所有平台都表现一致。
  31. * @tutorial https://www.uviewui.com/components/actionSheet.html
  32. * @property {Array<Object>} list 按钮的文字数组,见官方文档示例
  33. * @property {Object} tips 顶部的提示文字,见官方文档示例
  34. * @property {String} cancel-text 取消按钮的提示文字
  35. * @property {Boolean} cancel-btn 是否显示底部的取消按钮(默认true)
  36. * @property {Number String} border-radius 弹出部分顶部左右的圆角值,单位rpx(默认0)
  37. * @property {Boolean} mask-close-able 点击遮罩是否可以关闭(默认true)
  38. * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false)
  39. * @property {Number String} z-index z-index值(默认1075)
  40. * @property {String} cancel-text 取消按钮的提示文字
  41. * @event {Function} click 点击ActionSheet列表项时触发
  42. * @event {Function} close 点击取消按钮时触发
  43. * @example <u-action-sheet :list="list" @click="click" v-model="show"></u-action-sheet>
  44. */
  45. export default {
  46. name: "u-action-sheet",
  47. props: {
  48. // 点击遮罩是否可以关闭actionsheet
  49. maskCloseAble: {
  50. type: Boolean,
  51. default: true
  52. },
  53. // 按钮的文字数组,可以自定义颜色和字体大小,字体单位为rpx
  54. list: {
  55. type: Array,
  56. default() {
  57. // 如下
  58. // return [{
  59. // text: '确定',
  60. // color: '',
  61. // fontSize: ''
  62. // }]
  63. return [];
  64. }
  65. },
  66. // 顶部的提示文字
  67. tips: {
  68. type: Object,
  69. default() {
  70. return {
  71. text: '',
  72. color: '',
  73. fontSize: '26'
  74. }
  75. }
  76. },
  77. // 底部的取消按钮
  78. cancelBtn: {
  79. type: Boolean,
  80. default: true
  81. },
  82. // 是否开启底部安全区适配,开启的话,会在iPhoneX机型底部添加一定的内边距
  83. safeAreaInsetBottom: {
  84. type: Boolean,
  85. default: false
  86. },
  87. // 通过双向绑定控制组件的弹出与收起
  88. value: {
  89. type: Boolean,
  90. default: false
  91. },
  92. // 弹出的顶部圆角值
  93. borderRadius: {
  94. type: [String, Number],
  95. default: 0
  96. },
  97. // 弹出的z-index值
  98. zIndex: {
  99. type: [String, Number],
  100. default: 0
  101. },
  102. // 取消按钮的文字提示
  103. cancelText: {
  104. type: String,
  105. default: '取消'
  106. }
  107. },
  108. computed: {
  109. // 顶部提示的样式
  110. tipsStyle() {
  111. let style = {};
  112. if (this.tips.color) style.color = this.tips.color;
  113. if (this.tips.fontSize) style.fontSize = this.tips.fontSize + 'rpx';
  114. return style;
  115. },
  116. // 操作项目的样式
  117. itemStyle() {
  118. return (index) => {
  119. let style = {};
  120. if (this.list[index].color) style.color = this.list[index].color;
  121. if (this.list[index].fontSize) style.fontSize = this.list[index].fontSize + 'rpx';
  122. // 选项被禁用的样式
  123. if (this.list[index].disabled) style.color = '#c0c4cc';
  124. return style;
  125. }
  126. },
  127. uZIndex() {
  128. // 如果用户有传递z-index值,优先使用
  129. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  130. }
  131. },
  132. methods: {
  133. // 点击取消按钮
  134. close() {
  135. // 发送input事件,并不会作用于父组件,而是要设置组件内部通过props传递的value参数
  136. // 这是一个vue发送事件的特殊用法
  137. this.popupClose();
  138. this.$emit('close');
  139. },
  140. // 弹窗关闭
  141. popupClose() {
  142. this.$emit('input', false);
  143. },
  144. // 点击某一个item
  145. itemClick(index) {
  146. // disabled的项禁止点击
  147. if (this.list[index].disabled) return;
  148. this.$emit('click', index);
  149. this.$emit('input', false);
  150. }
  151. }
  152. }
  153. </script>
  154. <style lang="scss" scoped>
  155. @import "../../libs/css/style.components.scss";
  156. .u-tips {
  157. font-size: 26 rpx;
  158. text-align: center;
  159. padding: 34 rpx 0;
  160. line-height: 1;
  161. color: $u-tips-color;
  162. }
  163. .u-action-sheet-item {
  164. @include vue-flex;;
  165. line-height: 1;
  166. justify-content: center;
  167. align-items: center;
  168. font-size: 32 rpx;
  169. padding: 34 rpx 0;
  170. flex-direction: column;
  171. }
  172. .u-action-sheet-item__subtext {
  173. font-size: 24 rpx;
  174. color: $u-tips-color;
  175. margin-top: 20 rpx;
  176. }
  177. .u-gab {
  178. height: 12 rpx;
  179. background-color: rgb(234, 234, 236);
  180. }
  181. .u-actionsheet-cancel {
  182. color: $u-main-color;
  183. }
  184. </style>