u-action-sheet.vue 5.9 KB

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