#![doc = include_str!("../DIOXUS.md")]
use crate::common::{
Color, Placement, Shape, Size, Variant, base_anchor_style, base_badge_style, base_label_style,
};
use dioxus::prelude::*;
#[derive(Props, Clone, PartialEq)]
pub struct AnchorProps {
#[props(default)]
pub children: Element,
#[props(default)]
pub class: &'static str,
#[props(default)]
pub style: &'static str,
#[props(default)]
pub id: &'static str,
#[props(default)]
pub data_testid: &'static str,
}
#[component]
pub fn Anchor(props: AnchorProps) -> Element {
rsx! {
span {
id: props.id,
class: "badge-anchor {props.class}",
style: "{base_anchor_style()} {props.style}",
"data-testid": props.data_testid,
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct LabelProps {
#[props(default)]
pub children: Element,
#[props(default)]
pub class: &'static str,
#[props(default)]
pub style: &'static str,
#[props(default)]
pub id: &'static str,
}
#[component]
pub fn Label(props: LabelProps) -> Element {
rsx! {
span {
id: props.id,
class: "badge__label {props.class}",
style: "{base_label_style()} {props.style}",
{props.children}
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct BadgeProps {
#[props(default = None)]
pub children: Option<Element>,
#[props(default = Color::Default)]
pub color: Color,
#[props(default = Variant::Primary)]
pub variant: Variant,
#[props(default = Size::Md)]
pub size: Size,
#[props(default = Placement::TopRight)]
pub placement: Placement,
#[props(default = Shape::Rectangle)]
pub shape: Shape,
#[props(default)]
pub class: &'static str,
#[props(default)]
pub style: &'static str,
#[props(default)]
pub id: &'static str,
#[props(default = "Badge")]
pub aria_label: &'static str,
#[props(default)]
pub data_testid: &'static str,
}
#[component]
pub fn Badge(props: BadgeProps) -> Element {
let has_children = props.children.is_some();
let color_style = match props.variant {
Variant::Primary => props.color.to_primary_style(),
Variant::Secondary => props.color.to_secondary_style(),
Variant::Soft => props.color.to_soft_style(),
};
let size_style = if has_children {
props.size.to_label_style()
} else {
props.size.to_dot_style()
};
let badge_class = format!(
"badge {} {} {} {} {}",
props.variant.to_class(),
props.color.to_class(),
props.size.to_class(),
props.placement.to_class(),
props.class,
);
let full_style = format!(
"{} {} {} {} {}",
base_badge_style(),
color_style,
size_style,
props.placement.to_style(props.shape),
props.style,
);
rsx! {
span {
id: props.id,
class: "{badge_class}",
style: "{full_style}",
role: "status",
aria_label: props.aria_label,
aria_atomic: "true",
"data-testid": props.data_testid,
if let Some(children) = props.children {
Label { {children} }
}
}
}
}