antd message源码分析 Posted on 2019-04-22 github地址。调用了 rc-notification 模块。 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177/* global Promise */import * as React from 'react';import Notification from 'rc-notification';import Icon from '../icon';let defaultDuration = 3; // 持续时间 slet defaultTop: number; // css top值let messageInstance: any; // message实例let key = 1; // 自增keylet prefixCls = 'ant-message'; // prefix clslet transitionName = 'move-up'; // transitionlet getContainer: () => HTMLElement;// containerlet maxCount: number; // message个数阈值/** * 如果有 messageInstance 则 callback(messageInstance)* 否则创建一个 messageInstance 再 callback(messageInstance)* @param {any} callback*/function getMessageInstance(callback: (i: any) => void) { if (messageInstance) { callback(messageInstance); return; } Notification.newInstance({ prefixCls, transitionName, style: { top: defaultTop }, // 覆盖原来的样式 getContainer, maxCount, }, (instance: any) => { if (messageInstance) { callback(messageInstance); return; } messageInstance = instance; callback(instance); });}type NoticeType = 'info' | 'success' | 'error' | 'warning' | 'loading';export interface ThenableArgument { (_: any): any;}export interface MessageType { (): void; then: (fill: ThenableArgument, reject: ThenableArgument) => Promise<any>; promise: Promise<any>;}/*** @param {React.ReactNode} content message组件的content* @param {func | number} duration 持续时间* @param {NoticeType} type 类型*/function notice( content: React.ReactNode, duration: (() => void) | number = defaultDuration, type: NoticeType, onClose?: () => void,): MessageType { const iconType = ({ info: 'info-circle', success: 'check-circle', error: 'cross-circle', warning: 'exclamation-circle', loading: 'loading', })[type]; if (typeof duration === 'function') { onClose = duration; duration = defaultDuration; } const target = key++; // 调用 messageInstance,并将 onClose 作为 callback 传进去 const closePromise = new Promise((resolve) => { const callback = () => { if (typeof onClose === 'function') { onClose(); } return resolve(true); }; getMessageInstance((instance) => { instance.notice({ key: target, duration, style: {}, content: ( <div className={`${prefixCls}-custom-content ${prefixCls}-${type}`}> <Icon type={iconType} /> <span>{content}</span> </div> ), onClose: callback, }); }); }); const result: any = () => { if (messageInstance) { messageInstance.removeNotice(target); } }; result.then = (filled: ThenableArgument, rejected: ThenableArgument) => closePromise.then(filled, rejected); result.promise = closePromise; // 返回remove notice的函数 return result;}type ConfigContent = React.ReactNode | string;type ConfigDuration = number | (() => void);export type ConfigOnClose = () => void;export interface ConfigOptions { top?: number; duration?: number; prefixCls?: string; getContainer?: () => HTMLElement; transitionName?: string; maxCount?: number;}// 入口export default { info(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) { return notice(content, duration, 'info', onClose); }, success(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) { return notice(content, duration, 'success', onClose); }, error(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) { return notice(content, duration, 'error', onClose); }, // Departed usage, please use warning() warn(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) { return notice(content, duration, 'warning', onClose); }, warning(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) { return notice(content, duration, 'warning', onClose); }, loading(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) { return notice(content, duration, 'loading', onClose); }, config(options: ConfigOptions) { if (options.top !== undefined) { defaultTop = options.top; messageInstance = null; // delete messageInstance for new defaultTop } if (options.duration !== undefined) { defaultDuration = options.duration; } if (options.prefixCls !== undefined) { prefixCls = options.prefixCls; } if (options.getContainer !== undefined) { getContainer = options.getContainer; } if (options.transitionName !== undefined) { transitionName = options.transitionName; messageInstance = null; // delete messageInstance for new transitionName } if (options.maxCount !== undefined) { maxCount = options.maxCount; messageInstance = null; } }, destroy() { if (messageInstance) { messageInstance.destroy(); messageInstance = null; } },};