耀实惠小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
7.6 KiB

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