vue-vben-admin/src/hooks/web/useMessage.tsx

114 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-11-01 18:34:35 +08:00
import type { ModalFunc, ModalFuncProps } from 'ant-design-vue/lib/modal/Modal';
2020-09-28 20:19:10 +08:00
import { Modal, message as Message, notification } from 'ant-design-vue';
import { InfoCircleFilled, CheckCircleFilled, CloseCircleFilled } from '@ant-design/icons-vue';
2020-11-12 22:55:30 +08:00
import { ArgsProps, ConfigProps } from 'ant-design-vue/lib/notification';
2020-09-28 20:19:10 +08:00
2020-11-12 22:55:30 +08:00
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;
2020-11-03 21:30:25 +08:00
}
export declare type NotificationPlacement = 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight';
export declare type IconType = 'success' | 'info' | 'error' | 'warning';
2020-11-01 22:47:40 +08:00
export interface ModalOptionsEx extends Omit<ModalFuncProps, 'iconType'> {
iconType: 'warning' | 'success' | 'error' | 'info';
}
export type ModalOptionsPartial = Partial<ModalOptionsEx> & Pick<ModalOptionsEx, 'content'>;
2020-11-01 18:34:35 +08:00
interface ConfirmOptions {
info: ModalFunc;
success: ModalFunc;
error: ModalFunc;
warn: ModalFunc;
warning: ModalFunc;
}
2020-09-28 20:19:10 +08:00
function getIcon(iconType: string) {
if (iconType === 'warning') {
return <InfoCircleFilled class="modal-icon-warning" />;
} else if (iconType === 'success') {
return <CheckCircleFilled class="modal-icon-success" />;
2020-10-27 23:58:44 +08:00
} else if (iconType === 'info') {
return <InfoCircleFilled class="modal-icon-info" />;
2020-09-28 20:19:10 +08:00
} else {
return <CloseCircleFilled class="modal-icon-error" />;
}
}
2020-11-21 22:47:10 +08:00
2020-09-28 20:19:10 +08:00
function renderContent({ content }: Pick<ModalOptionsEx, 'content'>) {
2020-11-01 18:34:35 +08:00
return <div innerHTML={`<div>${content as string}</div>`}></div>;
2020-09-28 20:19:10 +08:00
}
/**
* @description: Create confirmation box
*/
2020-11-01 18:34:35 +08:00
function createConfirm(options: ModalOptionsEx): ConfirmOptions {
2020-09-28 20:19:10 +08:00
const iconType = options.iconType || 'warning';
Reflect.deleteProperty(options, 'iconType');
2020-11-01 18:34:35 +08:00
const opt: ModalFuncProps = {
2020-09-28 20:19:10 +08:00
centered: true,
icon: getIcon(iconType),
...options,
};
2020-11-01 18:34:35 +08:00
return Modal.confirm(opt) as any;
2020-09-28 20:19:10 +08:00
}
2020-11-21 22:47:10 +08:00
2020-09-28 20:19:10 +08:00
const baseOptions = {
okText: '确定',
centered: true,
};
function createModalOptions(options: ModalOptionsPartial, icon: string): ModalOptionsPartial {
return {
...baseOptions,
...options,
content: renderContent(options),
icon: getIcon(icon),
};
}
2020-11-21 22:47:10 +08:00
2020-09-28 20:19:10 +08:00
function createSuccessModal(options: ModalOptionsPartial) {
2020-10-27 23:58:44 +08:00
return Modal.success(createModalOptions(options, 'success'));
2020-09-28 20:19:10 +08:00
}
2020-11-21 22:47:10 +08:00
2020-09-28 20:19:10 +08:00
function createErrorModal(options: ModalOptionsPartial) {
return Modal.error(createModalOptions(options, 'close'));
}
2020-11-21 22:47:10 +08:00
2020-09-28 20:19:10 +08:00
function createInfoModal(options: ModalOptionsPartial) {
return Modal.info(createModalOptions(options, 'info'));
}
2020-11-21 22:47:10 +08:00
2020-09-28 20:19:10 +08:00
function createWarningModal(options: ModalOptionsPartial) {
2020-10-27 23:58:44 +08:00
return Modal.warning(createModalOptions(options, 'warning'));
2020-09-28 20:19:10 +08:00
}
notification.config({
placement: 'topRight',
duration: 3,
});
2020-11-21 22:47:10 +08:00
2020-09-28 20:19:10 +08:00
/**
* @description: message
*/
export function useMessage() {
return {
createMessage: Message,
2020-11-12 22:55:30 +08:00
notification: notification as NotifyApi,
2020-09-28 20:19:10 +08:00
createConfirm: createConfirm,
createSuccessModal,
createErrorModal,
createInfoModal,
createWarningModal,
};
}