use super::{
BuilderCxFn, BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidationState,
};
use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle};
use leptos::{
prelude::{AnyView, RwSignal, Signal},
reactive::wrappers::write::SignalSetter,
};
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct SliderData {
pub name: String,
pub label: Option<String>,
pub step: Option<Signal<String>>,
pub min: Option<Signal<String>>,
pub max: Option<Signal<String>>,
}
impl<FD: FormToolData> ControlData<FD> for SliderData {
type ReturnType = String;
fn render_control<FS: FormStyle>(
fs: &FS,
_fd: RwSignal<FD>,
control: ControlRenderData<FS, Self>,
value_getter: Signal<Self::ReturnType>,
value_setter: SignalSetter<Self::ReturnType>,
validation_state: Signal<ValidationState>,
) -> AnyView {
fs.slider(control, value_getter, value_setter, validation_state)
}
}
impl<FD: FormToolData> FormBuilder<FD> {
pub fn slider<FDT: Clone + PartialEq + 'static>(
self,
builder: impl BuilderFn<ControlBuilder<FD, SliderData, FDT>>,
) -> Self {
self.new_control(builder)
}
pub fn slider_cx<FDT: Clone + PartialEq + 'static>(
self,
builder: impl BuilderCxFn<ControlBuilder<FD, SliderData, FDT>, FD::Context>,
) -> Self {
self.new_control_cx(builder)
}
}
impl<FD: FormToolData, FDT> ControlBuilder<FD, SliderData, FDT> {
pub fn named(mut self, control_name: impl ToString) -> Self {
self.data.name = control_name.to_string();
self
}
pub fn labeled(mut self, label: impl ToString) -> Self {
self.data.label = Some(label.to_string());
self
}
pub fn step(mut self, step: impl ToString) -> Self {
self.data.step = Some(Signal::stored(step.to_string()));
self
}
pub fn step_signal(mut self, step: Signal<String>) -> Self {
self.data.step = Some(step);
self
}
pub fn min(mut self, min: impl ToString) -> Self {
self.data.min = Some(Signal::stored(min.to_string()));
self
}
pub fn min_signal(mut self, min: Signal<String>) -> Self {
self.data.min = Some(min);
self
}
pub fn max(mut self, max: impl ToString) -> Self {
self.data.max = Some(Signal::stored(max.to_string()));
self
}
pub fn max_signal(mut self, max: Signal<String>) -> Self {
self.data.max = Some(max);
self
}
}