add maxCount config to message (#10169)

This commit is contained in:
Jin ZHANG 2018-04-20 15:40:43 +08:00 committed by niko
parent d016471638
commit e1c74eb799
4 changed files with 25 additions and 0 deletions

View File

@ -33,6 +33,20 @@ describe('message', () => {
expect(document.querySelectorAll('.custom-container').length).toBe(1); expect(document.querySelectorAll('.custom-container').length).toBe(1);
}); });
it('should be able to config maxCount', () => {
message.config({
maxCount: 5,
});
for (let i = 0; i < 10; i += 1) {
message.info('test');
}
message.info('last');
expect(document.querySelectorAll('.ant-message-notice').length).toBe(5);
expect(document.querySelectorAll('.ant-message-notice')[4].textContent).toBe('last');
jest.runAllTimers();
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
});
it('should be able to hide manually', () => { it('should be able to hide manually', () => {
const hide1 = message.info('whatever', 0); const hide1 = message.info('whatever', 0);
const hide2 = message.info('whatever', 0); const hide2 = message.info('whatever', 0);

View File

@ -40,6 +40,7 @@ Methods for global configuration and destruction are also provided:
message.config({ message.config({
top: 100, top: 100,
duration: 2, duration: 2,
maxCount: 3,
}); });
``` ```
@ -48,3 +49,4 @@ message.config({
| duration | time before auto-dismiss, in seconds | number | 1.5 | | duration | time before auto-dismiss, in seconds | number | 1.5 |
| getContainer | Return the mount node for Message | () => HTMLElement | () => document.body | | getContainer | Return the mount node for Message | () => HTMLElement | () => document.body |
| top | distance from top | number | 24px | | top | distance from top | number | 24px |
| maxCount | max message show, drop oldest if exceed limit | number | - |

View File

@ -9,6 +9,7 @@ let key = 1;
let prefixCls = 'ant-message'; let prefixCls = 'ant-message';
let transitionName = 'move-up'; let transitionName = 'move-up';
let getContainer: () => HTMLElement; let getContainer: () => HTMLElement;
let maxCount: number;
function getMessageInstance(callback: (i: any) => void) { function getMessageInstance(callback: (i: any) => void) {
if (messageInstance) { if (messageInstance) {
@ -20,6 +21,7 @@ function getMessageInstance(callback: (i: any) => void) {
transitionName, transitionName,
style: { top: defaultTop }, // 覆盖原来的样式 style: { top: defaultTop }, // 覆盖原来的样式
getContainer, getContainer,
maxCount,
}, (instance: any) => { }, (instance: any) => {
if (messageInstance) { if (messageInstance) {
callback(messageInstance); callback(messageInstance);
@ -83,6 +85,7 @@ export interface ConfigOptions {
prefixCls?: string; prefixCls?: string;
getContainer?: () => HTMLElement; getContainer?: () => HTMLElement;
transitionName?: string; transitionName?: string;
maxCount?: number;
} }
export default { export default {
@ -123,6 +126,10 @@ export default {
transitionName = options.transitionName; transitionName = options.transitionName;
messageInstance = null; // delete messageInstance for new transitionName messageInstance = null; // delete messageInstance for new transitionName
} }
if (options.maxCount !== undefined) {
maxCount = options.maxCount;
messageInstance = null;
}
}, },
destroy() { destroy() {
if (messageInstance) { if (messageInstance) {

View File

@ -41,6 +41,7 @@ title: Message
message.config({ message.config({
top: 100, top: 100,
duration: 2, duration: 2,
maxCount: 3,
}); });
``` ```
@ -49,3 +50,4 @@ message.config({
| duration | 默认自动关闭延时,单位秒 | number | 3 | | duration | 默认自动关闭延时,单位秒 | number | 3 |
| getContainer | 配置渲染节点的输出位置 | () => HTMLElement | () => document.body | | getContainer | 配置渲染节点的输出位置 | () => HTMLElement | () => document.body |
| top | 消息距离顶部的位置 | number | 24px | | top | 消息距离顶部的位置 | number | 24px |
| maxCount | 最大显示数, 超过限制时,最早的消息会被自动关闭 | number | - |