u-action-sheet.vue 6.2 KB

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