From de6559e5e4ddf303fd7eeb0adbfa85a8658e72e8 Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Fri, 6 Dec 2024 16:08:21 +0300 Subject: [PATCH] fix: resolve compilation issues in `ItemForm` component - Updated signal declarations to use tuple destructuring for clarity and consistency. - Corrected event type in `handle_submit` to `SubmitEvent` and added `prevent_default` to prevent page reload on form submission. - Adjusted the `tags` signal to use `Vec` for improved type safety. - Fixed signal updates post-submission to ensure values are reset properly. - Enhanced readability by explicitly referencing signal getters in `view!`. This ensures the `ItemForm` component compiles and functions as intended. --- src/components/item_form.rs | 38 +++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/components/item_form.rs b/src/components/item_form.rs index 3e94952..c82a1d7 100644 --- a/src/components/item_form.rs +++ b/src/components/item_form.rs @@ -1,23 +1,37 @@ use leptos::*; +use leptos_dom::ev::SubmitEvent; // Import the correct event type #[component] -pub fn ItemForm(on_submit: Box)>) -> impl IntoView { - let name = create_signal(String::new()); - let description = create_signal(String::new()); - let tags = create_signal(vec![]); +pub fn ItemForm(on_submit: Box)>) -> impl IntoView { + let (name, set_name) = create_signal(String::new()); + let (description, set_description) = create_signal(String::new()); + let (tags, set_tags) = create_signal(Vec::::new()); - let handle_submit = move |_| { - on_submit(name.get().clone(), description.get().clone(), tags.get().clone()); - name.set(String::new()); - description.set(String::new()); - tags.set(vec![]); + // Use SubmitEvent for the form submission handler + let handle_submit = move |ev: SubmitEvent| { + ev.prevent_default(); // Prevent form submission from reloading the page + on_submit(name.get(), description.get(), tags.get().clone()); + + // Reset values after submission + set_name.update(|n| *n = String::new()); + set_description.update(|d| *d = String::new()); + set_tags.update(|t| t.clear()); }; view! {
- -