u-modal.vue 7.2 KB

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