u-modal.vue 7.9 KB

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