u-modal.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <u-popup
  3. mode="center"
  4. :zoom="zoom"
  5. :show="show"
  6. :class="[customClass]"
  7. :customStyle="{
  8. borderRadius: '6px',
  9. overflow: 'hidden',
  10. marginTop: `-${addUnit(negativeTop)}`
  11. }"
  12. :closeOnClickOverlay="closeOnClickOverlay"
  13. :safeAreaInsetBottom="false"
  14. :duration="duration"
  15. @click="clickHandler"
  16. >
  17. <view
  18. class="u-modal"
  19. :style="{
  20. width: addUnit(width),
  21. }"
  22. >
  23. <view
  24. class="u-modal__title"
  25. v-if="title"
  26. >{{ title }}</view>
  27. <view
  28. class="u-modal__content"
  29. :style="contentStyleCpu"
  30. >
  31. <slot>
  32. <text class="u-modal__content__text" :style="{textAlign: contentTextAlign}">
  33. {{ content }}
  34. </text>
  35. </slot>
  36. </view>
  37. <view
  38. class="u-modal__button-group--confirm-button"
  39. v-if="$slots.confirmButton"
  40. >
  41. <slot name="confirmButton"></slot>
  42. </view>
  43. <template v-else>
  44. <u-line></u-line>
  45. <view
  46. class="u-modal__button-group"
  47. :style="{
  48. flexDirection: buttonReverse ? 'row-reverse' : 'row'
  49. }"
  50. >
  51. <view
  52. class="u-modal__button-group__wrapper u-modal__button-group__wrapper--cancel"
  53. :hover-stay-time="150"
  54. hover-class="u-modal__button-group__wrapper--hover"
  55. :class="[showCancelButton && !showConfirmButton && 'u-modal__button-group__wrapper--only-cancel']"
  56. v-if="showCancelButton"
  57. @tap="cancelHandler"
  58. >
  59. <text
  60. class="u-modal__button-group__wrapper__text"
  61. :style="{
  62. color: cancelColor
  63. }"
  64. >{{ cancelText }}</text>
  65. </view>
  66. <u-line
  67. direction="column"
  68. v-if="showConfirmButton && showCancelButton"
  69. ></u-line>
  70. <view
  71. class="u-modal__button-group__wrapper u-modal__button-group__wrapper--confirm"
  72. :hover-stay-time="150"
  73. hover-class="u-modal__button-group__wrapper--hover"
  74. :class="[!showCancelButton && showConfirmButton && 'u-modal__button-group__wrapper--only-confirm']"
  75. v-if="showConfirmButton"
  76. @tap="confirmHandler"
  77. >
  78. <u-loading-icon v-if="loading"></u-loading-icon>
  79. <text
  80. v-else
  81. class="u-modal__button-group__wrapper__text"
  82. :style="{
  83. color: confirmColor
  84. }"
  85. >{{ confirmText }}</text>
  86. </view>
  87. </view>
  88. </template>
  89. </view>
  90. <template #bottom>
  91. <slot name="popupBottom"></slot>
  92. </template>
  93. </u-popup>
  94. </template>
  95. <script>
  96. import { props } from './props';
  97. import { mpMixin } from '../../libs/mixin/mpMixin';
  98. import { mixin } from '../../libs/mixin/mixin';
  99. import { addUnit } from '../../libs/function/index';
  100. /**
  101. * Modal 模态框
  102. * @description 弹出模态框,常用于消息提示、消息确认、在当前页面内完成特定的交互操作。
  103. * @tutorial https://ijry.github.io/uview-plus/components/modul.html
  104. * @property {Boolean} show 是否显示模态框,请赋值给show (默认 false )
  105. * @property {String} title 标题内容
  106. * @property {String} content 模态框内容,如传入slot内容,则此参数无效
  107. * @property {String} confirmText 确认按钮的文字 (默认 '确认' )
  108. * @property {String} cancelText 取消按钮的文字 (默认 '取消' )
  109. * @property {Boolean} showConfirmButton 是否显示确认按钮 (默认 true )
  110. * @property {Boolean} showCancelButton 是否显示取消按钮 (默认 false )
  111. * @property {String} confirmColor 确认按钮的颜色 (默认 '#2979ff' )
  112. * @property {String} cancelColor 取消按钮的颜色 (默认 '#606266' )
  113. * @property {Boolean} buttonReverse 对调确认和取消的位置 (默认 false )
  114. * @property {Boolean} zoom 是否开启缩放模式 (默认 true )
  115. * @property {Boolean} asyncClose 是否异步关闭,只对确定按钮有效,见上方说明 (默认 false )
  116. * @property {Boolean} closeOnClickOverlay 是否允许点击遮罩关闭Modal (默认 false )
  117. * @property {String | Number} negativeTop 往上偏移的值,给一个负的margin-top,往上偏移,避免和键盘重合的情况,单位任意,数值则默认为px单位 (默认 0 )
  118. * @property {String | Number} width modal宽度,不支持百分比,可以数值,px,rpx单位 (默认 '650rpx' )
  119. * @property {String} confirmButtonShape 确认按钮的样式,如设置,将不会显示取消按钮
  120. * @property {Number} duration 弹窗动画过度时间 (默认 400 )
  121. * @event {Function} confirm 点击确认按钮时触发
  122. * @event {Function} cancel 点击取消按钮时触发
  123. * @event {Function} close 点击遮罩关闭出发,closeOnClickOverlay为true有效
  124. * @example <u-modal :show="show" />
  125. */
  126. export default {
  127. name: 'u-modal',
  128. mixins: [mpMixin, mixin, props],
  129. data() {
  130. return {
  131. loading: false
  132. }
  133. },
  134. watch: {
  135. show(n) {
  136. // 为了避免第一次打开modal,又使用了异步关闭的loading
  137. // 第二次打开modal时,loading依然存在的情况
  138. if (n && this.loading) this.loading = false
  139. }
  140. },
  141. emits: ["confirm", "cancel", "close", "update:show", 'cancelOnAsync'],
  142. computed: {
  143. contentStyleCpu() {
  144. let style = this.contentStyle;
  145. style.paddingTop = `${this.title ? 12 : 25}px`
  146. return style;
  147. }
  148. },
  149. methods: {
  150. addUnit,
  151. // 点击确定按钮
  152. confirmHandler() {
  153. // 如果配置了异步关闭,将按钮值为loading状态
  154. if (this.asyncClose) {
  155. this.loading = true;
  156. } else {
  157. this.$emit('update:show', false)
  158. }
  159. this.$emit('confirm')
  160. },
  161. // 点击取消按钮
  162. cancelHandler() {
  163. // 如果点击了确定按钮,确定按钮正在请求接口执行异步操作,那么限制不能取消。
  164. if (this.asyncClose && this.loading) {
  165. if (this.asyncCloseTip) {
  166. uni.showToast({
  167. title: this.asyncCloseTip,
  168. icon: 'none'
  169. });
  170. }
  171. this.$emit('cancelOnAsync')
  172. } else {
  173. // 如果配置了取消时异步关闭
  174. if (!this.asyncCancelClose) {
  175. this.$emit('update:show', false)
  176. }
  177. }
  178. this.$emit('cancel')
  179. },
  180. // 点击遮罩
  181. // 从原理上来说,modal的遮罩点击,并不是真的点击到了遮罩
  182. // 因为modal依赖于popup的中部弹窗类型,中部弹窗比较特殊,虽有然遮罩,但是为了让弹窗内容能flex居中
  183. // 多了一个透明的遮罩,此透明的遮罩会覆盖在灰色的遮罩上,所以实际上是点击不到灰色遮罩的,popup内部在
  184. // 透明遮罩的子元素做了.stop处理,所以点击内容区,也不会导致误触发
  185. clickHandler() {
  186. if (this.closeOnClickOverlay) {
  187. this.$emit('update:show', false)
  188. this.$emit('close')
  189. }
  190. }
  191. }
  192. }
  193. </script>
  194. <style lang="scss" scoped>
  195. $u-modal-border-radius: 6px;
  196. .u-modal {
  197. width: 650rpx;
  198. border-radius: $u-modal-border-radius;
  199. overflow: hidden;
  200. &__title {
  201. display: flex;
  202. flex-direction: column;
  203. justify-content: center;
  204. align-items: center;
  205. font-size: 16px;
  206. font-weight: bold;
  207. color: $u-content-color;
  208. text-align: center;
  209. padding-top: 25px;
  210. }
  211. &__content {
  212. padding: 12px 25px 25px 25px;
  213. @include flex;
  214. justify-content: center;
  215. &__text {
  216. font-size: 15px;
  217. color: $u-content-color;
  218. flex: 1;
  219. }
  220. }
  221. &__button-group {
  222. @include flex;
  223. &--confirm-button {
  224. flex-direction: column;
  225. padding: 0px 25px 15px 25px;
  226. }
  227. &__wrapper {
  228. flex: 1;
  229. @include flex;
  230. justify-content: center;
  231. align-items: center;
  232. height: 48px;
  233. &--confirm,
  234. &--only-cancel {
  235. border-bottom-right-radius: $u-modal-border-radius;
  236. }
  237. &--cancel,
  238. &--only-confirm {
  239. border-bottom-left-radius: $u-modal-border-radius;
  240. }
  241. &--hover {
  242. background-color: $u-bg-color;
  243. }
  244. &__text {
  245. color: $u-content-color;
  246. font-size: 16px;
  247. text-align: center;
  248. }
  249. }
  250. }
  251. }
  252. </style>