From 1fc165f2a5a3b6d77da2cfea2aa05e1db1c73577 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sun, 5 Oct 2025 18:27:05 +0200 Subject: feat(form): Re-write the form macro as a proc macro This allows more possibilities. --- src/components/buy.rs | 23 ++++--- src/components/form.rs | 120 +---------------------------------- src/components/input_placeholder.rs | 7 +- src/components/mod.rs | 11 +++- src/components/select_placeholder.rs | 93 +++++++++++++++++++++++++++ 5 files changed, 120 insertions(+), 134 deletions(-) create mode 100644 src/components/select_placeholder.rs (limited to 'src') diff --git a/src/components/buy.rs b/src/components/buy.rs index 0c294ee..6d9402e 100644 --- a/src/components/buy.rs +++ b/src/components/buy.rs @@ -1,7 +1,9 @@ use leptos::{IntoView, component, view}; use log::info; +use rocie_client::models::UnitId; +use uuid::Uuid; -use crate::components::{form::Form, input_placeholder::InputPlaceholder, site_header::SiteHeader}; +use crate::components::{form::Form, site_header::SiteHeader}; #[component] pub fn Buy() -> impl IntoView { @@ -9,22 +11,27 @@ pub fn Buy() -> impl IntoView { {Form! { - on_submit = |Inputs {product_barcode, amount}| { - info!("Got product barcode: {product_barcode} with amount: {amount}"); - } + on_submit = |product_barcode, amount, unit_id| { + info!("Got product barcode: {product_barcode} with amount: {amount}, {unit_id}"); + }; + - )* - ) => {{ - 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; +pub(crate) use rocie_macros::Form; diff --git a/src/components/input_placeholder.rs b/src/components/input_placeholder.rs index 05b9509..aeef838 100644 --- a/src/components/input_placeholder.rs +++ b/src/components/input_placeholder.rs @@ -1,5 +1,3 @@ -use std::sync::atomic::{AtomicU32, Ordering}; - use leptos::{ IntoView, component, html::Input, @@ -7,11 +5,8 @@ use leptos::{ view, }; -fn get_id() -> u32 { - static ID: AtomicU32 = AtomicU32::new(0); +use crate::components::get_id; - ID.fetch_add(1, Ordering::Relaxed) -} #[component] pub fn InputPlaceholder( diff --git a/src/components/mod.rs b/src/components/mod.rs index f7b8dba..1ee37d5 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -1,9 +1,12 @@ +use std::sync::atomic::{AtomicU32, Ordering}; + // Generic pub mod async_fetch; pub mod container; +pub mod form; pub mod icon_p; pub mod input_placeholder; -pub mod form; +pub mod select_placeholder; // Specific pub mod buy; @@ -11,3 +14,9 @@ pub mod inventory; pub mod product_overview; pub mod recipies; pub mod site_header; + +fn get_id() -> u32 { + static ID: AtomicU32 = AtomicU32::new(0); + + ID.fetch_add(1, Ordering::Relaxed) +} diff --git a/src/components/select_placeholder.rs b/src/components/select_placeholder.rs new file mode 100644 index 0000000..947931c --- /dev/null +++ b/src/components/select_placeholder.rs @@ -0,0 +1,93 @@ +use leptos::{ + IntoView, + attr::{AttributeValue, IntoAttributeValue}, + component, + html::Select, + prelude::{ + ClassAttribute, CollectView, ElementChild, GlobalAttributes, NodeRef, NodeRefAttribute, + }, + view, +}; + +use crate::components::get_id; + +#[component] +pub fn SelectPlaceholder( + label: &'static str, + node_ref: NodeRef + {options} + + + // TODO: Reference `var(--tw-border-2)` instead of the `2 px` <2025-10-01> +
+ + +
+ } +} -- cgit 1.4.1