chore: refactor message types implementation (#34654)

This commit is contained in:
zhao-huo-long 2022-03-23 14:49:57 +08:00 committed by GitHub
parent b77a8fdce3
commit 2282b1c580
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 9 deletions

View File

@ -12,7 +12,7 @@ import {
attachTypeApi,
ThenableArgument,
getKeyThenIncreaseKey,
NoticeType,
typeList,
} from '..';
export default function createUseMessage(
@ -78,9 +78,7 @@ export default function createUseMessage(
hookApiRef.current.open = notify;
(['success', 'info', 'warning', 'error', 'loading'] as NoticeType[]).forEach(type =>
attachTypeApi(hookApiRef.current, type),
);
typeList.forEach(type => attachTypeApi(hookApiRef.current, type));
return [
hookApiRef.current,

View File

@ -13,8 +13,6 @@ import InfoCircleFilled from '@ant-design/icons/InfoCircleFilled';
import createUseMessage from './hooks/useMessage';
import ConfigProvider, { globalConfig } from '../config-provider';
export type NoticeType = 'info' | 'success' | 'error' | 'warning' | 'loading';
let messageInstance: RCNotificationInstance | null;
let defaultDuration = 3;
let defaultTop: number;
@ -128,6 +126,11 @@ const typeToIcon = {
warning: ExclamationCircleFilled,
loading: LoadingOutlined,
};
export type NoticeType = keyof typeof typeToIcon;
export const typeList = Object.keys(typeToIcon) as NoticeType[];
export interface ArgsProps {
content: React.ReactNode;
duration?: number;
@ -247,9 +250,7 @@ export function attachTypeApi(originalApi: MessageApi, type: NoticeType) {
};
}
(['success', 'info', 'warning', 'error', 'loading'] as NoticeType[]).forEach(type =>
attachTypeApi(api, type),
);
typeList.forEach(type => attachTypeApi(api, type));
api.warn = api.warning;
api.useMessage = createUseMessage(getRCNotificationInstance, getRCNoticeProps);