index.jsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { defineComponent, ref, computed } from 'vue'
  2. import { ElDialog, ElButton } from 'element-plus'
  3. import CipFormValidate from '@cip/components/cip-form-validate'
  4. import { componentScheme } from './component.scheme'
  5. import { generateProps, generateEmits } from '../helper/component-util'
  6. import { getUsingConfig } from '@cip/utils/util'
  7. import './dialog.less'
  8. const sizeConfig = {
  9. mini: '374px',
  10. small: '568px',
  11. default: '860px',
  12. large: '1144px'
  13. }
  14. export default defineComponent({
  15. name: 'CipDialog',
  16. props: generateProps(componentScheme),
  17. emits: generateEmits(componentScheme),
  18. setup (props, { slots, emit }) {
  19. const formValidateRef = ref()
  20. const waiting = ref(false)
  21. // 默认使用props.onConfirm当入参为函数时使用函数
  22. const confirm = async (cb) => {
  23. // 防止cb为e导致的错误
  24. if (typeof cb !== 'function') cb = props.onConfirm
  25. try {
  26. waiting.value = true
  27. await formValidateRef.value.validate()
  28. const res = await new Promise((resolve, reject) => {
  29. if (typeof cb === 'function') {
  30. cb(resolve, reject)
  31. } else {
  32. reject(new TypeError('onConfirm is not a function'))
  33. }
  34. })
  35. updateVisible(false)
  36. return res ?? true
  37. } finally {
  38. waiting.value = false
  39. }
  40. }
  41. const cancel = () => {
  42. updateVisible(false)
  43. emit('cancel')
  44. }
  45. const openHandler = () => {
  46. formValidateRef.value?.clear()
  47. }
  48. const closeHandler = () => {
  49. emit('close')
  50. }
  51. const updateVisible = (val) => {
  52. emit('update:modelValue', val)
  53. }
  54. const width = computed(() => {
  55. return getUsingConfig(props.width, sizeConfig[props.size], sizeConfig.default)
  56. })
  57. return () => (
  58. <ElDialog
  59. class={['cip-dialog__wrapper', { 'cip-dialog--show-only': props.showOnly }]}
  60. modelValue={props.modelValue}
  61. onUpdate:modelValue={updateVisible}
  62. title={props.title}
  63. width={width.value}
  64. closeOnClickModal={props.closeOnClickModal}
  65. top={props.top}
  66. destroyOnClose={props.destroyOnClose}
  67. appendToBody={true}
  68. onClose={() => closeHandler()}
  69. onOpen={() => openHandler()}
  70. fullscreen={props.fullscreen}
  71. >
  72. {{
  73. header: () => <>
  74. <div class="el-dialog__mainTitle">{ slots.mainTitle?.() || props.title }</div>
  75. <div class="el-dialog__subTitle">{slots.subTitle?.() || props.subTitle}</div>
  76. </>,
  77. default: () => <CipFormValidate ref={formValidateRef}>
  78. {slots.default?.()}
  79. </CipFormValidate>,
  80. footer: props.showOnly && !slots.footer
  81. ? undefined
  82. : () => {
  83. if (!props.showOnly) {
  84. const defaultFooter = <div>
  85. {
  86. !waiting.value && props.showCancel &&
  87. <ElButton
  88. onClick={() => cancel()}
  89. size={props.buttonSize}>
  90. {props.cancelText}
  91. </ElButton>
  92. }
  93. <ElButton
  94. onClick={() => confirm()}
  95. size={props.buttonSize}
  96. type={'primary'}
  97. loading={waiting.value} >
  98. {props.confirmText}
  99. </ElButton>
  100. </div>
  101. const footerSlot = slots.footer?.({ confirm, loading: waiting.value, cancel })
  102. return slots.footer ? footerSlot : defaultFooter
  103. } else {
  104. return slots.footer?.()
  105. }
  106. }
  107. }}
  108. </ElDialog>
  109. )
  110. }
  111. })