Dialog.vue 3.3 KB

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