u-modal.vue 7.7 KB

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