Navigation Menu

Skip to content

Commit

Permalink
chore: refactor message types implementation (#34654)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhao-huo-long committed Mar 23, 2022
1 parent b77a8fd commit 2282b1c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
6 changes: 2 additions & 4 deletions components/message/hooks/useMessage.tsx
Expand Up @@ -12,7 +12,7 @@ import {
attachTypeApi,
ThenableArgument,
getKeyThenIncreaseKey,
NoticeType,
typeList,
} from '..';

export default function createUseMessage(
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions components/message/index.tsx
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2282b1c

Please sign in to comment.