This commit is contained in:
2024-05-20 09:51:01 -05:00
parent 68d6b857ed
commit 74407d6c0a
5 changed files with 72 additions and 42 deletions
+50 -30
View File
@@ -7,10 +7,8 @@ use crate::{
},
styles::FormStyle,
};
use leptos::{
create_rw_signal, create_signal, IntoSignal, IntoView, RwSignal, SignalGet, SignalSet,
SignalUpdate, View,
};
use leptos::*;
use leptos_router::Form;
pub struct Validator<FD: FormData> {
validations: Vec<Rc<dyn ValidationFn<FD>>>,
@@ -102,20 +100,8 @@ pub struct FormBuilder<FD: FormData, FS: FormStyle> {
}
impl<FD: FormData, FS: FormStyle> FormBuilder<FD, FS> {
// TODO: remove the Default trait bound and bind it to this function only
fn new_full_builder(form_style: FS) -> FormBuilder<FD, FS> {
let fd = create_rw_signal(FD::default());
FormBuilder {
inner: FormBuilderInner::FullBuilder(FullFormBuilder {
fd,
fs: form_style,
validations: Vec::new(),
views: Vec::new(),
}),
}
}
fn new_full_builder_with(starting_data: FD, form_style: FS) -> FormBuilder<FD, FS> {
// TODO: remove the Default trait bound
fn new_full_builder(starting_data: FD, form_style: FS) -> FormBuilder<FD, FS> {
let fd = create_rw_signal(starting_data);
FormBuilder {
inner: FormBuilderInner::FullBuilder(FullFormBuilder {
@@ -267,12 +253,38 @@ impl<FD: FormData, FS: FormStyle> FormBuilder<FD, FS> {
)
}
fn build(self) -> Option<Form<FD>> {
fn build(
self,
action_location: String,
action: Option<Action<FD, Result<(), ServerFnError>>>,
) -> Option<Form<FD>> {
let builder = match self.inner {
FormBuilderInner::FullBuilder(fb) => fb,
FormBuilderInner::ValidationBuilder { validations: _ } => return None,
};
let view = builder.fs.form_frame(builder.views.into_view());
let validations = builder.validations.clone();
let validate_form = move || {
let fd = builder.fd.get();
validations.iter().all(|v| v(&fd).is_ok())
};
let elements = builder.fs.form_frame(builder.views.into_view());
let view = view! {
<Form action=action_location
on:submit=move |ev| {
if !validate_form() {
ev.prevent_default();
return;
}
if let Some(action) = action {
action.dispatch(builder.fd.get());
}
}>
{elements}
</Form>
}
.into_view();
Some(Form {
fd: builder.fd,
@@ -281,10 +293,10 @@ impl<FD: FormData, FS: FormStyle> FormBuilder<FD, FS> {
})
}
fn validator(self) -> Validator<FD> {
let validations = match self.inner {
FormBuilderInner::FullBuilder(fb) => fb.validations,
FormBuilderInner::ValidationBuilder { validations } => validations,
fn validator(&self) -> Validator<FD> {
let validations = match &self.inner {
FormBuilderInner::FullBuilder(fb) => fb.validations.clone(),
FormBuilderInner::ValidationBuilder { validations } => validations.clone(),
};
Validator { validations }
}
@@ -309,16 +321,24 @@ pub trait FormData: Default + Clone + 'static {
/// Constructs a [`Form`] for this FormData type.
///
/// The [`Form`] provides the way to render the form.
fn get_form(style: Self::Style) -> Form<Self> {
let builder = FormBuilder::new_full_builder(style);
fn get_form(self, action: impl ToString, style: Self::Style) -> Form<Self> {
let builder = FormBuilder::new_full_builder(self, style);
let builder = Self::build_form(builder);
builder.build().expect("builder should be full builder")
builder
.build(action.to_string(), None)
.expect("builder should be full builder")
}
fn get_form_with_starting_data(self, style: Self::Style) -> Form<Self> {
let builder = FormBuilder::new_full_builder_with(self, style);
fn get_action_form(
self,
action: Action<Self, Result<(), ServerFnError>>,
style: Self::Style,
) -> Form<Self> {
let builder = FormBuilder::new_full_builder(self, style);
let builder = Self::build_form(builder);
builder.build().expect("builder should be full builder")
builder
.build(action.url().unwrap_or(String::new()), Some(action))
.expect("builder should be full builder")
}
fn get_validator() -> Validator<Self> {