Revert "fix: should cache correctly"

This reverts commit 75423ed4b1.
This commit is contained in:
Benjy Cui 2016-10-27 17:39:28 +08:00
parent 7860d74725
commit ca2e9c6dc2
3 changed files with 34 additions and 43 deletions

View File

@ -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(
<Button onClick={success}>unstack messages</Button>,
mountNode
);
ReactDOM.render(<Button onClick={success}>unstack messages</Button>
, mountNode);
````

View File

@ -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 messageInstance;
let key = 1;
let prefixCls = 'ant-message';
let defaultStack = true;
let key = 1;
let defaultDuration = 1.5;
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;
}
},
};

View File

@ -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;
}