use super::{BuilderCxFn, BuilderFn, ControlBuilder, ControlData, ControlRenderData}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{MaybeSignal, Signal, View}; use std::rc::Rc; /// Data used for the slider control. #[derive(Debug, Clone, PartialEq, Eq)] pub struct SliderData { pub name: String, pub label: Option, pub min: MaybeSignal, pub max: MaybeSignal, } impl Default for SliderData { fn default() -> Self { SliderData { name: String::new(), label: None, min: MaybeSignal::Static(0), max: MaybeSignal::Static(1), } } } impl ControlData for SliderData { type ReturnType = i32; fn build_control( fs: &FS, control: Rc>, value_getter: Signal, value_setter: Rc, validation_state: Signal>, ) -> View { fs.slider(control, value_getter, value_setter, validation_state) } } impl FormBuilder { /// Builds a slider (or range) control and adds it to the form. pub fn slider( self, builder: impl BuilderFn>, ) -> Self { self.new_control(builder) } /// Bulids a slider (or range) control using the form's context and adds /// it to the form. pub fn slider_cx( self, builder: impl BuilderCxFn, FD::Context>, ) -> Self { self.new_control_cx(builder) } } impl ControlBuilder { /// Sets the name of the slider. /// /// This is used for the html element's "name" attribute. /// In forms, the name attribute is the key that the data is sent /// with. pub fn named(mut self, control_name: impl ToString) -> Self { self.data.name = control_name.to_string(); self } /// Sets the label for the slider. pub fn labeled(mut self, label: impl ToString) -> Self { self.data.label = Some(label.to_string()); self } /// Sets the minimum value for the slider. pub fn min(mut self, min: i32) -> Self { self.data.min = MaybeSignal::Static(min); self } /// Sets the minimum value for the slider to a signal. pub fn min_signal(mut self, min: Signal) -> Self { self.data.min = MaybeSignal::Dynamic(min); self } /// Sets the maximum value for the slider. pub fn max(mut self, max: i32) -> Self { self.data.max = MaybeSignal::Static(max); self } /// Sets the maximum value for the slider to a signal. pub fn max_signal(mut self, max: Signal) -> Self { self.data.max = MaybeSignal::Dynamic(max); self } }