Message 全局提示

全局展示操作反馈信息。

何时使用

  • 可提供成功、警告和错误等反馈信息。
  • 顶部居中显示并自动消失,是一种不打断用户操作的轻量级提示方式。

代码演示

普通提示

信息提醒反馈。

Message全局提示 - 图1

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { message, Button } from 'choerodon-ui';
  4. const info = () => {
  5. message.info('This is a normal message');
  6. };
  7. ReactDOM.render(
  8. <Button type="primary" onClick={info}>Display normal message</Button>,
  9. document.getElementById('container'));

其他提示类型

包括成功、失败、警告。

Message全局提示 - 图2

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { message, Button } from 'choerodon-ui';
  4. message.config({
  5. top: '24px',
  6. });
  7. const success = () => {
  8. message.success('This is a message of success');
  9. };
  10. const error = () => {
  11. message.error('This is a message of error');
  12. };
  13. const warning = () => {
  14. message.warning('This is message of warning');
  15. };
  16. ReactDOM.render(
  17. <div>
  18. <Button onClick={success}>Success</Button>
  19. <Button onClick={error}>Error</Button>
  20. <Button onClick={warning}>Warning</Button>
  21. </div>,
  22. document.getElementById('container'));

修改延时

自定义时长 10s,默认时长为 3s

Message全局提示 - 图3

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { message, Button } from 'choerodon-ui';
  4. const success = () => {
  5. message.success('This is a prompt message for success, and it will disappear in 10 seconds', 10);
  6. };
  7. ReactDOM.render(
  8. <Button onClick={success}>Customized display duration</Button>,
  9. document.getElementById('container'));

加载中

进行全局 loading,异步自行移除。

Message全局提示 - 图4

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { message, Button } from 'choerodon-ui';
  4. const success = () => {
  5. const hide = message.loading('Action in progress..', 0);
  6. // Dismiss manually and asynchronously
  7. setTimeout(hide, 2500);
  8. };
  9. ReactDOM.render(
  10. <Button onClick={success}>Display a loading indicator</Button>,
  11. document.getElementById('container'));

消息位置

定义消息位置

Message全局提示 - 图5

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { message, Button } from 'choerodon-ui';
  4. const handleClick = (placement) => {
  5. message.destroy();
  6. message.config({
  7. top: 100,
  8. bottom: 100,
  9. duration: 5,
  10. });
  11. message.success(placement, undefined, undefined, placement);
  12. };
  13. ReactDOM.render(
  14. <div>
  15. <Button onClick={() => { handleClick('topLeft'); }}>topLeft</Button>
  16. <Button onClick={() => { handleClick('top'); }}>top</Button>
  17. <Button onClick={() => { handleClick('topRight'); }}>topRight</Button>
  18. <Button onClick={() => { handleClick('leftTop'); }}>leftTop</Button>
  19. <Button onClick={() => { handleClick('left'); }}>left</Button>
  20. <Button onClick={() => { handleClick('leftBottom'); }}>leftBottom</Button>
  21. <Button onClick={() => { handleClick('rightTop'); }}>rightTop</Button>
  22. <Button onClick={() => { handleClick('right'); }}>right</Button>
  23. <Button onClick={() => { handleClick('rightBottom'); }}>rightBottom</Button>
  24. <Button onClick={() => { handleClick('bottomLeft'); }}>bottomLeft</Button>
  25. <Button onClick={() => { handleClick('bottom'); }}>bottom</Button>
  26. <Button onClick={() => { handleClick('bottomRight'); }}>bottomRight</Button>
  27. </div>,
  28. document.getElementById('container'));

API

组件提供了一些静态方法,使用方式和参数如下:

  • message.success(content, [duration], [onClose], placement)
  • message.error(content, [duration], [onClose], placement)
  • message.info(content, [duration], [onClose], placement)
  • message.warning(content, [duration], [onClose], placement)
  • message.warn(content, [duration], [onClose], placement) // alias of warning
  • message.loading(content, [duration], [onClose], placement)
参数说明类型默认值
content提示内容string|ReactNode-
duration自动关闭的延时,单位秒number3
onClose关闭时触发的回调函数Function-
placement消息展示的位置, 可选值: top left right bottom topLeft topRight bottomLeft bottomRight leftTop leftBottom rightTop rightBottomstringleftBottom

还提供了全局配置和全局销毁方法:

  • message.config(options)
  • message.destroy()

message.config

  1. message.config({
  2. top: 100,
  3. duration: 2,
  4. });
参数说明类型默认值
duration默认自动关闭延时,单位秒number3
getContainer配置渲染节点的输出位置() => HTMLElement() => document.body
top消息在顶部出现时距离顶部的位置number24
bottom消息在底部出现时距离底部的位置number24
placement消息展示的位置, 可选值: top left right bottom topLeft topRight bottomLeft bottomRight leftTop leftBottom rightTop rightBottomstringleftBottom