u-modal.vue 7.9 KB

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