dioxus_tw_components/components/atoms/buttongroup/
props.rs

1use crate::attributes::*;
2use dioxus::prelude::*;
3use dioxus_tw_components_macro::UiComp;
4
5#[derive(Clone, PartialEq, Props, UiComp)]
6pub struct ButtonGroupProps {
7    #[props(extends = div, extends = GlobalAttributes)]
8    attributes: Vec<Attribute>,
9
10    #[props(optional, default)]
11    pub color: ReadOnlySignal<Color>,
12    #[props(optional, default)]
13    pub size: ReadOnlySignal<Size>,
14    #[props(optional, default)]
15    pub animation: ReadOnlySignal<Animation>,
16
17    children: Element,
18}
19
20impl std::default::Default for ButtonGroupProps {
21    fn default() -> Self {
22        Self {
23            attributes: Vec::<Attribute>::default(),
24            color: ReadOnlySignal::<Color>::default(),
25            size: ReadOnlySignal::<Size>::default(),
26            animation: ReadOnlySignal::<Animation>::default(),
27            children: rsx! {},
28        }
29    }
30}
31
32#[derive(Default, Clone)]
33struct FieldData {
34    pub color: ReadOnlySignal<Color>,
35    pub size: ReadOnlySignal<Size>,
36    pub animation: ReadOnlySignal<Animation>,
37}
38
39#[component]
40pub fn ButtonGroup(mut props: ButtonGroupProps) -> Element {
41    props.update_class_attribute();
42    let _class = use_context_provider(|| {
43        let data = FieldData {
44            color: props.color,
45            size: props.size,
46            animation: props.animation,
47        };
48        Signal::new(data)
49    });
50
51    rsx! {
52        div { ..props.attributes,{props.children} }
53    }
54}
55
56#[derive(Clone, PartialEq, Props, UiComp)]
57pub struct ButtonGroupItemProps {
58    #[props(extends = button, extends = GlobalAttributes)]
59    attributes: Vec<Attribute>,
60
61    #[props(optional, default)]
62    pub(crate) color: ReadOnlySignal<Color>,
63    #[props(optional, default)]
64    pub(crate) size: ReadOnlySignal<Size>,
65    #[props(optional, default)]
66    pub(crate) animation: ReadOnlySignal<Animation>,
67
68    #[props(optional)]
69    onclick: EventHandler<MouseEvent>,
70    #[props(optional)]
71    onmouseenter: EventHandler<MouseEvent>,
72    #[props(optional)]
73    onmouseleave: EventHandler<MouseEvent>,
74    #[props(optional)]
75    onfocus: EventHandler<FocusEvent>,
76
77    children: Element,
78}
79
80impl std::default::Default for ButtonGroupItemProps {
81    fn default() -> Self {
82        Self {
83            attributes: Vec::<Attribute>::default(),
84            color: ReadOnlySignal::<Color>::default(),
85            size: ReadOnlySignal::<Size>::default(),
86            animation: ReadOnlySignal::<Animation>::default(),
87            onclick: EventHandler::<MouseEvent>::default(),
88            onmouseenter: EventHandler::<MouseEvent>::default(),
89            onmouseleave: EventHandler::<MouseEvent>::default(),
90            onfocus: EventHandler::<FocusEvent>::default(),
91            children: rsx! {},
92        }
93    }
94}
95
96#[component]
97pub fn ButtonGroupItem(mut props: ButtonGroupItemProps) -> Element {
98    let data = use_context::<Signal<FieldData>>();
99    props.color = data.read().color;
100    props.size = data.read().size;
101    props.animation = data.read().animation;
102
103    props.update_class_attribute();
104
105    let onclick = move |event| props.onclick.call(event);
106    let onmouseenter = move |event| props.onmouseenter.call(event);
107    let onmouseleave = move |event| props.onmouseleave.call(event);
108    let onfocus = move |event| props.onfocus.call(event);
109
110    rsx! {
111        button {
112            onclick,
113            onmouseenter,
114            onmouseleave,
115            onfocus,
116            ..props.attributes,
117            {props.children}
118        }
119    }
120}