diff --git a/src/components/mod.rs b/src/components/mod.rs index 78235d2..f8e76ac 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -5,6 +5,7 @@ use crate::prelude::*; pub mod header; pub mod sidebar; +pub mod numput; #[component] pub fn Dash () -> impl IntoView { diff --git a/src/components/numput.rs b/src/components/numput.rs new file mode 100644 index 0000000..858bbe5 --- /dev/null +++ b/src/components/numput.rs @@ -0,0 +1,38 @@ +use std::{ fmt::Debug, str::FromStr, ops::{ Sub, Add }}; + +use leptos::{ logging, prelude::*, tachys::html::property::IntoProperty }; +use num_format::{Locale, ToFormattedString}; +use num_traits::{ SaturatingSub, SaturatingAdd }; +use crate::prelude::*; + +#[component] +pub fn Numput ( + field: U, + #[prop(optional)] + class: &'a str, +) -> impl IntoView + where T: ToString + FromStr + Clone + Debug + SaturatingSub + SaturatingAdd + Copy + Sync + ToFormattedString, + U: Get + Set + GetUntracked + With + IntoProperty + Copy + Clone + Send + Sync + 'static, +{ + let change = move |ev: Targeted| { + let old = field.get_untracked(); + let new = ev.target().value().replace(",", ""); + + + if let Some(t) = new.strip_prefix("+") && let Ok(v) = t.parse::() { + field.set(old.saturating_add(&v)) + } else if let Some(t) = new.strip_prefix("-") && let Ok(v) = t.parse::() { + field.set(old.saturating_sub(&v)) + } else if let Ok(v) = new.parse::() { + field.set(v); + } else { + ev.target().set_value(&old.to_string()); + } + }; + + let class = format!("{} numput", class); + + view! { + + } +}