u-modal.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <view>
  3. <u-popup :blur="blur" :zoom="zoom" mode="center" :popup="false" :z-index="uZIndex" v-model="popupValue" :length="width"
  4. :mask-close-able="maskCloseAble" :border-radius="borderRadius" @close="popupClose" :negative-top="negativeTop">
  5. <view class="u-model">
  6. <view v-if="showTitle" class="u-model__title u-line-1" :style="[titleStyle]">{{ title }}</view>
  7. <view class="u-model__content">
  8. <view :style="[contentStyle]" v-if="$slots.default || $slots.$default">
  9. <slot />
  10. </view>
  11. <view v-else class="u-model__content__message" :style="[contentStyle]">{{ content }}</view>
  12. </view>
  13. <view class="u-model__footer u-border-top" v-if="showCancelButton || showConfirmButton">
  14. <view v-if="showCancelButton" :hover-stay-time="100" hover-class="u-model__btn--hover" class="u-model__footer__button"
  15. :style="[cancelBtnStyle]" @tap="cancel">
  16. {{cancelText}}
  17. </view>
  18. <view v-if="showConfirmButton || $slots['confirm-button']" :hover-stay-time="100" :hover-class="asyncClose ? 'none' : 'u-model__btn--hover'"
  19. class="u-model__footer__button hairline-left" :style="[confirmBtnStyle]" @tap="confirm">
  20. <slot v-if="$slots['confirm-button']" name="confirm-button"></slot>
  21. <block v-else>
  22. <u-loading mode="circle" :color="confirmColor" v-if="loading"></u-loading>
  23. <block v-else>
  24. {{confirmText}}
  25. </block>
  26. </block>
  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. cancelBtnStyle() {
  198. return Object.assign({
  199. color: this.cancelColor
  200. }, this.cancelStyle);
  201. },
  202. confirmBtnStyle() {
  203. return Object.assign({
  204. color: this.confirmColor
  205. }, this.confirmStyle);
  206. },
  207. uZIndex() {
  208. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  209. }
  210. },
  211. watch: {
  212. // 如果是异步关闭时,外部修改v-model的值为false时,重置内部的loading状态
  213. // 避免下次打开的时候,状态混乱
  214. value(n) {
  215. if (n === true) this.loading = false;
  216. this.popupValue = n;
  217. },
  218. modelValue(n) {
  219. if (n === true) this.loading = false;
  220. this.popupValue = n;
  221. }
  222. },
  223. methods: {
  224. getValue(){
  225. // #ifndef VUE3
  226. return this.value;
  227. // #endif
  228. // #ifdef VUE3
  229. return this.modelValue;
  230. // #endif
  231. },
  232. confirm() {
  233. // 异步关闭
  234. if (this.asyncClose) {
  235. this.loading = true;
  236. } else {
  237. this.$emit('input', false);
  238. this.$emit("update:modelValue", false);
  239. }
  240. this.$emit('confirm');
  241. },
  242. cancel() {
  243. this.$emit('cancel');
  244. this.$emit('input', false);
  245. this.$emit("update:modelValue", false);
  246. // 目前popup弹窗关闭有一个延时操作,此处做一个延时
  247. // 避免确认按钮文字变成了"确定"字样,modal还没消失,造成视觉不好的效果
  248. setTimeout(() => {
  249. this.loading = false;
  250. }, 300);
  251. },
  252. // 点击遮罩关闭modal,设置v-model的值为false,否则无法第二次弹起modal
  253. popupClose() {
  254. this.$emit('input', false);
  255. this.$emit("update:modelValue", false);
  256. },
  257. // 清除加载中的状态
  258. clearLoading() {
  259. this.loading = false;
  260. }
  261. }
  262. };
  263. </script>
  264. <style lang="scss" scoped>
  265. @import "../../libs/css/style.components.scss";
  266. .u-model {
  267. height: auto;
  268. overflow: hidden;
  269. font-size: 32rpx;
  270. background-color: #fff;
  271. &__btn--hover {
  272. background-color: rgb(230, 230, 230);
  273. }
  274. &__title {
  275. padding-top: 48rpx;
  276. font-weight: 500;
  277. text-align: center;
  278. color: $u-main-color;
  279. }
  280. &__content {
  281. &__message {
  282. padding: 48rpx;
  283. font-size: 30rpx;
  284. text-align: center;
  285. color: $u-content-color;
  286. }
  287. }
  288. &__footer {
  289. @include vue-flex;
  290. &__button {
  291. flex: 1;
  292. height: 100rpx;
  293. line-height: 100rpx;
  294. font-size: 32rpx;
  295. box-sizing: border-box;
  296. cursor: pointer;
  297. text-align: center;
  298. border-radius: 4rpx;
  299. }
  300. }
  301. }
  302. </style>