CusDialog.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <el-dialog
  3. :style="`
  4. --cus-dialog_height: ${height};
  5. --cus-dialog_max-height: ${maxHeight};
  6. --cus-dialog_min-height: ${minHeight};
  7. `"
  8. :modal-class="`
  9. __cus-dialog ${maxHeight === 'unset' ? '' : '__cus-dialog-auto-height'} ${hiddenStyle ? '__cus-dialog-hidden-style' : ''}
  10. `"
  11. v-bind="$attrs"
  12. v-model="showDialog"
  13. :title="title"
  14. :width="width"
  15. align-center
  16. :before-close="beforeClose"
  17. :show-close="false"
  18. :append-to-body="$util.isValue($attrs.appendToBody) ? $attrs.appendToBody : true"
  19. :close-on-click-modal="closeOnClickModal"
  20. :close-on-press-escape="closeOnPressEscape"
  21. >
  22. <template #header>
  23. <div class="_cus-dialog-head" v-if="title">
  24. <div class="__cdh-title">{{title}}</div>
  25. <div class="__cdh-slot">
  26. <slot name="head"/>
  27. </div>
  28. <div class="__cdh-close __hover" @click="CDClose()">
  29. <SvgIcon name="close_3" size="16" color="#ffffff"/>
  30. </div>
  31. </div>
  32. </template>
  33. <div class="__cus-dialog-main" :class="{isFull: full !== false}" v-loading="loading">
  34. <div class="__cus-dialog-content">
  35. <slot/>
  36. </div>
  37. <div class="__cus-dialog-foot" :style="`justify-content: ${footAlign};padding: ${footPadding};`" v-if="showSubmit || showClose || $slots.foot">
  38. <slot name="foot" :close="CDClose" :submit="CDSubmit"/>
  39. <template v-if="footAlign === 'center'">
  40. <div v-if="showSubmit" class="__cus-dialog-foot_submit __hover" @click="CDSubmit">{{submitText}}</div>
  41. <div v-if="showClose" class="__cus-dialog-foot_cancel __hover" @click="CDClose()">{{closeText}}</div>
  42. </template>
  43. <template v-else>
  44. <div v-if="showClose" class="__cus-dialog-foot_cancel __hover" @click="CDClose()">{{closeText}}</div>
  45. <div v-if="showSubmit" class="__cus-dialog-foot_submit __hover" @click="CDSubmit">{{submitText}}</div>
  46. </template>
  47. </div>
  48. </div>
  49. </el-dialog>
  50. </template>
  51. <script lang="ts">
  52. import {
  53. defineComponent,
  54. computed,
  55. onMounted,
  56. ref,
  57. reactive,
  58. watch,
  59. getCurrentInstance,
  60. ComponentInternalInstance,
  61. toRefs,
  62. nextTick
  63. } from 'vue'
  64. import {ElMessage, ElMessageBox} from "element-plus";
  65. export default defineComponent({
  66. name: 'CusDialog',
  67. components: {},
  68. props: {
  69. show: {required: true, type: Boolean},
  70. title: {default: ''},
  71. width: {default: '50%'},
  72. full: {default: false},
  73. submitText: {default: '确定'},
  74. closeText: {default: '取消'},
  75. showClose: {default: true},
  76. showSubmit: {default: true},
  77. footAlign: {default: 'center'},
  78. footPadding: {default: '16px 26px'},
  79. height: {default: '60%'},
  80. maxHeight: {default: 'unset'},
  81. minHeight: {default: 'unset'},
  82. closeOnClickModal: {default: false},
  83. closeOnPressEscape: {default: false},
  84. closeConfirm: {default: false},
  85. closeConfirmText: {default: {
  86. title: null,
  87. message: null,
  88. submit: null,
  89. close: null,
  90. }},
  91. loading: {
  92. default: false,
  93. type: Boolean
  94. },
  95. hiddenStyle: {
  96. default: false
  97. }
  98. },
  99. setup(props, {emit}) {
  100. const that = (getCurrentInstance() as ComponentInternalInstance).appContext.config.globalProperties
  101. const state = reactive({
  102. showDialog: false,
  103. closeConfirmTextTemp: {
  104. title: '提示',
  105. message: '请确认是否关闭?',
  106. submit: '确定',
  107. close: '取消',
  108. }
  109. })
  110. watch(() => props.show, (n) => {
  111. state.showDialog = n
  112. })
  113. const beforeClose = (done) => {
  114. CDClose(done)
  115. }
  116. const closeConfirmTextCpt: any = computed(() => {
  117. return {
  118. title: that.$util.isValue(props.closeConfirmText.title) ? props.closeConfirmText.title : state.closeConfirmTextTemp.title,
  119. message: that.$util.isValue(props.closeConfirmText.message) ? props.closeConfirmText.message : state.closeConfirmTextTemp.message,
  120. submit: that.$util.isValue(props.closeConfirmText.submit) ? props.closeConfirmText.submit : state.closeConfirmTextTemp.submit,
  121. close: that.$util.isValue(props.closeConfirmText.close) ? props.closeConfirmText.close : state.closeConfirmTextTemp.close,
  122. }
  123. })
  124. const CDClose = async (done = () => {}) => {
  125. if (props.closeConfirm !== false) {
  126. await ElMessageBox.confirm(closeConfirmTextCpt.value.message, closeConfirmTextCpt.value.title, {
  127. confirmButtonText: closeConfirmTextCpt.value.submit,
  128. cancelButtonText: closeConfirmTextCpt.value.close,
  129. type: "warning",
  130. }).then(() => {
  131. emit('update:show', false)
  132. emit('onClose')
  133. done()
  134. }).catch(() => {})
  135. } else {
  136. emit('update:show', false)
  137. emit('onClose')
  138. done()
  139. }
  140. }
  141. const CDSubmit = () => {
  142. emit('onSubmit')
  143. }
  144. onMounted(() => {
  145. state.showDialog = props.show
  146. })
  147. return {
  148. ...toRefs(state),
  149. beforeClose,
  150. CDClose,
  151. CDSubmit,
  152. }
  153. },
  154. })
  155. </script>
  156. <style scoped lang="scss">
  157. </style>