fc_dioxus/components/
button_group.rs1use crate::{
4 core::{assets, attrs::component_attributes},
5 prelude::*,
6 theme::use_stylesheets,
7};
8const BUTTONS: Bem = Bem::new("fc-button-group");
9const CONTROLS: Bem = Bem::new("fc-control-group");
10
11#[component]
14pub fn ButtonGroup(
15 label: AccessibleName,
16 #[props(default)] class: Option<String>,
17 #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
18 children: Element,
19) -> Element {
20 super::provide_group_item(BUTTONS.element("item"), None);
21 let attributes = component_attributes(
22 attributes,
23 ClassList::new(BUTTONS.block()).extend(class.as_deref()),
24 &["role", "aria-label"],
25 );
26 rsx! {
27 if use_stylesheets() { document::Stylesheet { href: assets::COMPONENTS_BUTTON_GROUP_CSS } }
28 div { role: "group", aria_label: label.to_string(), ..attributes, {children} }
29 }
30}
31
32fc_modifier! {
33 pub enum ControlLayout for CONTROLS {
35 Wrap => None,
37 Joined => "joined",
39 Vertical => "vertical",
41 Plain => "plain",
43 }
44}
45
46#[derive(Debug, Clone, PartialEq)]
48pub enum GroupName {
49 Label(AccessibleName),
51 Legend(Element),
53}
54
55#[component]
58pub fn ControlGroup(
59 name: GroupName,
60 #[props(default)] layout: ControlLayout,
61 #[props(default)]
63 disabled: bool,
64 #[props(default)] class: Option<String>,
65 #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>,
66 children: Element,
67) -> Element {
68 let joined = layout == ControlLayout::Joined;
69 super::provide_group_item(
70 CONTROLS.element(if joined { "joined-item" } else { "item" }),
71 joined.then(|| CONTROLS.element("control")),
72 );
73 let attributes = component_attributes(
74 attributes,
75 ClassList::new(CONTROLS.block())
76 .with_modifier(CONTROLS, layout)
77 .extend(class.as_deref()),
78 &["role", "aria-label", "disabled"],
79 );
80 rsx! {
81 if use_stylesheets() { document::Stylesheet { href: assets::COMPONENTS_CONTROL_GROUP_CSS } }
82 match name {
83 GroupName::Label(label) => rsx! {
84 div { role: "group", aria_label: label.to_string(), ..attributes, {children} }
85 },
86 GroupName::Legend(legend) => rsx! {
87 fieldset { disabled, ..attributes,
88 legend { class: CONTROLS.element("label"), {legend} }
89 {children}
90 }
91 },
92 }
93 }
94}
95
96#[component]
98pub fn ControlLabel(r#for: Id, children: Element) -> Element {
99 rsx! { label { class: CONTROLS.element("label"), r#for: format!("{}", r#for), {children} } }
103}