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

简单的徽章展示,当 count 为 0 时,默认不显示,但是可以使用 showZero 修改为显示。
import { Badge } from 'antd';import { ClockCircleOutlined } from '@ant-design/icons';ReactDOM.render(<div><Badge count={5}><a href="#" className="head-example" /></Badge><Badge count={0} showZero><a href="#" className="head-example" /></Badge><Badge count={<ClockCircleOutlined style={{ color: '#f5222d' }} />}><a href="#" className="head-example" /></Badge></div>,mountNode,);

超过 overflowCount 的会显示为 ${overflowCount}+,默认的 overflowCount 为 99。
import { Badge } from 'antd';ReactDOM.render(<div><Badge count={99}><a href="#" className="head-example" /></Badge><Badge count={100}><a href="#" className="head-example" /></Badge><Badge count={99} overflowCount={10}><a href="#" className="head-example" /></Badge><Badge count={1000} overflowCount={999}><a href="#" className="head-example" /></Badge></div>,mountNode,);

用 a 标签进行包裹即可。
import { Badge } from 'antd';ReactDOM.render(<a href="#"><Badge count={5}><span className="head-example" /></Badge></a>,mountNode,);

设置状态点的位置偏移,格式为 [left, top],表示状态点距默认位置左侧、上方的偏移量。
import { Badge } from 'antd';ReactDOM.render(<><Badge count={5} offset={[10, 10]}><a href="#" className="head-example" /></Badge></>,mountNode,);

我们添加了多种预设色彩的徽标样式,用作不同场景使用。如果预设值不能满足你的需求,可以设置为具体的色值。
import { Badge, Divider } from 'antd';const colors = ['pink','red','yellow','orange','cyan','green','blue','purple','geekblue','magenta','volcano','gold','lime',];ReactDOM.render(<><Divider orientation="left">Presets</Divider><div>{colors.map(color => (<div key={color}><Badge color={color} text={color} /></div>))}</div><Divider orientation="left">Custom</Divider><div><Badge color="#f50" text="#f50" /><br /><Badge color="#2db7f5" text="#2db7f5" /><br /><Badge color="#87d068" text="#87d068" /><br /><Badge color="#108ee9" text="#108ee9" /></div></>,mountNode,);
.ant-tag {margin-bottom: 8px;}

使用缎带型的徽标。
import { Badge, Card } from 'antd';ReactDOM.render(<Badge.Ribbon text="Pushes open the window"><Card>And raises the spyglass.</Card></Badge.Ribbon>,mountNode,);

不包裹任何元素即是独立使用,可自定样式展现。
在右上角的 badge 则限定为红色。
import { Badge, Space, Switch } from 'antd';const Demo = () => {const [show, setShow] = React.useState(true);return (<Space><Switchchecked={show}onChange={() => {setShow(!show);}}/><Badge count={show ? 25 : 0} /><Badge count={show ? 4 : 0} className="site-badge-count-4" /><BadgeclassName="site-badge-count-109"count={show ? 109 : 0}style={{ backgroundColor: '#52c41a' }}/></Space>);};ReactDOM.render(<Demo />, mountNode);
.site-badge-count-4 .ant-badge-count {background-color: #fff;color: #999;box-shadow: 0 0 0 1px #d9d9d9 inset;}

没有具体的数字。
import { Badge } from 'antd';import { NotificationOutlined } from '@ant-design/icons';ReactDOM.render(<div><Badge dot><NotificationOutlined /></Badge><Badge count={0} dot><NotificationOutlined /></Badge><Badge dot><a href="#">Link something</a></Badge></div>,mountNode,);

展示动态变化的效果。
import { Badge, Button, Switch } from 'antd';import { MinusOutlined, PlusOutlined } from '@ant-design/icons';const ButtonGroup = Button.Group;class Demo extends React.Component {state = {count: 5,show: true,};increase = () => {const count = this.state.count + 1;this.setState({ count });};decline = () => {let count = this.state.count - 1;if (count < 0) {count = 0;}this.setState({ count });};onChange = show => {this.setState({ show });};render() {return (<div><div><Badge count={this.state.count}><a href="#" className="head-example" /></Badge><ButtonGroup><Button onClick={this.decline}><MinusOutlined /></Button><Button onClick={this.increase}><PlusOutlined /></Button></ButtonGroup></div><div style={{ marginTop: 10 }}><Badge dot={this.state.show}><a href="#" className="head-example" /></Badge><Switch onChange={this.onChange} checked={this.state.show} /></div></div>);}}ReactDOM.render(<Demo />, mountNode);

用于表示状态的小圆点。
import { Badge } from 'antd';ReactDOM.render(<div><Badge status="success" /><Badge status="error" /><Badge status="default" /><Badge status="processing" /><Badge status="warning" /><br /><Badge status="success" text="Success" /><br /><Badge status="error" text="Error" /><br /><Badge status="default" text="Default" /><br /><Badge status="processing" text="Processing" /><br /><Badge status="warning" text="Warning" /></div>,mountNode,);

可以设置有数字徽标的大小。
import { Badge } from 'antd';ReactDOM.render(<><Badge size="default" count={5}><a href="#" className="head-example" /></Badge><Badge size="small" count={5}><a href="#" className="head-example" /></Badge></>,mountNode,);
API
Badge
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| color | 自定义小圆点的颜色 | string | - | |
| count | 展示的数字,大于 overflowCount 时显示为 ${overflowCount}+,为 0 时隐藏 | ReactNode | - | |
| dot | 不展示数字,只有一个小红点 | boolean | false | |
| offset | 设置状态点的位置偏移 | [number, number] | - | |
| overflowCount | 展示封顶的数字值 | number | 99 | |
| showZero | 当数值为 0 时,是否展示 Badge | boolean | false | |
| size | 在设置了 count 的前提下有效,设置小圆点的大小 | default | small | - | 4.6.0 |
| status | 设置 Badge 为状态点 | success | processing | default | error | warning | - | |
| text | 在设置了 status 的前提下有效,设置状态点的文本 | ReactNode | - | |
| title | 设置鼠标放在状态点上时显示的文字 | string | - |
Badge.Ribbon (4.5.0+)
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| color | 自定义缎带的颜色 | string | - | |
| placement | 缎带的位置,start 和 end 随文字方向(RTL 或 LTR)变动 | start | end | end | |
| text | 缎带中填入的内容 | ReactNode | - |