generated from mitchell/rust_template
49 lines
1.2 KiB
Rust
49 lines
1.2 KiB
Rust
use leptos::{Signal, View};
|
|
|
|
use super::{ControlBuilder, ControlData, ControlRenderData};
|
|
use crate::{
|
|
form::{FormBuilder, FormData},
|
|
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>,
|
|
validation_state: Signal<Result<(), String>>,
|
|
) -> (View, Signal<Self::ReturnType>) {
|
|
fs.select(control, validation_state)
|
|
}
|
|
}
|
|
|
|
impl<FD: FormData, FS: FormStyle> FormBuilder<FD, FS> {
|
|
pub fn select<FDT>(
|
|
self,
|
|
builder: impl Fn(
|
|
ControlBuilder<FD, FS, SelectData, FDT>,
|
|
) -> ControlBuilder<FD, FS, SelectData, FDT>,
|
|
) -> Self {
|
|
self.new_control(builder)
|
|
}
|
|
}
|
|
|
|
impl<FD: FormData, 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
|
|
}
|
|
}
|