Archived
generated from mitchell/rust_template
before removing VanityControl and Control
This commit is contained in:
+32
-6
@@ -5,11 +5,16 @@ use crate::{
|
||||
},
|
||||
styles::FormStyle,
|
||||
};
|
||||
use leptos::{IntoView, View};
|
||||
use std::marker::PhantomData;
|
||||
use leptos::{
|
||||
create_effect, create_signal, IntoView, ReadSignal, SignalGet, SignalSet, SignalUpdate, View,
|
||||
WriteSignal,
|
||||
};
|
||||
|
||||
pub struct Form {}
|
||||
|
||||
pub struct FormBuilder<FD: FormData, FS: FormStyle> {
|
||||
_fd: PhantomData<FD>,
|
||||
fd_get: ReadSignal<FD>,
|
||||
fd_set: WriteSignal<FD>,
|
||||
fs: FS,
|
||||
validations: Vec<Box<ValidationFn<FD>>>,
|
||||
views: Vec<View>,
|
||||
@@ -17,8 +22,10 @@ pub struct FormBuilder<FD: FormData, FS: FormStyle> {
|
||||
|
||||
impl<FD: FormData, FS: FormStyle> FormBuilder<FD, FS> {
|
||||
pub fn new(form_style: FS) -> FormBuilder<FD, FS> {
|
||||
let (fs_get, fs_set) = create_signal(FD::default());
|
||||
FormBuilder {
|
||||
_fd: PhantomData::default(),
|
||||
fd_get,
|
||||
fd_set,
|
||||
fs: form_style,
|
||||
validations: Vec::new(),
|
||||
views: Vec::new(),
|
||||
@@ -51,7 +58,26 @@ impl<FD: FormData, FS: FormStyle> FormBuilder<FD, FS> {
|
||||
}
|
||||
|
||||
fn add_control<C: ControlData>(&mut self, control: Control<FD, FS, C>) {
|
||||
let view = ControlData::build_control(&self.fs, control);
|
||||
let (validation_signal, validation_signal_set) = create_signal(Ok(()));
|
||||
let (view, control_value) = ControlData::build_control(&self.fs, control);
|
||||
// TODO: add a signal that triggers on submit to refresh the validation on_submit
|
||||
// TODO: we might want a way to see if this is the first time this ran, which would
|
||||
// prevent the form's validation to pop up before the user typed anything in
|
||||
// TODO: add validation here that run on the input changing, and writes
|
||||
// it to the fd signals
|
||||
create_effect(move |last_value| {
|
||||
let control_value = control_value.get();
|
||||
let mut validation_result;
|
||||
self.fd_set.update(|v| {
|
||||
validation_result =
|
||||
(control.parse_fn)(control_value, v).and_then(|_| (*control.validation)(v));
|
||||
});
|
||||
// TODO: or this happened on a submit
|
||||
if Some(validation_result) != last_value {
|
||||
validation_signal_set.set(validation_result);
|
||||
}
|
||||
validation_result
|
||||
});
|
||||
self.views.push(view);
|
||||
}
|
||||
|
||||
@@ -62,7 +88,7 @@ impl<FD: FormData, FS: FormStyle> FormBuilder<FD, FS> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FormData: Default {
|
||||
pub trait FormData: Default + Clone + 'static {
|
||||
// TODO: this should return a Form Object
|
||||
fn create_form() -> View;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user