u-action-sheet.vue 5.1 KB

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