Alert 警告提示

警告提示,展现需要关注的信息。

何时使用

  • 当某个页面需要向用户显示警告的信息时。
  • 非浮层的静态展现形式,始终展现,不会自动消失,用户可以点击关闭。

代码演示

基本

最简单的用法,适用于简短的警告提示。

Alert警告提示 - 图1

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Alert } from 'choerodon-ui';
  4. ReactDOM.render(
  5. <Alert message="Success Text" type="success" />,
  6. document.getElementById('container'));

四种样式

共有四种样式 successinfowarningerror

Alert警告提示 - 图2

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Alert } from 'choerodon-ui';
  4. ReactDOM.render(
  5. <div>
  6. <Alert style={{ margin: '4px' }} message="Success Text" type="success" />
  7. <Alert style={{ margin: '4px' }} message="Info Text" type="info" />
  8. <Alert style={{ margin: '4px' }} message="Warning Text" type="warning" />
  9. <Alert style={{ margin: '4px' }} message="Error Text" type="error" />
  10. </div>,
  11. document.getElementById('container'),
  12. );

可关闭的警告提示

显示关闭按钮,点击可关闭警告提示。

Alert警告提示 - 图3

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Alert } from 'choerodon-ui';
  4. const onClose = function (e) {
  5. console.log(e, 'I was closed.');
  6. };
  7. ReactDOM.render(
  8. <div>
  9. <Alert
  10. style={{ margin: '4px' }}
  11. message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
  12. type="warning"
  13. closable
  14. onClose={onClose}
  15. />
  16. <Alert
  17. style={{ margin: '4px' }}
  18. message="Error Text"
  19. description="Error Description Error Description Error Description Error Description Error Description Error Description"
  20. type="error"
  21. closable
  22. onClose={onClose}
  23. />
  24. </div>,
  25. document.getElementById('container'),
  26. );

含有辅助性文字介绍

含有辅助性文字介绍的警告提示。

Alert警告提示 - 图4

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Alert } from 'choerodon-ui';
  4. ReactDOM.render(
  5. <div>
  6. <Alert
  7. style={{ margin: '4px' }}
  8. message="Success Text"
  9. description="Success Description Success Description Success Description"
  10. type="success"
  11. />
  12. <Alert
  13. style={{ margin: '4px' }}
  14. message="Info Text"
  15. description="Info Description Info Description Info Description Info Description"
  16. type="info"
  17. />
  18. <Alert
  19. style={{ margin: '4px' }}
  20. message="Warning Text"
  21. description="Warning Description Warning Description Warning Description Warning Description"
  22. type="warning"
  23. />
  24. <Alert
  25. style={{ margin: '4px' }}
  26. message="Error Text"
  27. description="Error Description Error Description Error Description Error Description"
  28. type="error"
  29. />
  30. </div>,
  31. document.getElementById('container'),
  32. );

图标

可口的图标让信息类型更加醒目。

Alert警告提示 - 图5

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Alert } from 'choerodon-ui';
  4. ReactDOM.render(
  5. <div>
  6. <Alert
  7. style={{ margin: '4px' }}
  8. message="Success Tips"
  9. type="success"
  10. showIcon
  11. />
  12. <Alert
  13. style={{ margin: '4px' }}
  14. message="Informational Notes"
  15. type="info"
  16. showIcon
  17. />
  18. <Alert
  19. style={{ margin: '4px' }}
  20. message="Warning"
  21. type="warning"
  22. showIcon
  23. />
  24. <Alert style={{ margin: '4px' }} message="Error" type="error" showIcon />
  25. <Alert
  26. style={{ margin: '4px' }}
  27. message="Success Tips"
  28. description="Detailed description and advices about successful copywriting."
  29. type="success"
  30. showIcon
  31. />
  32. <Alert
  33. style={{ margin: '4px' }}
  34. message="Informational Notes"
  35. description="Additional description and informations about copywriting."
  36. type="info"
  37. showIcon
  38. />
  39. <Alert
  40. style={{ margin: '4px' }}
  41. message="Warning"
  42. description="This is a warning notice about copywriting."
  43. type="warning"
  44. showIcon
  45. />
  46. <Alert
  47. style={{ margin: '4px' }}
  48. message="Error"
  49. description="This is an error message about copywriting."
  50. type="error"
  51. showIcon
  52. />
  53. </div>,
  54. document.getElementById('container'),
  55. );

自定义关闭

可以自定义关闭,自定义的文字会替换原先的关闭 Icon

Alert警告提示 - 图6

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Alert } from 'choerodon-ui';
  4. ReactDOM.render(
  5. <Alert message="Info Text" type="info" closeText="Close Now" />,
  6. document.getElementById('container'));

顶部公告

页面顶部通告形式,默认有图标且type 为 ‘warning’。

Alert警告提示 - 图7

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Alert } from 'choerodon-ui';
  4. ReactDOM.render(
  5. <div>
  6. <Alert message="Warning text" banner />
  7. <br style={{ margin: '4px' }} />
  8. <Alert
  9. message="Very long warning text warning text text text text text text text"
  10. banner
  11. closable
  12. />
  13. <br style={{ margin: '4px' }} />
  14. <Alert showIcon={false} message="Warning text without icon" banner />
  15. <br style={{ margin: '4px' }} />
  16. <Alert type="error" message="Error text" banner />
  17. </div>,
  18. document.getElementById('container'),
  19. );

平滑地卸载

平滑、自然的卸载提示

Alert警告提示 - 图8

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Alert } from 'choerodon-ui';
  4. class App extends React.Component {
  5. state = {
  6. visiable: true,
  7. }
  8. handleClose = () => {
  9. this.setState({ visiable: false });
  10. }
  11. render() {
  12. return (
  13. <div>
  14. {
  15. this.state.visiable ? (
  16. <Alert
  17. message="Alert Message Text"
  18. type="success"
  19. closable
  20. afterClose={this.handleClose}
  21. />
  22. ) : null
  23. }
  24. <p>placeholder text here</p>
  25. </div>
  26. );
  27. }
  28. }

API

参数说明类型默认值
afterClose关闭动画结束后的回掉() => void-
banner是否用作顶部公告booleanfalse
closable默认不显示关闭按钮boolean
closeText自定义关闭按钮string|ReactNode
description警告提示的辅助性文字介绍string|ReactNode
message警告提示内容string|ReactNode
showIcon是否显示辅助图标booleanfalse,banner 模式下默认值为 true
iconType自定义图标类型,showIcontrue 时有效string-
type指定警告提示的样式,有四种选择 successinfowarningerrorstringinfobanner 模式下默认值为 warning
onClose关闭时触发的回调函数(e: MouseEvent) => void