useMessage.tsx 3.2 KB
Newer Older
V
vben 已提交
1
import type { ModalFunc, ModalFuncProps } from 'ant-design-vue/lib/modal/Modal';
陈文彬 已提交
2 3 4 5

import { Modal, message as Message, notification } from 'ant-design-vue';
import { InfoCircleFilled, CheckCircleFilled, CloseCircleFilled } from '@ant-design/icons-vue';

V
vben 已提交
6
import { ArgsProps, ConfigProps } from 'ant-design-vue/lib/notification';
V
vben 已提交
7
import { useI18n } from './useI18n';
陈文彬 已提交
8

V
vben 已提交
9 10 11 12 13 14 15 16 17 18
export interface NotifyApi {
  info(config: ArgsProps): void;
  success(config: ArgsProps): void;
  error(config: ArgsProps): void;
  warn(config: ArgsProps): void;
  warning(config: ArgsProps): void;
  open(args: ArgsProps): void;
  close(key: String): void;
  config(options: ConfigProps): void;
  destroy(): void;
V
vben 已提交
19 20 21 22
}

export declare type NotificationPlacement = 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight';
export declare type IconType = 'success' | 'info' | 'error' | 'warning';
V
vben 已提交
23 24 25 26 27
export interface ModalOptionsEx extends Omit<ModalFuncProps, 'iconType'> {
  iconType: 'warning' | 'success' | 'error' | 'info';
}
export type ModalOptionsPartial = Partial<ModalOptionsEx> & Pick<ModalOptionsEx, 'content'>;

V
vben 已提交
28 29 30 31 32 33 34 35
interface ConfirmOptions {
  info: ModalFunc;
  success: ModalFunc;
  error: ModalFunc;
  warn: ModalFunc;
  warning: ModalFunc;
}

陈文彬 已提交
36 37 38 39 40
function getIcon(iconType: string) {
  if (iconType === 'warning') {
    return <InfoCircleFilled class="modal-icon-warning" />;
  } else if (iconType === 'success') {
    return <CheckCircleFilled class="modal-icon-success" />;
V
vben 已提交
41 42
  } else if (iconType === 'info') {
    return <InfoCircleFilled class="modal-icon-info" />;
陈文彬 已提交
43 44 45 46
  } else {
    return <CloseCircleFilled class="modal-icon-error" />;
  }
}
V
vben 已提交
47

陈文彬 已提交
48
function renderContent({ content }: Pick<ModalOptionsEx, 'content'>) {
V
vben 已提交
49
  return <div innerHTML={`<div>${content as string}</div>`}></div>;
陈文彬 已提交
50 51 52 53 54
}

/**
 * @description: Create confirmation box
 */
V
vben 已提交
55
function createConfirm(options: ModalOptionsEx): ConfirmOptions {
陈文彬 已提交
56 57
  const iconType = options.iconType || 'warning';
  Reflect.deleteProperty(options, 'iconType');
V
vben 已提交
58
  const opt: ModalFuncProps = {
陈文彬 已提交
59 60 61 62
    centered: true,
    icon: getIcon(iconType),
    ...options,
  };
V
vben 已提交
63
  return (Modal.confirm(opt) as unknown) as ConfirmOptions;
陈文彬 已提交
64
}
V
vben 已提交
65

V
vben 已提交
66 67 68 69 70 71
const getBaseOptions = () => {
  const { t } = useI18n();
  return {
    okText: t('common.okText'),
    centered: true,
  };
陈文彬 已提交
72 73 74 75
};

function createModalOptions(options: ModalOptionsPartial, icon: string): ModalOptionsPartial {
  return {
V
vben 已提交
76
    ...getBaseOptions(),
陈文彬 已提交
77 78 79 80 81
    ...options,
    content: renderContent(options),
    icon: getIcon(icon),
  };
}
V
vben 已提交
82

陈文彬 已提交
83
function createSuccessModal(options: ModalOptionsPartial) {
V
vben 已提交
84
  return Modal.success(createModalOptions(options, 'success'));
陈文彬 已提交
85
}
V
vben 已提交
86

陈文彬 已提交
87 88 89
function createErrorModal(options: ModalOptionsPartial) {
  return Modal.error(createModalOptions(options, 'close'));
}
V
vben 已提交
90

陈文彬 已提交
91 92 93
function createInfoModal(options: ModalOptionsPartial) {
  return Modal.info(createModalOptions(options, 'info'));
}
V
vben 已提交
94

陈文彬 已提交
95
function createWarningModal(options: ModalOptionsPartial) {
V
vben 已提交
96
  return Modal.warning(createModalOptions(options, 'warning'));
陈文彬 已提交
97 98 99 100 101 102
}

notification.config({
  placement: 'topRight',
  duration: 3,
});
V
vben 已提交
103

陈文彬 已提交
104 105 106 107 108
/**
 * @description: message
 */
export function useMessage() {
  return {
109
    createMessage: Message,
V
vben 已提交
110
    notification: notification as NotifyApi,
陈文彬 已提交
111 112 113 114 115 116 117
    createConfirm: createConfirm,
    createSuccessModal,
    createErrorModal,
    createInfoModal,
    createWarningModal,
  };
}