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