Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support updating message content with a unique key #18678

Merged
merged 1 commit into from Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions components/message/__tests__/__snapshots__/demo.test.js.snap
Expand Up @@ -72,3 +72,14 @@ exports[`renders ./components/message/demo/thenable.md correctly 1`] = `
</span>
</button>
`;

exports[`renders ./components/message/demo/update.md correctly 1`] = `
<button
class="ant-btn ant-btn-primary"
type="button"
>
<span>
Open the message box
</span>
</button>
`;
23 changes: 23 additions & 0 deletions components/message/__tests__/index.test.js
Expand Up @@ -146,4 +146,27 @@ describe('message', () => {
jest.runAllTimers();
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
});

it('should support update message content with a unique key', () => {
const key = 'updatable';
class Test extends React.Component {
componentDidMount() {
message.loading({ content: 'Loading...', key });
// Testing that content of the message should be updated.
setTimeout(() => message.success({ content: 'Loaded', key }), 1000);
wzhudev marked this conversation as resolved.
Show resolved Hide resolved
setTimeout(() => message.destroy(), 3000);
wzhudev marked this conversation as resolved.
Show resolved Hide resolved
}

render() {
return <div>test</div>;
}
}

mount(<Test />);
expect(document.querySelectorAll('.ant-message-notice').length).toBe(1);
jest.advanceTimersByTime(1500);
expect(document.querySelectorAll('.ant-message-notice').length).toBe(1);
jest.runAllTimers();
expect(document.querySelectorAll('.ant-message-notice').length).toBe(0);
});
});
34 changes: 34 additions & 0 deletions components/message/demo/update.md
@@ -0,0 +1,34 @@
---
order: 5
title:
zh-CN: 更新消息内容
en-US: Update Message Content
---

## zh-CN

可以通过唯一的 `key` 来更新内容。

## en-US

Update message content with unique `key`.

```jsx
import { Button, message } from 'antd';

const key = 'updatable';

const openMessage = () => {
message.loading({ content: 'Loading...', key });
setTimeout(() => {
message.success({ content: 'Loaded!', key, duration: 2 });
});
};

ReactDOM.render(
<Button type="primary" onClick={openMessage}>
Open the message box
</Button>,
mountNode,
);
```
wzhudev marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 10 additions & 1 deletion components/message/index.en-US.md
Expand Up @@ -25,7 +25,7 @@ This components provides some static methods, with usage and arguments as follow

| Argument | Description | Type | Default | Version |
| --- | --- | --- | --- | --- |
| content | content of the message | string\|ReactNode | - | |
| content | content of the message | string\|ReactNode\|config | - | |
| duration | time(seconds) before auto-dismiss, don't dismiss if set to 0 | number | 1.5 | |
| onClose | Specify a function that will be called when the message is closed | Function | - | |

Expand All @@ -36,7 +36,15 @@ This components provides some static methods, with usage and arguments as follow

where `level` refers one static methods of `message`. The result of `then` method will be a Promise.

Supports passing parameters wrapped in an object:

- `message.open(config)`
- `message.success(config)`
- `message.error(config)`
- `message.info(config)`
- `message.warning(config)`
- `message.warn(config)` // alias of warning
- `message.loading(config)`

The properties of config are as follows:

Expand All @@ -46,6 +54,7 @@ The properties of config are as follows:
| duration | time(seconds) before auto-dismiss, don't dismiss if set to 0 | number | 3 | |
| onClose | Specify a function that will be called when the message is closed | function | - | |
| icon | Customized Icon | ReactNode | - | 3.9.0 |
| key | The unique identifier of the Message | string\|number | - | 3.24.0 |

### Global static methods

Expand Down
27 changes: 19 additions & 8 deletions components/message/index.tsx
Expand Up @@ -53,6 +53,7 @@ export interface ArgsProps {
type: NoticeType;
onClose?: () => void;
icon?: React.ReactNode;
key?: string | number;
}

function notice(args: ArgsProps): MessageType {
Expand All @@ -79,7 +80,7 @@ function notice(args: ArgsProps): MessageType {
);
const switchIconNode = iconType ? iconNode : '';
instance.notice({
key: target,
key: args.key || target,
duration,
style: {},
content: (
Expand Down Expand Up @@ -109,8 +110,13 @@ function notice(args: ArgsProps): MessageType {

type ConfigContent = React.ReactNode | string;
type ConfigDuration = number | (() => void);
type JointContent = ConfigContent | ArgsProps;
export type ConfigOnClose = () => void;

function isArgsProps(content: JointContent): content is ArgsProps {
return typeof content === 'object' && !!(content as ArgsProps).content;
}

export interface ConfigOptions {
top?: number;
duration?: number;
Expand Down Expand Up @@ -154,24 +160,29 @@ const api: any = {
};

['success', 'info', 'warning', 'error', 'loading'].forEach(type => {
api[type] = (content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose) => {
api[type] = (content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose) => {
if (isArgsProps(content)) {
return api.open({ ...content, type });
}

if (typeof duration === 'function') {
onClose = duration;
duration = undefined;
wzhudev marked this conversation as resolved.
Show resolved Hide resolved
}

return api.open({ content, duration, type, onClose });
};
});

api.warn = api.warning;

export interface MessageApi {
info(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
success(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
error(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
warn(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
warning(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
loading(content: ConfigContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
info(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
success(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
error(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
warn(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
warning(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
loading(content: JointContent, duration?: ConfigDuration, onClose?: ConfigOnClose): MessageType;
open(args: ArgsProps): MessageType;
config(options: ConfigOptions): void;
destroy(): void;
Expand Down
35 changes: 23 additions & 12 deletions components/message/index.zh-CN.md
Expand Up @@ -24,11 +24,11 @@ title: Message
- `message.warn(content, [duration], onClose)` // alias of warning
- `message.loading(content, [duration], onClose)`
wzhudev marked this conversation as resolved.
Show resolved Hide resolved

| 参数 | 说明 | 类型 | 默认值 | 版本 |
| -------- | --------------------------------------------- | ----------------- | ------ | ---- |
| content | 提示内容 | string\|ReactNode | - | |
| duration | 自动关闭的延时,单位秒。设为 0 时不自动关闭。 | number | 3 | |
| onClose | 关闭时触发的回调函数 | Function | - | |
| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| content | 提示内容 | string\|ReactNode\|config | - | |
| duration | 自动关闭的延时,单位秒。设为 0 时不自动关闭。 | number | 3 | |
| onClose | 关闭时触发的回调函数 | Function | - | |

组件同时提供 promise 接口。

Expand All @@ -37,14 +37,25 @@ title: Message

其中`message[level]` 是组件已经提供的静态方法。`then` 接口返回值是 Promise。

- `message.open(config)`
也可以对象的形式传递参数:
yoyo837 marked this conversation as resolved.
Show resolved Hide resolved

| 参数 | 说明 | 类型 | 默认值 | 版本 |
| -------- | --------------------------------------------- | --------- | ------ | ----- |
| content | 提示内容 | ReactNode | - | |
| duration | 自动关闭的延时,单位秒。设为 0 时不自动关闭。 | number | 3 | |
| onClose | 关闭时触发的回调函数 | Function | - | |
| icon | 自定义图标 | ReactNode | - | 3.9.0 |
- `message.open(config)`
- `message.success(config)`
- `message.error(config)`
- `message.info(config)`
- `message.warning(config)`
- `message.warn(config)` // alias of warning
- `message.loading(config)`

`config` 对象属性如下:

| 参数 | 说明 | 类型 | 默认值 | 版本 |
| -------- | --------------------------------------------- | -------------- | ------ | ------ |
| content | 提示内容 | ReactNode | - | |
| duration | 自动关闭的延时,单位秒。设为 0 时不自动关闭。 | number | 3 | |
| onClose | 关闭时触发的回调函数 | Function | - | |
| icon | 自定义图标 | ReactNode | - | 3.9.0 |
| key | 当前提示的唯一标志 | string\|number | - | 3.24.0 |

### 全局方法

Expand Down