u-modal.vue 7.0 KB

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