Alert警告提示

    • 当某个页面需要向用户显示警告的信息时。

    • 非浮层的静态展现形式,始终展现,不会自动消失,用户可以点击关闭。

    基本

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

    Alert警告提示 - 图2

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

    1. const onClose = e => {
    2. console.log(e, 'I was closed.');
    3. };
    4. ReactDOM.render(
    5. <div>
    6. <Alert
    7. message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
    8. type="warning"
    9. closable
    10. onClose={onClose}
    11. />
    12. <Alert
    13. message="Error Text"
    14. description="Error Description Error Description Error Description Error Description Error Description Error Description"
    15. type="error"
    16. closable
    17. onClose={onClose}
    18. />
    19. </div>,
    20. mountNode,
    21. );

    图标

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

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

    1. import { Alert } from 'antd';
    2. ReactDOM.render(
    3. <div>
    4. <br />
    5. <Alert
    6. message="Very long warning text warning text text text text text text text"
    7. banner
    8. closable
    9. />
    10. <br />
    11. <Alert showIcon={false} message="Warning text without icon" banner />
    12. <Alert type="error" message="Error text" banner />
    13. </div>,
    14. mountNode,
    15. );

    Alert警告提示 - 图5

    四种样式

    共有四种样式 successinfowarningerror

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

    1. import { Alert } from 'antd';
    2. ReactDOM.render(
    3. <div>
    4. <Alert
    5. message="Success Text"
    6. description="Success Description Success Description Success Description"
    7. type="success"
    8. />
    9. <Alert
    10. message="Info Text"
    11. description="Info Description Info Description Info Description Info Description"
    12. type="info"
    13. />
    14. <Alert
    15. message="Warning Text"
    16. description="Warning Description Warning Description Warning Description Warning Description"
    17. type="warning"
    18. />
    19. <Alert
    20. description="Error Description Error Description Error Description Error Description"
    21. type="error"
    22. />
    23. </div>,
    24. mountNode,
    25. );

    自定义关闭

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

    Alert警告提示 - 图8

    平滑、自然的卸载提示。

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