3
0

u-modal.vue 7.1 KB

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