diff --git a/src/form.rs b/src/form.rs index 5e6557a..2c1ae44 100644 --- a/src/form.rs +++ b/src/form.rs @@ -96,19 +96,35 @@ pub trait FormToolData: Clone + 'static { /// Constructs a [`Form`] for this [`FormToolData`] type. /// - /// This renders the form as a the leptos_router - /// [`Form`](leptos_router::Form) - /// component. Call [`get_action_form`]\() to get the - /// [`ActionForm`](leptos_router::ActionForm) version. - fn get_form( + /// This renders the form as a enhanced + /// [`ActionForm`](leptos_router::ActionForm) that sends the form data + /// directly by calling the server function. + /// + /// By doing this, we avoid doing the [`FromFormData`](leptos_router::FromFormData) + /// conversion. However, to support + /// [Progressive Enhancement](https://book.leptos.dev/progressive_enhancement/index.html), + /// you should name the form elements to work with a plain ActionForm anyway. + /// If progresssive enhancement is not important to you, you may freely + /// use this version + /// + /// For the other ways to construct a [`Form`], see: + /// - [`get_action_form`](Self::get_action_form) + /// - [`get_plain_form`](Self::get_plain_form) + fn get_form( self, - action: impl ToString, + action: Action>>, style: Self::Style, context: Self::Context, - ) -> Form { + ) -> Form + where + ServFn: DeserializeOwned + ServerFn + 'static, + <>::Request as ClientReq>::FormData: + From, + ServFn: From, + { let builder = FormBuilder::new(context); let builder = Self::build_form(builder); - builder.build_plain_form(action.to_string(), self, style) + builder.build_form(action, self, style) } /// Constructs a [`Form`] for this [`FormToolData`] type. @@ -117,6 +133,10 @@ pub trait FormToolData: Clone + 'static { /// [`ActionForm`](leptos_router::ActionForm) /// component. Call [`get_form`]\() to get the plain /// [`Form`](leptos_router::Form) version. + /// + /// For the other ways to construct a [`Form`], see: + /// - [`get_form`](Self::get_form) + /// - [`get_action_form`](Self::get_action_form) fn get_action_form( self, action: Action>>, @@ -133,6 +153,27 @@ pub trait FormToolData: Clone + 'static { builder.build_action_form(action, self, style) } + /// Constructs a [`Form`] for this [`FormToolData`] type. + /// + /// This renders the form as a the leptos_router + /// [`Form`](leptos_router::Form) + /// component. Call [`get_action_form`]\() to get the + /// [`ActionForm`](leptos_router::ActionForm) version. + /// + /// For the other ways to construct a [`Form`], see: + /// - [`get_form`](Self::get_form) + /// - [`get_action_form`](Self::get_action_form) + fn get_plain_form( + self, + action: impl ToString, + style: Self::Style, + context: Self::Context, + ) -> Form { + let builder = FormBuilder::new(context); + let builder = Self::build_form(builder); + builder.build_plain_form(action.to_string(), self, style) + } + /// Gets a [`Validator`] for this [`ToolFormData`]. /// /// This doesn't render the view, but just collects all the validation diff --git a/src/form_builder.rs b/src/form_builder.rs index a22934b..4784dd0 100644 --- a/src/form_builder.rs +++ b/src/form_builder.rs @@ -301,9 +301,6 @@ impl FormBuilder { Rc::new(value_setter) } - // TODO: impl build_formless function that builds the form without adding - // and form components around anything - pub(crate) fn build_form( self, action: Action>>, @@ -314,6 +311,7 @@ impl FormBuilder { ServFn: DeserializeOwned + ServerFn + 'static, <>::Request as ClientReq>::FormData: From, + ServFn: From, { let fd = create_rw_signal(fd); let fs = Rc::new(fs); @@ -338,10 +336,14 @@ impl FormBuilder { } if failed { ev.prevent_default(); + return; } + + ev.prevent_default(); + let server_fn = ServFn::from(fd.get_untracked()); + action.dispatch(server_fn); }; - // TODO: do enhanced form with direct calling of the server_fn let view = view! { {elements}