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.

u-action-sheet.vue 5.0 KiB

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