u-modal.vue 6.8 KB

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