Badge徽标数

图标右上角的圆形徽标数字。

何时使用

一般出现在通知图标或头像的右上角,用于显示需要处理的消息条数,通过醒目视觉形式吸引用户处理。

代码演示

Badge徽标数 - 图1

基本

简单的徽章展示,当 count0 时,默认不显示,但是可以使用 showZero 修改为显示。

  1. import { Badge } from 'antd';
  2. import { ClockCircleOutlined } from '@ant-design/icons';
  3. ReactDOM.render(
  4. <div>
  5. <Badge count={5}>
  6. <a href="#" className="head-example" />
  7. </Badge>
  8. <Badge count={0} showZero>
  9. <a href="#" className="head-example" />
  10. </Badge>
  11. <Badge count={<ClockCircleOutlined style={{ color: '#f5222d' }} />}>
  12. <a href="#" className="head-example" />
  13. </Badge>
  14. </div>,
  15. mountNode,
  16. );

Badge徽标数 - 图2

封顶数字

超过 overflowCount 的会显示为 ${overflowCount}+,默认的 overflowCount99

  1. import { Badge } from 'antd';
  2. ReactDOM.render(
  3. <div>
  4. <Badge count={99}>
  5. <a href="#" className="head-example" />
  6. </Badge>
  7. <Badge count={100}>
  8. <a href="#" className="head-example" />
  9. </Badge>
  10. <Badge count={99} overflowCount={10}>
  11. <a href="#" className="head-example" />
  12. </Badge>
  13. <Badge count={1000} overflowCount={999}>
  14. <a href="#" className="head-example" />
  15. </Badge>
  16. </div>,
  17. mountNode,
  18. );

Badge徽标数 - 图3

可点击

用 a 标签进行包裹即可。

  1. import { Badge } from 'antd';
  2. ReactDOM.render(
  3. <a href="#">
  4. <Badge count={5}>
  5. <span className="head-example" />
  6. </Badge>
  7. </a>,
  8. mountNode,
  9. );

Badge徽标数 - 图4

状态点

用于表示状态的小圆点。

  1. import { Badge } from 'antd';
  2. ReactDOM.render(
  3. <div>
  4. <Badge status="success" />
  5. <Badge status="error" />
  6. <Badge status="default" />
  7. <Badge status="processing" />
  8. <Badge status="warning" />
  9. <br />
  10. <Badge status="success" text="Success" />
  11. <br />
  12. <Badge status="error" text="Error" />
  13. <br />
  14. <Badge status="default" text="Default" />
  15. <br />
  16. <Badge status="processing" text="Processing" />
  17. <br />
  18. <Badge status="warning" text="Warning" />
  19. </div>,
  20. mountNode,
  21. );

Badge徽标数 - 图5

独立使用

不包裹任何元素即是独立使用,可自定样式展现。

在右上角的 badge 则限定为红色。

  1. import { Badge } from 'antd';
  2. ReactDOM.render(
  3. <div>
  4. <Badge count={25} />
  5. <Badge count={4} className="site-badge-count-4" />
  6. <Badge className="site-badge-count-109" count={109} style={{ backgroundColor: '#52c41a' }} />
  7. </div>,
  8. mountNode,
  9. );
  1. .site-badge-count-4 .ant-badge-count {
  2. background-color: #fff;
  3. color: #999;
  4. box-shadow: 0 0 0 1px #d9d9d9 inset;
  5. }

Badge徽标数 - 图6

讨嫌的小红点

没有具体的数字。

  1. import { Badge } from 'antd';
  2. import { NotificationOutlined } from '@ant-design/icons';
  3. ReactDOM.render(
  4. <div>
  5. <Badge dot>
  6. <NotificationOutlined />
  7. </Badge>
  8. <Badge count={0} dot>
  9. <NotificationOutlined />
  10. </Badge>
  11. <Badge dot>
  12. <a href="#">Link something</a>
  13. </Badge>
  14. </div>,
  15. mountNode,
  16. );

Badge徽标数 - 图7

动态

展示动态变化的效果。

  1. import { Badge, Button, Switch } from 'antd';
  2. import { MinusOutlined, PlusOutlined } from '@ant-design/icons';
  3. const ButtonGroup = Button.Group;
  4. class Demo extends React.Component {
  5. state = {
  6. count: 5,
  7. show: true,
  8. };
  9. increase = () => {
  10. const count = this.state.count + 1;
  11. this.setState({ count });
  12. };
  13. decline = () => {
  14. let count = this.state.count - 1;
  15. if (count < 0) {
  16. count = 0;
  17. }
  18. this.setState({ count });
  19. };
  20. onChange = show => {
  21. this.setState({ show });
  22. };
  23. render() {
  24. return (
  25. <div>
  26. <div>
  27. <Badge count={this.state.count}>
  28. <a href="#" className="head-example" />
  29. </Badge>
  30. <ButtonGroup>
  31. <Button onClick={this.decline}>
  32. <MinusOutlined />
  33. </Button>
  34. <Button onClick={this.increase}>
  35. <PlusOutlined />
  36. </Button>
  37. </ButtonGroup>
  38. </div>
  39. <div style={{ marginTop: 10 }}>
  40. <Badge dot={this.state.show}>
  41. <a href="#" className="head-example" />
  42. </Badge>
  43. <Switch onChange={this.onChange} checked={this.state.show} />
  44. </div>
  45. </div>
  46. );
  47. }
  48. }
  49. ReactDOM.render(<Demo />, mountNode);

Badge徽标数 - 图8

多彩徽标

3.16.0 后新增。我们添加了多种预设色彩的徽标样式,用作不同场景使用。如果预设值不能满足你的需求,可以设置为具体的色值。

  1. import { Badge } from 'antd';
  2. const colors = [
  3. 'pink',
  4. 'red',
  5. 'yellow',
  6. 'orange',
  7. 'cyan',
  8. 'green',
  9. 'blue',
  10. 'purple',
  11. 'geekblue',
  12. 'magenta',
  13. 'volcano',
  14. 'gold',
  15. 'lime',
  16. ];
  17. ReactDOM.render(
  18. <div>
  19. <h4 style={{ marginBottom: 16 }}>Presets:</h4>
  20. <div>
  21. {colors.map(color => (
  22. <div key={color}>
  23. <Badge color={color} text={color} />
  24. </div>
  25. ))}
  26. </div>
  27. <h4 style={{ margin: '16px 0' }}>Custom:</h4>
  28. <div>
  29. <Badge color="#f50" text="#f50" />
  30. <br />
  31. <Badge color="#2db7f5" text="#2db7f5" />
  32. <br />
  33. <Badge color="#87d068" text="#87d068" />
  34. <br />
  35. <Badge color="#108ee9" text="#108ee9" />
  36. </div>
  37. </div>,
  38. mountNode,
  39. );
  1. .ant-tag {
  2. margin-bottom: 8px;
  3. }

API

  1. <Badge count={5}>
  2. <a href="#" className="head-example" />
  3. </Badge>
  1. <Badge count={5} />
参数说明类型默认值版本
color自定义小圆点的颜色string-
count展示的数字,大于 overflowCount 时显示为 ${overflowCount}+,为 0 时隐藏ReactNode
dot不展示数字,只有一个小红点booleanfalse
offset设置状态点的位置偏移,格式为 [x, y][number, number]-
overflowCount展示封顶的数字值number99
showZero当数值为 0 时,是否展示 Badgebooleanfalse
status设置 Badge 为状态点success | processing | default | error | warning''
text在设置了 status 的前提下有效,设置状态点的文本string''
title设置鼠标放在状态点上时显示的文字stringcount