generated from mitchell/rust_template
48 lines
1.4 KiB
Rust
48 lines
1.4 KiB
Rust
use leptos::{Signal, View};
|
|
|
|
use super::{ControlBuilder, ControlData, ControlRenderData};
|
|
use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle};
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
|
pub struct SelectData {
|
|
pub(crate) name: String,
|
|
pub(crate) options: Vec<String>,
|
|
}
|
|
|
|
impl ControlData for SelectData {
|
|
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.select(control, value_getter, value_setter, validation_state)
|
|
}
|
|
}
|
|
|
|
impl<FD: FormToolData, FS: FormStyle> FormBuilder<FD, FS> {
|
|
pub fn select<FDT: Clone + PartialEq + 'static>(
|
|
self,
|
|
builder: impl Fn(
|
|
ControlBuilder<FD, FS, SelectData, FDT>,
|
|
) -> ControlBuilder<FD, FS, SelectData, FDT>,
|
|
) -> Self {
|
|
self.new_control(builder)
|
|
}
|
|
}
|
|
|
|
impl<FD: FormToolData, FS: FormStyle, FDT> ControlBuilder<FD, FS, SelectData, FDT> {
|
|
pub fn options(mut self, options: Vec<String>) -> Self {
|
|
self.data.options = options;
|
|
self
|
|
}
|
|
|
|
pub fn and_option(mut self, option: impl ToString) -> Self {
|
|
self.data.options.push(option.to_string());
|
|
self
|
|
}
|
|
}
|