u-modal.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 }}
  26. </text>
  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">{{ content }}</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 }}
  65. </text>
  66. </view>
  67. <u-line
  68. direction="column"
  69. v-if="showConfirmButton && showCancelButton"
  70. ></u-line>
  71. <view
  72. class="u-modal__button-group__wrapper u-modal__button-group__wrapper--confirm"
  73. :hover-stay-time="150"
  74. hover-class="u-modal__button-group__wrapper--hover"
  75. :class="[!showCancelButton && showConfirmButton && 'u-modal__button-group__wrapper--only-confirm']"
  76. v-if="showConfirmButton"
  77. @tap="confirmHandler"
  78. >
  79. <u-loading-icon v-if="loading"></u-loading-icon>
  80. <text
  81. v-else
  82. class="u-modal__button-group__wrapper__text"
  83. :style="{
  84. color: confirmColor
  85. }"
  86. >{{ confirmText }}
  87. </text>
  88. </view>
  89. </view>
  90. </template>
  91. </view>
  92. </u-popup>
  93. </template>
  94. <script>
  95. import props from './props.js';
  96. /**
  97. * Modal 模态框
  98. * @description 弹出模态框,常用于消息提示、消息确认、在当前页面内完成特定的交互操作。
  99. * @tutorial https://www.uviewui.com/components/modul.html
  100. * @property {Boolean} show 是否显示模态框,请赋值给show (默认 false )
  101. * @property {String} title 标题内容
  102. * @property {String} content 模态框内容,如传入slot内容,则此参数无效
  103. * @property {String} confirmText 确认按钮的文字 (默认 '确认' )
  104. * @property {String} cancelText 取消按钮的文字 (默认 '取消' )
  105. * @property {Boolean} showConfirmButton 是否显示确认按钮 (默认 true )
  106. * @property {Boolean} showCancelButton 是否显示取消按钮 (默认 false )
  107. * @property {String} confirmColor 确认按钮的颜色 (默认 '#2979ff' )
  108. * @property {String} cancelColor 取消按钮的颜色 (默认 '#606266' )
  109. * @property {Boolean} buttonReverse 对调确认和取消的位置 (默认 false )
  110. * @property {Boolean} zoom 是否开启缩放模式 (默认 true )
  111. * @property {Boolean} asyncClose 是否异步关闭,只对确定按钮有效,见上方说明 (默认 false )
  112. * @property {Boolean} closeOnClickOverlay 是否允许点击遮罩关闭Modal (默认 false )
  113. * @property {String | Number} negativeTop 往上偏移的值,给一个负的margin-top,往上偏移,避免和键盘重合的情况,单位任意,数值则默认为px单位 (默认 0 )
  114. * @property {String | Number} width modal宽度,不支持百分比,可以数值,px,rpx单位 (默认 '650rpx' )
  115. * @property {String} confirmButtonShape 确认按钮的样式,如设置,将不会显示取消按钮
  116. * @event {Function} confirm 点击确认按钮时触发
  117. * @event {Function} cancel 点击取消按钮时触发
  118. * @event {Function} close 点击遮罩关闭出发,closeOnClickOverlay为true有效
  119. * @example <u-loadmore :status="status" icon-type="iconType" load-text="loadText" />
  120. */
  121. export default {
  122. name: 'u-modal',
  123. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  124. data() {
  125. return {
  126. loading: false
  127. }
  128. },
  129. watch: {
  130. show(n) {
  131. // 为了避免第一次打开modal,又使用了异步关闭的loading
  132. // 第二次打开modal时,loading依然存在的情况
  133. if (n && this.loading) this.loading = false
  134. }
  135. },
  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: 650 rpx;
  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>