generated from mitchell/rust_template
75 lines
2.1 KiB
Rust
75 lines
2.1 KiB
Rust
use std::ops::RangeInclusive;
|
|
|
|
use leptos::{Signal, View};
|
|
|
|
use super::{ControlBuilder, ControlData, ControlRenderData, ValidatedControlData};
|
|
use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle};
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
|
pub struct StepperData {
|
|
pub(crate) name: String,
|
|
pub(crate) label: Option<String>,
|
|
pub(crate) step: Option<i32>,
|
|
pub(crate) min: Option<i32>,
|
|
pub(crate) max: Option<i32>,
|
|
}
|
|
|
|
impl ControlData for StepperData {
|
|
type ReturnType = String;
|
|
|
|
fn build_control<FS: FormStyle>(
|
|
fs: &FS,
|
|
control: ControlRenderData<FS, Self>,
|
|
value_getter: Signal<Self::ReturnType>,
|
|
value_setter: Box<dyn Fn(Self::ReturnType)>,
|
|
validation_state: Signal<Result<(), String>>,
|
|
) -> View {
|
|
fs.stepper(control, value_getter, value_setter, validation_state)
|
|
}
|
|
}
|
|
impl ValidatedControlData for StepperData {}
|
|
|
|
impl<FD: FormToolData, FS: FormStyle, CX: 'static> FormBuilder<FD, FS, CX> {
|
|
pub fn stepper<FDT: Clone + PartialEq + 'static>(
|
|
self,
|
|
builder: impl Fn(
|
|
ControlBuilder<FD, FS, StepperData, FDT>,
|
|
) -> ControlBuilder<FD, FS, StepperData, FDT>,
|
|
) -> Self {
|
|
self.new_control(builder)
|
|
}
|
|
}
|
|
|
|
impl<FD: FormToolData, FS: FormStyle, FDT> ControlBuilder<FD, FS, StepperData, 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: i32) -> Self {
|
|
self.data.step = Some(step);
|
|
self
|
|
}
|
|
|
|
pub fn min(mut self, min: i32) -> Self {
|
|
self.data.min = Some(min);
|
|
self
|
|
}
|
|
|
|
pub fn max(mut self, max: i32) -> Self {
|
|
self.data.max = Some(max);
|
|
self
|
|
}
|
|
|
|
pub fn range(mut self, range: RangeInclusive<i32>) -> Self {
|
|
self.data.min = Some(*range.start());
|
|
self.data.max = Some(*range.end());
|
|
self
|
|
}
|
|
}
|