dioxus_bootstrap/
badge.rs

1use dioxus::prelude::*;
2
3#[derive(Clone, Copy, PartialEq)]
4pub enum BadgeVariant {
5    Primary,
6    Secondary,
7    Success,
8    Danger,
9    Warning,
10    Info,
11    Light,
12    Dark,
13}
14
15impl Into<&'static str> for BadgeVariant {
16    fn into(self) -> &'static str {
17        match self {
18            BadgeVariant::Primary => "text-bg-primary",
19            BadgeVariant::Secondary => "text-bg-secondary",
20            BadgeVariant::Success => "text-bg-success",
21            BadgeVariant::Danger => "text-bg-danger",
22            BadgeVariant::Warning => "text-bg-warning",
23            BadgeVariant::Info => "text-bg-info",
24            BadgeVariant::Light => "text-bg-light",
25            BadgeVariant::Dark => "text-bg-dark",
26        }
27    }
28}
29
30#[derive(Clone, Props, PartialEq)]
31pub struct BadgeProps {
32    #[props(optional)]
33    id: String,
34    #[props(optional, default = "".to_string())]
35    class: String,
36    #[props(optional, default = BadgeVariant::Primary)]
37    variant: BadgeVariant,
38    #[props(optional, default = false)]
39    pill: bool,
40    children: Element,
41}
42
43#[component]
44pub fn Badge(props: BadgeProps) -> Element {
45    let mut class_list = vec!["badge".to_string()];
46    
47    let variant_class: &str = props.variant.into();
48    class_list.push(variant_class.to_string());
49    
50    if props.pill {
51        class_list.push("rounded-pill".to_string());
52    }
53    
54    if !props.class.is_empty() {
55        class_list.push(props.class.clone());
56    }
57    
58    let class_list = class_list.join(" ");
59    
60    rsx! {
61        span {
62            id: props.id,
63            class: class_list,
64            {props.children}
65        }
66    }
67}