usePopup.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { ref } from 'vue'
  2. import { useI18n } from 'vue-i18n'
  3. interface PopupOptions {
  4. title?: string
  5. content?: string
  6. cancelText?: string
  7. confirmText?: string
  8. width?: number
  9. showCancel?: boolean
  10. // confirmBtnType?: 'primary' | 'danger'
  11. }
  12. interface PopupState {
  13. visible: boolean
  14. title: string
  15. content: string
  16. cancelText: string
  17. confirmText: string
  18. showCancel: boolean
  19. // confirmBtnType: 'primary' | 'danger'
  20. resolve: ((value: boolean) => void) | null
  21. }
  22. const popupState = ref<PopupState>({
  23. visible: false,
  24. title: '',
  25. content: '',
  26. cancelText: '',
  27. confirmText: '',
  28. showCancel: true,
  29. // confirmBtnType: 'primary',
  30. resolve: null
  31. })
  32. export function usePopup() {
  33. const { t } = useI18n()
  34. const confirm = (options: PopupOptions = {}): Promise<boolean> => {
  35. return new Promise((resolve) => {
  36. console.log(options,'dia')
  37. popupState.value = {
  38. visible: true,
  39. title: options.title || t('Msg.SystemPrompt'),
  40. content: options.content || '',
  41. showCancel: options.showCancel != undefined ? options.showCancel: true,
  42. cancelText: options.cancelText || t('Btn.Cancel'),
  43. confirmText: options.confirmText || t('Btn.Confirm'),
  44. // confirmBtnType: options.confirmBtnType || 'primary',
  45. resolve
  46. }
  47. })
  48. }
  49. const close = () => {
  50. if (popupState.value.resolve) {
  51. popupState.value.resolve(false)
  52. popupState.value.resolve = null
  53. }
  54. popupState.value.visible = false
  55. }
  56. const handleConfirm = () => {
  57. if (popupState.value.resolve) {
  58. popupState.value.resolve(true)
  59. popupState.value.resolve = null
  60. }
  61. popupState.value.visible = false
  62. }
  63. return {
  64. popupState,
  65. confirm,
  66. close,
  67. handleConfirm
  68. }
  69. }