Notification通知提醒框

    在系统四个角显示通知提醒信息。经常用于以下情况:

    • 较为复杂的通知内容。

    • 带有交互的通知,给出用户下一步的行动点。

    • 系统主动推送。

    基本

    最简单的用法,4.5 秒后自动关闭。

    Notification 通知提醒框 - 图2

    通知提醒框左侧有图标。

    1. const openNotificationWithIcon = type => {
    2. notification[type]({
    3. message: 'Notification Title',
    4. description:
    5. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
    6. });
    7. };
    8. ReactDOM.render(
    9. <div>
    10. <Button onClick={() => openNotificationWithIcon('success')}>Success</Button>
    11. <Button onClick={() => openNotificationWithIcon('info')}>Info</Button>
    12. <Button onClick={() => openNotificationWithIcon('warning')}>Warning</Button>
    13. <Button onClick={() => openNotificationWithIcon('error')}>Error</Button>
    14. </div>,
    15. mountNode,
    16. );

    自定义图标

    1. import { Button, notification, Icon } from 'antd';
    2. const openNotification = () => {
    3. notification.open({
    4. message: 'Notification Title',
    5. description:
    6. 'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
    7. icon: <Icon type="smile" style={{ color: '#108ee9' }} />,
    8. });
    9. };
    10. ReactDOM.render(
    11. <Button type="primary" onClick={openNotification}>
    12. Open the notification box
    13. </Button>,
    14. mountNode,
    15. );

    Notification 通知提醒框 - 图4

    使用 style 和 className 来定义样式。

    自动关闭的延时

    自定义通知框自动关闭的延时,默认4.5s,取消自动关闭只要将该值设为 0 即可。

    1. const openNotification = () => {
    2. const args = {
    3. message: 'Notification Title',
    4. description:
    5. duration: 0,
    6. };
    7. notification.open(args);
    8. };
    9. ReactDOM.render(
    10. <Button type="primary" onClick={openNotification}>
    11. Open the notification box
    12. </Button>,
    13. mountNode,
    14. );

    Notification 通知提醒框 - 图6

    自定义关闭按钮的样式和文字。

    1. import { Button, notification } from 'antd';
    2. const close = () => {
    3. console.log(
    4. 'Notification was closed. Either the close button was clicked or duration time elapsed.',
    5. );
    6. };
    7. const openNotification = () => {
    8. const key = `open${Date.now()}`;
    9. const btn = (
    10. <Button type="primary" size="small" onClick={() => notification.close(key)}>
    11. Confirm
    12. </Button>
    13. );
    14. notification.open({
    15. message: 'Notification Title',
    16. description:
    17. 'A function will be be called after the notification is closed (automatically after the "duration" time of manually).',
    18. btn,
    19. key,
    20. onClose: close,
    21. });
    22. };
    23. <Button type="primary" onClick={openNotification}>
    24. Open the notification box
    25. </Button>,
    26. mountNode,
    27. );

    位置

    可以设置通知从右上角、右下角、左下角、左上角弹出。

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

    1. import { Button, notification } from 'antd';
    2. const key = 'updatable';
    3. const openNotification = () => {
    4. notification.open({
    5. key,
    6. message: 'Notification Title',
    7. description: 'description.',
    8. });
    9. setTimeout(() => {
    10. notification.open({
    11. key,
    12. message: 'New Title',
    13. description: 'New description.',
    14. });
    15. }, 1000);
    16. };
    17. ReactDOM.render(
    18. <Button type="primary" onClick={openNotification}>
    19. Open the notification box
    20. </Button>,
    21. mountNode,
    22. );
    • notification.success(config)

    • notification.error(config)

    • notification.info(config)

    • notification.warning(config)

    • notification.warn(config)

    • notification.open(config)

    • notification.close(key: String)

    • notification.destroy()

    config 参数如下:

    还提供了一个全局配置方法,在调用前提前配置,全局一次生效。

    • notification.config(options)
    1. notification.config({
    2. placement: 'bottomRight',
    3. bottom: 50,
    4. });
    参数说明类型默认值
    bottom消息从底部弹出时,距离底部的位置,单位像素。number24
    duration默认自动关闭延时,单位秒number4.5
    getContainer配置渲染节点的输出位置() => HTMLNode() => document.body
    placement弹出位置,可选 topLeft topRight bottomRightstringtopRight
    top消息从顶部弹出时,距离顶部的位置,单位像素。number24