u-modal.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <template>
  2. <view>
  3. <u-popup :blur="blur" :zoom="zoom" mode="center" :popup="false" :z-index="uZIndex" v-model="popupValue"
  4. :length="width" :mask-close-able="maskCloseAble" :border-radius="borderRadius" @close="popupClose"
  5. :negative-top="negativeTop">
  6. <view class="u-model">
  7. <view v-if="showTitle" class="u-model__title u-line-1" :style="[titleStyle]">
  8. {{ title }}
  9. </view>
  10. <view class="u-model__content">
  11. <view :style="[contentStyle]" v-if="$slots.default || $slots.$default">
  12. <slot />
  13. </view>
  14. <view v-else class="u-model__content__message" :style="[contentStyle]">
  15. {{ content }}
  16. </view>
  17. </view>
  18. <view class="u-model__footer u-border-top" v-if="showCancelButton || showConfirmButton">
  19. <view v-if="showCancelButton" :hover-stay-time="100" hover-class="u-model__btn--hover"
  20. class="u-model__footer__button" :style="[cancelBtnStyle]" @tap="cancel">
  21. {{ cancelText }}
  22. </view>
  23. <view v-if="showConfirmButton || $slots['confirm-button']" :hover-stay-time="100"
  24. :hover-class="asyncClose ? 'none' : 'u-model__btn--hover'"
  25. class="u-model__footer__button hairline-left" :style="[confirmBtnStyle]" @tap="confirm">
  26. {{ confirmText }}
  27. </view>
  28. </view>
  29. </view>
  30. </u-popup>
  31. </view>
  32. </template>
  33. <script>
  34. /**
  35. * modal 模态框
  36. * @description 弹出模态框,常用于消息提示、消息确认、在当前页面内完成特定的交互操作
  37. * @tutorial https://www.uviewui.com/components/modal.html
  38. * @property {Boolean} value 是否显示模态框
  39. * @property {String | Number} z-index 层级
  40. * @property {String} title 模态框标题(默认"提示")
  41. * @property {String | Number} width 模态框宽度(默认600)
  42. * @property {String} content 模态框内容(默认"内容")
  43. * @property {Boolean} show-title 是否显示标题(默认true)
  44. * @property {Boolean} async-close 是否异步关闭,只对确定按钮有效(默认false)
  45. * @property {Boolean} show-confirm-button 是否显示确认按钮(默认true)
  46. * @property {Stringr | Number} negative-top modal往上偏移的值
  47. * @property {Boolean} show-cancel-button 是否显示取消按钮(默认false)
  48. * @property {Boolean} mask-close-able 是否允许点击遮罩关闭modal(默认false)
  49. * @property {String} confirm-text 确认按钮的文字内容(默认"确认")
  50. * @property {String} cancel-text 取消按钮的文字内容(默认"取消")
  51. * @property {String} cancel-color 取消按钮的颜色(默认"#606266")
  52. * @property {String} confirm-color 确认按钮的文字内容(默认"#2979ff")
  53. * @property {String | Number} border-radius 模态框圆角值,单位rpx(默认16)
  54. * @property {Object} title-style 自定义标题样式,对象形式
  55. * @property {Object} content-style 自定义内容样式,对象形式
  56. * @property {Object} cancel-style 自定义取消按钮样式,对象形式
  57. * @property {Object} confirm-style 自定义确认按钮样式,对象形式
  58. * @property {Boolean} zoom 是否开启缩放模式(默认true)
  59. * @event {Function} confirm 确认按钮被点击
  60. * @event {Function} cancel 取消按钮被点击
  61. * @example <u-modal :src="title" :content="content"></u-modal>
  62. */
  63. export default {
  64. name: "u-modal",
  65. emits: ["update:modelValue", "input", "confirm", "cancel"],
  66. props: {
  67. // 是否显示Modal
  68. value: {
  69. type: Boolean,
  70. default: false
  71. },
  72. modelValue: {
  73. type: Boolean,
  74. default: false
  75. },
  76. // 层级z-index
  77. zIndex: {
  78. type: [Number, String],
  79. default: ""
  80. },
  81. // 标题
  82. title: {
  83. type: [String],
  84. default: "提示"
  85. },
  86. // 弹窗宽度,可以是数值(rpx),百分比,auto等
  87. width: {
  88. type: [Number, String],
  89. default: 600
  90. },
  91. // 弹窗内容
  92. content: {
  93. type: String,
  94. default: "内容"
  95. },
  96. // 是否显示标题
  97. showTitle: {
  98. type: Boolean,
  99. default: true
  100. },
  101. // 是否显示确认按钮
  102. showConfirmButton: {
  103. type: Boolean,
  104. default: true
  105. },
  106. // 是否显示取消按钮
  107. showCancelButton: {
  108. type: Boolean,
  109. default: false
  110. },
  111. // 确认文案
  112. confirmText: {
  113. type: String,
  114. default: "确认"
  115. },
  116. // 取消文案
  117. cancelText: {
  118. type: String,
  119. default: "取消"
  120. },
  121. // 确认按钮颜色
  122. confirmColor: {
  123. type: String,
  124. default: "#2979ff"
  125. },
  126. // 取消文字颜色
  127. cancelColor: {
  128. type: String,
  129. default: "#606266"
  130. },
  131. // 圆角值
  132. borderRadius: {
  133. type: [Number, String],
  134. default: 16
  135. },
  136. // 标题的样式
  137. titleStyle: {
  138. type: Object,
  139. default () {
  140. return {};
  141. }
  142. },
  143. // 内容的样式
  144. contentStyle: {
  145. type: Object,
  146. default () {
  147. return {};
  148. }
  149. },
  150. // 取消按钮的样式
  151. cancelStyle: {
  152. type: Object,
  153. default () {
  154. return {};
  155. }
  156. },
  157. // 确定按钮的样式
  158. confirmStyle: {
  159. type: Object,
  160. default () {
  161. return {};
  162. }
  163. },
  164. // 是否开启缩放效果
  165. zoom: {
  166. type: Boolean,
  167. default: true
  168. },
  169. // 是否异步关闭,只对确定按钮有效
  170. asyncClose: {
  171. type: Boolean,
  172. default: false
  173. },
  174. // 是否允许点击遮罩关闭modal
  175. maskCloseAble: {
  176. type: Boolean,
  177. default: false
  178. },
  179. // 给一个负的margin-top,往上偏移,避免和键盘重合的情况
  180. negativeTop: {
  181. type: [String, Number],
  182. default: 0
  183. },
  184. // 遮罩的模糊度
  185. blur: {
  186. type: [Number, String],
  187. default: 0
  188. }
  189. },
  190. data() {
  191. return {
  192. loading: false, // 确认按钮是否正在加载中
  193. popupValue: false
  194. };
  195. },
  196. computed: {
  197. valueCom() {
  198. // #ifndef VUE3
  199. return this.value;
  200. // #endif
  201. // #ifdef VUE3
  202. return this.modelValue;
  203. // #endif
  204. },
  205. cancelBtnStyle() {
  206. return Object.assign({
  207. color: this.cancelColor
  208. },
  209. this.cancelStyle
  210. );
  211. },
  212. confirmBtnStyle() {
  213. return Object.assign({
  214. color: this.confirmColor
  215. },
  216. this.confirmStyle
  217. );
  218. },
  219. uZIndex() {
  220. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  221. }
  222. },
  223. watch: {
  224. // 如果是异步关闭时,外部修改v-model的值为false时,重置内部的loading状态
  225. // 避免下次打开的时候,状态混乱
  226. valueCom: {
  227. immediate: true,
  228. handler(n) {
  229. if (n === true) this.loading = false;
  230. this.popupValue = n;
  231. }
  232. }
  233. },
  234. methods: {
  235. confirm() {
  236. // 异步关闭
  237. if (this.asyncClose) {
  238. this.loading = true;
  239. } else {
  240. this.$emit("input", false);
  241. this.$emit("update:modelValue", false);
  242. }
  243. this.$emit("confirm");
  244. },
  245. cancel() {
  246. this.$emit("cancel");
  247. this.$emit("input", false);
  248. this.$emit("update:modelValue", false);
  249. // 目前popup弹窗关闭有一个延时操作,此处做一个延时
  250. // 避免确认按钮文字变成了"确定"字样,modal还没消失,造成视觉不好的效果
  251. setTimeout(() => {
  252. this.loading = false;
  253. }, 300);
  254. },
  255. // 点击遮罩关闭modal,设置v-model的值为false,否则无法第二次弹起modal
  256. popupClose() {
  257. this.$emit("input", false);
  258. this.$emit("update:modelValue", false);
  259. },
  260. // 清除加载中的状态
  261. clearLoading() {
  262. this.loading = false;
  263. }
  264. }
  265. };
  266. </script>
  267. <style lang="scss" scoped>
  268. @import "../../libs/css/style.components.scss";
  269. .u-model {
  270. height: auto;
  271. overflow: hidden;
  272. font-size: 32rpx;
  273. background-color: #fff;
  274. &__btn--hover {
  275. background-color: rgb(230, 230, 230);
  276. }
  277. &__title {
  278. padding-top: 48rpx;
  279. font-weight: 500;
  280. text-align: center;
  281. color: $u-main-color;
  282. }
  283. &__content {
  284. &__message {
  285. padding: 48rpx;
  286. font-size: 30rpx;
  287. text-align: center;
  288. color: $u-content-color;
  289. }
  290. }
  291. &__footer {
  292. @include vue-flex;
  293. &__button {
  294. flex: 1;
  295. height: 100rpx;
  296. line-height: 100rpx;
  297. font-size: 32rpx;
  298. box-sizing: border-box;
  299. cursor: pointer;
  300. text-align: center;
  301. border-radius: 4rpx;
  302. }
  303. }
  304. }
  305. </style>