From 0564611c8e77e0f5791a3f4854bb456a8717e86a Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sun, 5 Oct 2025 13:21:31 +0200 Subject: feat(form): Provide basic form framework --- src/components/buy.rs | 56 ++++++++--------- src/components/form.rs | 119 ++++++++++++++++++++++++++++++++++++ src/components/input_placeholder.rs | 11 +++- src/components/mod.rs | 1 + 4 files changed, 155 insertions(+), 32 deletions(-) create mode 100644 src/components/form.rs (limited to 'src') diff --git a/src/components/buy.rs b/src/components/buy.rs index 86e9952..0c294ee 100644 --- a/src/components/buy.rs +++ b/src/components/buy.rs @@ -1,38 +1,34 @@ -use leptos::{ - IntoView, component, - html::Input, - prelude::{ClassAttribute, ElementChild, Get, NodeRef, OnAttribute, Set, signal}, - view, - web_sys::SubmitEvent, -}; +use leptos::{IntoView, component, view}; +use log::info; -use crate::components::{input_placeholder::InputPlaceholder, site_header::SiteHeader}; +use crate::components::{form::Form, input_placeholder::InputPlaceholder, site_header::SiteHeader}; #[component] pub fn Buy() -> impl IntoView { - let (product_barcode, set_product_barcode) = signal(String::new()); - - let input_element: NodeRef = NodeRef::new(); - - let on_submit = move |ev: SubmitEvent| { - // stop the page from reloading! - ev.prevent_default(); - - let value = input_element - .get() - // event handlers can only fire after the view - // is mounted to the DOM, so the `NodeRef` will be `Some` - .expect(" to exist") - .value(); - set_product_barcode.set(value); - }; - view! { -
- - - -

"Name is: " {product_barcode}

+ + {Form! { + on_submit = |Inputs {product_barcode, amount}| { + info!("Got product barcode: {product_barcode} with amount: {amount}"); + } + + + + }} } } diff --git a/src/components/form.rs b/src/components/form.rs new file mode 100644 index 0000000..fd55897 --- /dev/null +++ b/src/components/form.rs @@ -0,0 +1,119 @@ +macro_rules! Form { + ( + on_submit = |$bound:pat_param| $on_submit:block + $( + + )* + ) => {{ + use leptos::{ + view, + prelude::{ + Get, + NodeRef, + ElementChild, + ClassAttribute, + OnAttribute, + signal, + Set, + Show, + }, + html::Input, + web_sys::SubmitEvent + }; + + use log::info; + + + $( + let ($signal_name_get, $signal_name_set) = signal(None); + let $name: NodeRef = NodeRef::new(); + )* + + let on_submit = move |ev: SubmitEvent| { + struct Inputs { + $( + $name: $rust_type + ),* + } + + // stop the page from reloading! + ev.prevent_default(); + + $( + let value = { + let output = $name + .get() + // event handlers can only fire after the view + // is mounted to the DOM, so the `NodeRef` will be `Some` + .expect(" to exist") + .value(); + + let fin: Result<$rust_type, leptos::error::Error> = output + .parse() + .map_err(Into::::into); + fin + }; + + let $name = match value { + Ok(ok) => { + // Reset the signal + $signal_name_set.set(None); + + ok + } , + Err(err) => { + $signal_name_set.set(Some(err)); + + // Skip running the real `on_submit` + return + } + }; + )* + + let real_on_submit = |$bound| $on_submit; + real_on_submit(Inputs { + $( + $name + ),* + }) + }; + + + view! { +
+ $( + + +

{move || + format!( + "Input is invalid for type {}: {}", + stringify!($rust_type), + $signal_name_get.get().expect("Was `is_some`") + ) + }

+
+ )* + +
+ +
+ + } + }}; +} + +pub(crate) use Form; diff --git a/src/components/input_placeholder.rs b/src/components/input_placeholder.rs index 92f9926..05b9509 100644 --- a/src/components/input_placeholder.rs +++ b/src/components/input_placeholder.rs @@ -1,10 +1,17 @@ +use std::sync::atomic::{AtomicU32, Ordering}; + use leptos::{ IntoView, component, html::Input, prelude::{ClassAttribute, ElementChild, GlobalAttributes, NodeRef, NodeRefAttribute}, view, }; -use uuid::Uuid; + +fn get_id() -> u32 { + static ID: AtomicU32 = AtomicU32::new(0); + + ID.fetch_add(1, Ordering::Relaxed) +} #[component] pub fn InputPlaceholder( @@ -13,7 +20,7 @@ pub fn InputPlaceholder( node_ref: NodeRef, #[prop(default = None)] initial_value: Option, ) -> impl IntoView { - let id = Uuid::new_v4(); + let id = get_id(); view! {
diff --git a/src/components/mod.rs b/src/components/mod.rs index 55e4397..f7b8dba 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -3,6 +3,7 @@ pub mod async_fetch; pub mod container; pub mod icon_p; pub mod input_placeholder; +pub mod form; // Specific pub mod buy; -- cgit 1.4.1