You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
6.9 KiB

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