Dialog.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <Popup
  3. v-bind="props"
  4. round
  5. position="center"
  6. @close="onClose"
  7. >
  8. <DialogInner
  9. v-bind="$props"
  10. :onConfirm="$props.onConfirm"
  11. :onCancel="$props.onCancel"
  12. :topSlots="{
  13. default: Boolean($slots?.default),
  14. bottomContent: Boolean($slots?.bottomContent),
  15. title: Boolean($slots?.title),
  16. content: Boolean($slots?.content),
  17. icon: Boolean($slots?.icon),
  18. }"
  19. @close="onClose"
  20. >
  21. <template #default>
  22. <slot />
  23. </template>
  24. <template #icon="{ icon }">
  25. <slot name="icon" :icon="icon" />
  26. </template>
  27. <template #title="{ title }">
  28. <slot name="title" :title="title" />
  29. </template>
  30. <template #content>
  31. <slot name="content" />
  32. </template>
  33. <template #bottomContent="{ onConfirmClick, onCancelClick }">
  34. <slot
  35. name="bottomContent"
  36. :onConfirmClick="onConfirmClick"
  37. :onCancelClick="onCancelClick"
  38. />
  39. </template>
  40. </DialogInner>
  41. </Popup>
  42. </template>
  43. <script setup lang="ts">
  44. import DialogInner from './DialogInner.vue';
  45. import Popup from './Popup.vue';
  46. import type { PopupProps } from './Popup.vue';
  47. export interface DialogProps extends Omit<PopupProps, 'onClose'|'position'|'renderContent'> {
  48. /**
  49. * 对话框的标题
  50. */
  51. title?: string;
  52. /**
  53. * 对话框的图标,显示在标题上方,同 Icon 组件的图标名字。
  54. */
  55. icon?: string;
  56. /**
  57. * 图标的颜色
  58. * @default primary
  59. */
  60. iconColor?: string|undefined;
  61. /**
  62. * 图标大小
  63. * @default 40
  64. */
  65. iconSize?: number;
  66. /**
  67. * 对话框的内容
  68. */
  69. content?: string;
  70. /**
  71. * 对话框内容超高后是否自动滚动
  72. * @default true
  73. */
  74. contentScroll?: boolean;
  75. /**
  76. * 对话框内容自动滚动超高高度,
  77. * @default 75%
  78. */
  79. contentScrollMaxHeight?: number|string,
  80. /**
  81. * 对话框内容框边距。
  82. * 支持数字或者数组: 如果是数字,则设置所有方向边距;两位数组 [vetical,horizontal];四位数组 [top,right,down,left]
  83. * @default [ 15, 20 ]
  84. */
  85. contentPadding?: number|number[],
  86. /**
  87. * 底部按扭是否垂直排版
  88. * @default false
  89. */
  90. bottomVertical?: boolean;
  91. /**
  92. * 取消按扭的文字
  93. * @default 取消
  94. */
  95. cancelText?: string|undefined;
  96. /**
  97. * 确定按扭的文字
  98. * @default 确定
  99. */
  100. confirmText?: string|undefined;
  101. /**
  102. * 确定按扭文字的颜色
  103. * @default primary
  104. */
  105. confirmColor?: string|undefined;
  106. /**
  107. * 取消按扭文字的颜色
  108. * @default text.content
  109. */
  110. cancelColor?: string|undefined;
  111. /**
  112. * 自定义其他按扭,这些按扭将在 cancel 和 confirm 之间显示,建议设置 bottomVertical 使按扭垂直排列。
  113. */
  114. customButtons?: {
  115. name: string,
  116. text: string,
  117. color?: string|undefined,
  118. bold?: boolean,
  119. }[];
  120. /**
  121. * 是否显示取消按扭
  122. * @default false
  123. */
  124. showCancel?: boolean;
  125. /**
  126. * 是否显示确定按扭
  127. * @default true
  128. */
  129. showConfirm?: boolean;
  130. /**
  131. * 对话框宽度
  132. */
  133. width?: number|string|undefined;
  134. /**
  135. * 当对话框点击取消时的回调
  136. */
  137. onCancel?: () => void|Promise<void>;
  138. /**
  139. * 当对话框点击确定的回调
  140. */
  141. onConfirm?: (buttonName?: string) => void|Promise<void>;
  142. }
  143. export type DialogConfirmProps = Omit<DialogProps, 'show'|'showCancel'|'onClose'>;
  144. const props = withDefaults(defineProps<DialogProps>(), {
  145. mask: true,
  146. showConfirm: true,
  147. contentScroll: true,
  148. });
  149. const emit = defineEmits([ 'close', 'update:show' ]);
  150. function onClose() {
  151. emit('close');
  152. emit('update:show', false);
  153. }
  154. </script>