leptos_form_tool/controls/
radio_buttons.rs

1use super::{
2    BuilderCxFn, BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData,
3    ValidationState,
4};
5use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle};
6use leptos::{
7    prelude::{AnyView, RwSignal, Signal},
8    reactive::wrappers::write::SignalSetter,
9};
10
11/// Data used for the radio buttons control.
12#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
13pub struct RadioButtonsData {
14    pub name: String,
15    pub label: Option<String>,
16    /// The options for the select.
17    ///
18    /// The first value is the string to display, the second is the value.
19    pub options: Vec<(String, String)>,
20}
21
22impl<FD: FormToolData> ControlData<FD> for RadioButtonsData {
23    type ReturnType = String;
24
25    fn render_control<FS: FormStyle>(
26        fs: &FS,
27        _fd: RwSignal<FD>,
28        control: ControlRenderData<FS, Self>,
29        value_getter: Signal<Self::ReturnType>,
30        value_setter: SignalSetter<Self::ReturnType>,
31        validation_state: Signal<ValidationState>,
32    ) -> AnyView {
33        fs.radio_buttons(control, value_getter, value_setter, validation_state)
34    }
35}
36impl<FD: FormToolData> ValidatedControlData<FD> for RadioButtonsData {}
37
38impl<FD: FormToolData> FormBuilder<FD> {
39    /// Builds a radio buttons control and adds it to the form.
40    pub fn radio_buttons<FDT: Clone + PartialEq + 'static>(
41        self,
42        builder: impl BuilderFn<ControlBuilder<FD, RadioButtonsData, FDT>>,
43    ) -> Self {
44        self.new_control(builder)
45    }
46
47    /// Builds a radio buttons control using the form's context and adds it to
48    /// the form.
49    pub fn radio_buttons_cx<FDT: Clone + PartialEq + 'static>(
50        self,
51        builder: impl BuilderCxFn<ControlBuilder<FD, RadioButtonsData, FDT>, FD::Context>,
52    ) -> Self {
53        self.new_control_cx(builder)
54    }
55}
56
57impl<FD: FormToolData, FDT> ControlBuilder<FD, RadioButtonsData, FDT> {
58    /// Sets the name of the radio button inputs.
59    ///
60    /// This is used for the html element's "name" attribute.
61    /// In forms, the name attribute is the key that the data is sent
62    /// with.
63    pub fn named(mut self, control_name: impl ToString) -> Self {
64        self.data.name = control_name.to_string();
65        self
66    }
67
68    /// Sets the label for the radio button group.
69    pub fn labeled(mut self, label: impl ToString) -> Self {
70        self.data.label = Some(label.to_string());
71        self
72    }
73
74    /// Adds the option to the radio button group.
75    pub fn with_option(mut self, option: impl ToString) -> Self {
76        self.data
77            .options
78            .push((option.to_string(), option.to_string()));
79        self
80    }
81
82    /// Adds the option to the radio button group, specifying a different
83    /// value than what is displayed.
84    pub fn with_option_valued(mut self, display: impl ToString, value: impl ToString) -> Self {
85        self.data
86            .options
87            .push((display.to_string(), value.to_string()));
88        self
89    }
90
91    /// Adds all the options in the provided iterator to the radio button
92    /// group.
93    pub fn with_options(mut self, options: impl Iterator<Item = impl ToString>) -> Self {
94        for option in options {
95            self.data
96                .options
97                .push((option.to_string(), option.to_string()));
98        }
99        self
100    }
101
102    /// Adds all the (display_string, value) pairs in the provided iterator
103    /// to the radio button group.
104    pub fn with_options_valued(
105        mut self,
106        options: impl Iterator<Item = (impl ToString, impl ToString)>,
107    ) -> Self {
108        for option in options {
109            self.data
110                .options
111                .push((option.0.to_string(), option.1.to_string()));
112        }
113        self
114    }
115}