From 2282b1c58042ecfc2f57e120cff5ef07fd52c664 Mon Sep 17 00:00:00 2001 From: zhao-huo-long Date: Wed, 23 Mar 2022 14:49:57 +0800 Subject: [PATCH] chore: refactor message types implementation (#34654) --- components/message/hooks/useMessage.tsx | 6 ++---- components/message/index.tsx | 11 ++++++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/components/message/hooks/useMessage.tsx b/components/message/hooks/useMessage.tsx index 1a2b94baed..3d5b82cc70 100644 --- a/components/message/hooks/useMessage.tsx +++ b/components/message/hooks/useMessage.tsx @@ -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, diff --git a/components/message/index.tsx b/components/message/index.tsx index 278ca80a53..9f19058c4a 100755 --- a/components/message/index.tsx +++ b/components/message/index.tsx @@ -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);