use super::{BuilderFn, ControlBuilder, ControlData, ControlRenderData, ValidatedControlData}; use crate::{form::FormToolData, form_builder::FormBuilder, styles::FormStyle}; use leptos::{Signal, View}; use std::rc::Rc; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] pub struct SelectData { pub name: String, pub label: Option, /// The options for the select. /// /// The first value is the string to display, the second is the value. pub options: Vec<(String, String)>, } impl ControlData for SelectData { type ReturnType = String; fn build_control( fs: &FS, control: Rc>, value_getter: Signal, value_setter: Rc, validation_state: Signal>, ) -> View { fs.select(control, value_getter, value_setter, validation_state) } } impl ValidatedControlData for SelectData {} impl FormBuilder { pub fn select( self, builder: impl BuilderFn, FD::Context>, ) -> Self { self.new_control(builder) } } impl ControlBuilder { 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 with_option(mut self, option: impl ToString) -> Self { self.data .options .push((option.to_string(), option.to_string())); self } pub fn with_option_valued(mut self, display: impl ToString, value: impl ToString) -> Self { self.data .options .push((display.to_string(), value.to_string())); self } pub fn with_options(mut self, options: impl Iterator) -> Self { for option in options { self.data .options .push((option.to_string(), option.to_string())); } self } pub fn with_options_valued( mut self, options: impl Iterator, ) -> Self { for option in options { self.data .options .push((option.0.to_string(), option.1.to_string())); } self } }