From ca2e9c6dc20c44a6397b18ebfa0376ca71e51946 Mon Sep 17 00:00:00 2001 From: Benjy Cui Date: Thu, 27 Oct 2016 17:39:28 +0800 Subject: [PATCH] Revert "fix: should cache correctly" This reverts commit 75423ed4b1819f88e5aeb1b2154f572aea2da2df. --- components/message/demo/unstack.md | 16 ++++----- components/message/index.tsx | 54 ++++++++++++----------------- components/message/style/index.less | 7 ++-- 3 files changed, 34 insertions(+), 43 deletions(-) diff --git a/components/message/demo/unstack.md b/components/message/demo/unstack.md index 066114521e..283593e97e 100644 --- a/components/message/demo/unstack.md +++ b/components/message/demo/unstack.md @@ -2,8 +2,7 @@ order: 4 title: zh-CN: 不堆叠 - en-US: Unstack -debug: true + en-US: unstack --- ## zh-CN @@ -18,15 +17,12 @@ List the messages without stack. import { message, Button } from 'antd'; const success = function () { - message.config({ stack: false }); + message.config({ + stack: false, + }); message.success('This is a unstack message'); - - // Should not affect other demos - message.config({ stack: true }); }; -ReactDOM.render( - , - mountNode -); +ReactDOM.render( +, mountNode); ```` diff --git a/components/message/index.tsx b/components/message/index.tsx index e420e496c6..9e3d35931f 100755 --- a/components/message/index.tsx +++ b/components/message/index.tsx @@ -2,44 +2,38 @@ import React from 'react'; import Notification from 'rc-notification'; import Icon from '../icon'; -const messageInstances = {}; -let prefixCls = 'ant-message'; +let defaultDuration = 1.5; let defaultTop; -let defaultStack = true; - +let messageInstance; let key = 1; -let defaultDuration = 1.5; +let prefixCls = 'ant-message'; +let defaultStack = true; function getMessageInstance() { - const cachedKey = `${prefixCls}-${defaultTop}-${defaultStack}`; - if (!messageInstances[cachedKey]) { - messageInstances[cachedKey] = Notification.newInstance({ - prefixCls, - className: defaultStack ? '' : `${prefixCls}-unstack`, - transitionName: 'move-up', - style: { top: defaultTop }, // 覆盖原来的样式 - }); - } - return messageInstances[cachedKey]; + messageInstance = messageInstance || Notification.newInstance({ + prefixCls: `${!defaultStack ? `${prefixCls}-unstack ` : ''}${prefixCls}`, + transitionName: 'move-up', + style: { top: defaultTop }, // 覆盖原来的样式 + }); + return messageInstance; } type NoticeType = 'info' | 'success' | 'error' | 'warning' | 'loading'; -const iconTypeMap = { - info: 'info-circle', - success: 'check-circle', - error: 'cross-circle', - warning: 'exclamation-circle', - loading: 'loading', -}; function notice( content: React.ReactNode, duration: number = defaultDuration, type: NoticeType, - onClose?: () => void -) { - const iconType = iconTypeMap[type]; - const instance = getMessageInstance(); + onClose?: () => void) { + let iconType = ({ + info: 'info-circle', + success: 'check-circle', + error: 'cross-circle', + warning: 'exclamation-circle', + loading: 'loading', + })[type]; + + let instance = getMessageInstance(); instance.notice({ key, duration, @@ -107,11 +101,9 @@ export default { } }, destroy() { - for (const cachedKey in messageInstances) { - if (messageInstances.hasOwnProperty(cachedKey)) { - messageInstances[cachedKey].destroy(); - delete messageInstances[cachedKey]; - } + if (messageInstance) { + messageInstance.destroy(); + messageInstance = null; } }, }; diff --git a/components/message/style/index.less b/components/message/style/index.less index bcbee7b452..66ba7ee996 100644 --- a/components/message/style/index.less +++ b/components/message/style/index.less @@ -28,13 +28,16 @@ } &-unstack { - .@{message-prefix-cls}-notice { + & .@{message-prefix-cls}-notice { + width: 100%; + display: block; position: relative; margin: 0 auto 10px; + height: 34px; text-align: center; } - .@{message-prefix-cls}-notice-content { + & .@{message-prefix-cls}-notice-content { width: auto; display: inline-block; }