use crate::{
core::{assets, attrs::component_attributes},
prelude::*,
theme::use_stylesheets,
};
const BUTTONS: Bem = Bem::new("fc-button-group");
const CONTROLS: Bem = Bem::new("fc-control-group");
#[component]
pub fn ButtonGroup(
label: AccessibleName,
#[props(default)] class: Option<String>,
#[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
children: Element,
) -> Element {
super::provide_group_item(BUTTONS.element("item"), None);
let attributes = component_attributes(
attributes,
ClassList::new(BUTTONS.block()).extend(class.as_deref()),
&["role", "aria-label"],
);
rsx! {
if use_stylesheets() { document::Stylesheet { href: assets::COMPONENTS_BUTTON_GROUP_CSS } }
div { role: "group", aria_label: label.to_string(), ..attributes, {children} }
}
}
fc_modifier! {
pub enum ControlLayout for CONTROLS {
Wrap => None,
Joined => "joined",
Vertical => "vertical",
Plain => "plain",
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum GroupName {
Label(AccessibleName),
Legend(Element),
}
#[component]
pub fn ControlGroup(
name: GroupName,
#[props(default)] layout: ControlLayout,
#[props(default)]
disabled: bool,
#[props(default)] class: Option<String>,
#[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
children: Element,
) -> Element {
let joined = layout == ControlLayout::Joined;
super::provide_group_item(
CONTROLS.element(if joined { "joined-item" } else { "item" }),
joined.then(|| CONTROLS.element("control")),
);
let attributes = component_attributes(
attributes,
ClassList::new(CONTROLS.block())
.with_modifier(CONTROLS, layout)
.extend(class.as_deref()),
&["role", "aria-label", "disabled"],
);
rsx! {
if use_stylesheets() { document::Stylesheet { href: assets::COMPONENTS_CONTROL_GROUP_CSS } }
match name {
GroupName::Label(label) => rsx! {
div { role: "group", aria_label: label.to_string(), ..attributes, {children} }
},
GroupName::Legend(legend) => rsx! {
fieldset { disabled, ..attributes,
legend { class: CONTROLS.element("label"), {legend} }
{children}
}
},
}
}
}
#[component]
pub fn ControlLabel(r#for: Id, children: Element) -> Element {
rsx! { label { class: CONTROLS.element("label"), r#for: format!("{}", r#for), {children} } }
}