CusDialog.vue 4.8 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
  10. ${maxHeight === 'unset' ? '' : '__cus-dialog-auto-height'}
  11. `"
  12. v-bind="$attrs"
  13. v-model="showDialog"
  14. :title="title"
  15. :width="width"
  16. align-center
  17. :before-close="beforeClose"
  18. :show-close="false"
  19. :append-to-body="$attrs['append-to-body'] ? $attrs['append-to-body'] : true"
  20. >
  21. <template #header>
  22. <div class="_cus-dialog-head">
  23. <div class="__cdh-title">{{title}}</div>
  24. <div class="__cdh-slot">
  25. <slot name="head"/>
  26. </div>
  27. <div class="__cdh-close __hover" @click="CDClose()">
  28. <SvgIcon name="close_2" size="14"/>
  29. </div>
  30. </div>
  31. </template>
  32. <div class="__cus-dialog-main" :class="{isFull: full !== false}" v-loading="loading">
  33. <div class="__cus-dialog-content">
  34. <slot/>
  35. </div>
  36. <div class="__cus-dialog-foot" :style="`justify-content: ${footAlign};padding: ${footPadding};`">
  37. <slot name="foot" :close="CDClose" :submit="CDSubmit"/>
  38. <template v-if="footAlign === 'center'">
  39. <div v-if="showSubmit" class="__cus-dialog-foot_submit __hover" @click="CDSubmit">确定</div>
  40. <div v-if="showClose" class="__cus-dialog-foot_cancel __hover" @click="CDClose()">取消</div>
  41. </template>
  42. <template v-else>
  43. <div v-if="showClose" class="__cus-dialog-foot_cancel __hover" @click="CDClose()">取消</div>
  44. <div v-if="showSubmit" class="__cus-dialog-foot_submit __hover" @click="CDSubmit">确定</div>
  45. </template>
  46. </div>
  47. </div>
  48. </el-dialog>
  49. </template>
  50. <script lang="ts">
  51. import {
  52. defineComponent,
  53. computed,
  54. onMounted,
  55. ref,
  56. reactive,
  57. watch,
  58. getCurrentInstance,
  59. ComponentInternalInstance,
  60. toRefs,
  61. nextTick
  62. } from 'vue'
  63. import {useStore} from 'vuex'
  64. import {useRouter, useRoute} from 'vue-router'
  65. import {ElMessage, ElMessageBox} from "element-plus";
  66. export default defineComponent({
  67. name: 'CusDialog',
  68. components: {},
  69. props: {
  70. show: {required: true, type: Boolean},
  71. title: {default: ''},
  72. width: {default: '50%'},
  73. full: {default: false},
  74. submitText: {default: '确定'},
  75. showClose: {default: true},
  76. showSubmit: {default: true},
  77. footAlign: {default: 'center', validator(val: string) {
  78. return ['center', 'right'].includes(val)
  79. }},
  80. footPadding: {default: '24px'},
  81. height: {default: '60%'},
  82. maxHeight: {default: 'unset'},
  83. minHeight: {default: 'unset'},
  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. },
  96. setup(props, {emit}) {
  97. const store = useStore();
  98. const router = useRouter();
  99. const route = useRoute();
  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('close')
  133. done()
  134. }).catch(() => {})
  135. } else {
  136. emit('update:show', false)
  137. emit('close')
  138. done()
  139. }
  140. }
  141. const CDSubmit = () => {
  142. emit('submit')
  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>