From 115ff1abde33ab24a95b6d95b498aa00adf829d9 Mon Sep 17 00:00:00 2001 From: Mitchell M Date: Wed, 12 Jun 2024 16:02:43 -0500 Subject: [PATCH] remove fd and fs from builder --- src/controls/button.rs | 10 +++++++++- src/controls/group.rs | 3 +-- src/form.rs | 12 ++++++------ src/form_builder.rs | 39 ++++++++++++++------------------------- src/styles/mod.rs | 2 +- 5 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/controls/button.rs b/src/controls/button.rs index e79556b..d47e803 100644 --- a/src/controls/button.rs +++ b/src/controls/button.rs @@ -4,11 +4,19 @@ use leptos::RwSignal; use std::rc::Rc; use web_sys::MouseEvent; -#[derive(Default, Clone)] +#[derive(Clone)] pub struct ButtonData { pub(crate) text: String, pub(crate) action: Option>, } +impl Default for ButtonData { + fn default() -> Self { + ButtonData { + text: String::default(), + action: None, + } + } +} impl FormBuilder { pub fn button( diff --git a/src/controls/group.rs b/src/controls/group.rs index 1832f41..1379205 100644 --- a/src/controls/group.rs +++ b/src/controls/group.rs @@ -4,9 +4,8 @@ use leptos::{CollectView, RwSignal}; impl FormBuilder { pub fn group(mut self, builder: impl Fn(FormBuilder) -> FormBuilder) -> Self { - let mut group_builder = FormBuilder::new_group(self.fd, self.fs); + let mut group_builder = FormBuilder::new(); group_builder = builder(group_builder); - self.fs = group_builder.fs; // take the style back for validation in group_builder.validations { self.validations.push(validation); diff --git a/src/form.rs b/src/form.rs index c5ed3d8..3d62d03 100644 --- a/src/form.rs +++ b/src/form.rs @@ -78,7 +78,7 @@ impl IntoView for Form { /// /// This trait defines a function that can be used to build all the data needed /// to physically lay out a form, and how that data should be parsed and validated. -pub trait FormToolData: Default + Clone + 'static { +pub trait FormToolData: Clone + 'static { type Style: FormStyle; /// Defines how the form should be layed out and how the data should be parsed and validated. @@ -97,9 +97,9 @@ pub trait FormToolData: Default + Clone + 'static { /// component. Call [`get_action_form`]\() to get the /// [`ActionForm`](leptos_router::ActionForm) version. fn get_form(self, action: impl ToString, style: Self::Style) -> Form { - let builder = FormBuilder::new(self, style); + let builder = FormBuilder::new(); let builder = Self::build_form(builder); - builder.build_plain_form(action.to_string()) + builder.build_plain_form(action.to_string(), self, style) } /// Constructs a [`Form`] for this [`FormToolData`] type. @@ -118,9 +118,9 @@ pub trait FormToolData: Default + Clone + 'static { <>::Request as ClientReq>::FormData: From, { - let builder = FormBuilder::new(self, style); + let builder = FormBuilder::new(); let builder = Self::build_form(builder); - builder.build_action_form(action) + builder.build_action_form(action, self, style) } /// Gets a [`Validator`] for this [`ToolFormData`]. @@ -132,7 +132,7 @@ pub trait FormToolData: Default + Clone + 'static { /// However, the code to render the views are not configured out, it /// simply doesn't run, so the view needs to compile even on the server. fn get_validator() -> FormValidator { - let builder = FormBuilder::new(Self::default(), Self::Style::default()); + let builder = FormBuilder::new(); let builder = Self::build_form(builder); builder.validator() } diff --git a/src/form_builder.rs b/src/form_builder.rs index 49cba10..f649a27 100644 --- a/src/form_builder.rs +++ b/src/form_builder.rs @@ -20,10 +20,6 @@ use web_sys::{FormData, SubmitEvent}; /// /// This builder allows you to specify what components should make up the form. pub struct FormBuilder { - /// The [`ToolFormData`] signal. - pub(crate) fd: RwSignal, - /// The [`FormStyle`]. - pub(crate) fs: FS, /// The list of [`ValidationFn`]s. pub(crate) validations: Vec>>, /// The list of functions that will render the form. @@ -34,21 +30,8 @@ pub struct FormBuilder { impl FormBuilder { /// Creates a new [`FormBuilder`] - pub(crate) fn new(starting_data: FD, form_style: FS) -> FormBuilder { - let fd = create_rw_signal(starting_data); + pub(crate) fn new() -> FormBuilder { FormBuilder { - fd, - fs: form_style, - validations: Vec::new(), - render_fns: Vec::new(), - styles: Vec::new(), - } - } - - pub(crate) fn new_group(fd: RwSignal, fs: FS) -> FormBuilder { - FormBuilder { - fd, - fs, validations: Vec::new(), render_fns: Vec::new(), styles: Vec::new(), @@ -232,19 +215,23 @@ impl FormBuilder { pub(crate) fn build_action_form( self, action: Action>>, + fd: FD, + fs: FS, ) -> Form where ServFn: DeserializeOwned + ServerFn + 'static, <>::Request as ClientReq>::FormData: From, { + let fd = create_rw_signal(fd); + let (views, validation_cbs): (Vec<_>, Vec<_>) = self .render_fns .into_iter() - .map(|r_fn| r_fn(&self.fs, self.fd)) + .map(|r_fn| r_fn(&fs, fd)) .unzip(); - let elements = self.fs.form_frame(views.into_view(), self.styles); + let elements = fs.form_frame(views.into_view(), self.styles); let on_submit = move |ev: SubmitEvent| { let mut failed = false; @@ -265,20 +252,22 @@ impl FormBuilder { }; Form { - fd: self.fd, + fd, validations: self.validations, view, } } - pub(crate) fn build_plain_form(self, url: String) -> Form { + pub(crate) fn build_plain_form(self, url: String, fd: FD, fs: FS) -> Form { + let fd = create_rw_signal(fd); + let (views, validation_cbs): (Vec<_>, Vec<_>) = self .render_fns .into_iter() - .map(|r_fn| r_fn(&self.fs, self.fd)) + .map(|r_fn| r_fn(&fs, fd)) .unzip(); - let elements = self.fs.form_frame(views.into_view(), self.styles); + let elements = fs.form_frame(views.into_view(), self.styles); let on_submit = move |ev: SubmitEvent| { let mut failed = false; @@ -299,7 +288,7 @@ impl FormBuilder { }; Form { - fd: self.fd, + fd, validations: self.validations, view, } diff --git a/src/styles/mod.rs b/src/styles/mod.rs index 68fb6cd..0bd85c8 100644 --- a/src/styles/mod.rs +++ b/src/styles/mod.rs @@ -13,7 +13,7 @@ use leptos::{RwSignal, Signal, View}; pub use grid_form::{GridFormStyle, GridFormStylingAttributes}; -pub trait FormStyle: Default + 'static { +pub trait FormStyle: 'static { type StylingAttributes; /// Render any containing components for the form.