Archived
generated from mitchell/rust_template
second round of impl
This commit is contained in:
+68
@@ -0,0 +1,68 @@
|
||||
use crate::{
|
||||
controls::{
|
||||
Control, ControlBuilder, ControlData, ValidationFn, VanityControl, VanityControlBuilder,
|
||||
VanityControlData,
|
||||
},
|
||||
styles::FormStyle,
|
||||
};
|
||||
use leptos::{IntoView, View};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct FormBuilder<FD: FormData, FS: FormStyle> {
|
||||
_fd: PhantomData<FD>,
|
||||
fs: FS,
|
||||
validations: Vec<Box<ValidationFn<FD>>>,
|
||||
views: Vec<View>,
|
||||
}
|
||||
|
||||
impl<FD: FormData, FS: FormStyle> FormBuilder<FD, FS> {
|
||||
pub fn new(form_style: FS) -> FormBuilder<FD, FS> {
|
||||
FormBuilder {
|
||||
_fd: PhantomData::default(),
|
||||
fs: form_style,
|
||||
validations: Vec::new(),
|
||||
views: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn new_vanity<C: VanityControlData + Default>(
|
||||
mut self,
|
||||
builder: impl Fn(VanityControlBuilder<FS, C>) -> VanityControl<FS, C>,
|
||||
) -> Self {
|
||||
let vanity_builder = VanityControlBuilder::new(C::default());
|
||||
let control = builder(vanity_builder);
|
||||
self.add_vanity(control);
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn new_control<C: ControlData + Default>(
|
||||
mut self,
|
||||
builder: impl Fn(ControlBuilder<FD, FS, C>) -> Control<FD, FS, C>,
|
||||
) -> Self {
|
||||
let control_builder = ControlBuilder::new(C::default());
|
||||
let control = builder(control_builder);
|
||||
self.add_control(control);
|
||||
self
|
||||
}
|
||||
|
||||
fn add_vanity<C: VanityControlData>(&mut self, vanity_control: VanityControl<FS, C>) {
|
||||
let view = VanityControlData::build_control::<FD, FS>(&self.fs, vanity_control);
|
||||
self.views.push(view);
|
||||
}
|
||||
|
||||
fn add_control<C: ControlData>(&mut self, control: Control<FD, FS, C>) {
|
||||
let view = ControlData::build_control(&self.fs, control);
|
||||
self.views.push(view);
|
||||
}
|
||||
|
||||
// TODO: this should return a Form object
|
||||
// The Form should have `form_view()`, and `validate(&FD)` functions.
|
||||
pub fn build(self) -> View {
|
||||
self.views.into_view()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FormData: Default {
|
||||
// TODO: this should return a Form Object
|
||||
fn create_form() -> View;
|
||||
}
|
||||
Reference in New Issue
Block a user