Badge 徽章
按钮和图标上的数字或状态标记。
基础用法
可以用来展示新消息的数量。
12
3
1
2
1
export function BadgeBasic() {
return (
<>
<ElBadge value={12} className="item" style={item}>
<ElButton>comments</ElButton>
</ElBadge>
<ElBadge value={3} className="item" style={item}>
<ElButton>replies</ElButton>
</ElBadge>
<ElBadge value={1} type="primary" className="item" style={item}>
<ElButton>comments</ElButton>
</ElBadge>
<ElBadge value={2} type="warning" className="item" style={item}>
<ElButton>replies</ElButton>
</ElBadge>
<ElBadge value={1} color="green" className="item" style={item}>
<ElButton>custom background</ElButton>
</ElBadge>
<span
style={{ ...dropdown, display: "inline-flex", verticalAlign: "top" }}
>
<ElDropdown
trigger="click"
menu={
<ElDropdownMenu>
<ElDropdownItem>
comments
<ElBadge value={12} className="mark" style={mark} />
</ElDropdownItem>
<ElDropdownItem>
replies
<ElBadge value={3} className="mark" style={mark} />
</ElDropdownItem>
</ElDropdownMenu>
}
>
<span style={dropdownLink}>
Click Me
<ElIcon className="el-icon--right">
<CaretBottom />
</ElIcon>
</span>
</ElDropdown>
</span>
</>
);
}
最大值
你还可以自定义最大值
由 max 属性定义,接受 Number 值。请注意,仅在值也是 Number 时起作用。
export function BadgeMax() {
return (
<>
<ElBadge value={200} max={99} className="item" style={itemWide}>
<ElButton>comments</ElButton>
</ElBadge>
<ElBadge value={100} max={10} className="item" style={itemWide}>
<ElButton>replies</ElButton>
</ElBadge>
</>
);
}
自定义显示内容
你也可以展示除数字以外你想要展示的任何值。或者您可以使用 content 栏位自定义内容。
当 value 是 String 时,可以显示自定义文字。或者使用 content 插槽。
export function BadgeCustomize() {
return (
<>
<ElBadge value="new" className="item" style={itemWide}>
<ElButton>comments</ElButton>
</ElBadge>
<ElBadge value="hot" className="item" style={itemWide}>
<ElButton>replies</ElButton>
</ElBadge>
<ElBadge
value={99}
className="item" style={itemWide}
content={(value) => (
<div style={customContent}>
<ElIcon>
<Message />
</ElIcon>
<span>{value}</span>
</div>
)}
>
<ElButton>share</ElButton>
</ElBadge>
</>
);
}
小红点
通过一个小红点标记来告知用户有新内容。
export function BadgeDot() {
return (
<>
<ElBadge isDot className="item" style={itemWide}>query</ElBadge>
<ElBadge isDot className="item" style={itemWide}>
<ElButton icon={<Share />} type="primary" />
</ElBadge>
</>
);
}
偏移量 2.7.0
设置徽章点的偏移,格式是[左,顶部], 代表状态点从左侧和默认位置顶部的偏移。
export function BadgeOffset() {
return (
<ElBadge value={1} offset={[10, 5]} className="item" style={item}>
<ElButton> offset</ElButton>
</ElBadge>
);
}
Badge API
Badge Attributes
| 属性名 | 说明 | 类型 | 默认值 |
|---|
| value | 显示值 | string / number | — |
| max | 最大值,超出显示 {max}+ 形式,仅 value 为数字时生效 | number | 99 |
| is-dot | 是否显示为小圆点 | boolean | false |
| hidden | 是否隐藏徽章 | boolean | false |
| type | 徽章类型 | enum | danger |
| show-zero | value 为 0 时是否显示 | boolean | true |
| color | 自定义圆点背景色 | string | — |
| badge-style | 徽章样式 | object | — |
| badge-class | 徽章类名 | string | — |
| offset | 徽章偏移量 [x, y] | array | [0, 0] |
Badge Slots
| 插槽名 | 说明 | React 写法 |
|---|
| default | 徽章包裹的内容 | children |
| content | 自定义徽章内容,参数为计算后的值 | content |