From 69ddf6b02f33eefb6dddacfacc4f7e8ca83ea95c Mon Sep 17 00:00:00 2001 From: Yota <91780796+wiyota@users.noreply.github.com> Date: Fri, 5 Apr 2024 22:43:27 +0900 Subject: [PATCH] feat: Add min/max props into InputNumber (#153) * feat: Add min/max props into InputNumber Adding range into InputNumber component with min/max props * refactor: InputNumber component * docs: Add Min / Max section into InputNumber * docs: Improvement InputNumber - Change min/max insertion position. - Change the crate that is added to T impl for conciseness. - Uniformity of expression: maximum for minimum. --- demo_markdown/docs/input_number/mod.md | 14 +++++++++++- thaw/Cargo.toml | 1 + thaw/src/input_number/mod.rs | 30 +++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/demo_markdown/docs/input_number/mod.md b/demo_markdown/docs/input_number/mod.md index 4c5e0c4..c50a21c 100644 --- a/demo_markdown/docs/input_number/mod.md +++ b/demo_markdown/docs/input_number/mod.md @@ -12,6 +12,16 @@ view! { } ``` +### Min / Max + +```rust demo +let value = create_rw_signal(0); + +view! { + +} +``` + ### Disabled ```rust demo @@ -40,13 +50,15 @@ view! { | value | `Model` | `T::default()` | Set the input value. | | placeholder | `OptionalProp>` | `Default::default()` | Placeholder of input number. | | step | `MaybeSignal` | | The number which the current value is increased or decreased on key or button press. | +| min | `MaybeSignal` | `T::min_value()` | The minimum number that the input value can take. | +| max | `MaybeSignal` | `T::max_value()` | The maximum number that the input value can take. | | disabled | `MaybeSignal` | `false` | Whether the input is disabled. | | invalid | `MaybeSignal` | `false` | Whether the input is invalid. | | attr: | `Vec<(&'static str, Attribute)>` | `Default::default()` | The dom attrs of the input element inside the component. | #### T impl -`T: Add + Sub + Default + Clone + FromStr + ToString + 'static` +`T: Add + Sub + PartialOrd + num::Bounded + Default + Clone + FromStr + ToString + 'static` ### InputNumber Ref diff --git a/thaw/Cargo.toml b/thaw/Cargo.toml index 11e41a6..583f729 100644 --- a/thaw/Cargo.toml +++ b/thaw/Cargo.toml @@ -29,6 +29,7 @@ uuid = { version = "1.7.0", features = ["v4"] } cfg-if = "1.0.0" chrono = "0.4.35" palette = "0.7.5" +num-traits = "0.2.18" [features] csr = ["leptos/csr", "thaw_components/csr", "thaw_utils/csr"] diff --git a/thaw/src/input_number/mod.rs b/thaw/src/input_number/mod.rs index fb4fe73..9cf81c5 100644 --- a/thaw/src/input_number/mod.rs +++ b/thaw/src/input_number/mod.rs @@ -1,5 +1,6 @@ use crate::{Button, ButtonVariant, ComponentRef, Icon, Input, InputRef, InputSuffix}; use leptos::*; +use num_traits::Bounded; use std::ops::{Add, Sub}; use std::str::FromStr; use thaw_utils::{Model, OptionalProp, StoredMaybeSignal}; @@ -14,9 +15,11 @@ pub fn InputNumber( #[prop(optional, into)] class: OptionalProp>, #[prop(optional)] comp_ref: ComponentRef, #[prop(attrs)] attrs: Vec<(&'static str, Attribute)>, + #[prop(default = MaybeSignal::Static(T::min_value()), into)] min: MaybeSignal, + #[prop(default = MaybeSignal::Static(T::max_value()), into)] max: MaybeSignal, ) -> impl IntoView where - T: Add + Sub, + T: Add + Sub + PartialOrd + Bounded, T: Default + Clone + FromStr + ToString + 'static, { let input_value = create_rw_signal(String::default()); @@ -39,6 +42,8 @@ where true }); let step: StoredMaybeSignal<_> = step.into(); + let min: StoredMaybeSignal<_> = min.into(); + let max: StoredMaybeSignal<_> = max.into(); let add = Callback::::new(move |e: ev::MouseEvent| { e.prevent_default(); @@ -54,6 +59,24 @@ where comp_ref.load(InputNumberRef { input_ref }); }); + let set_within_range = Callback::::new(move |_| { + let old_value = value.get_untracked(); + let min = min.get_untracked(); + let max = max.get_untracked(); + if old_value < min { + value.set(min); + } else if old_value > max { + value.set(max); + } + }); + + let minus_disabled = create_memo(move |_| disabled.get() || value.get() <= min.get()); + let plus_disabled = create_memo(move |_| disabled.get() || value.get() >= max.get()); + let invalid = create_memo(move |_| { + let value = value.get(); + invalid.get() || value < min.get() || value > max.get() + }); + view! { - -