From dc92f18b65ac1e1e4964f10b8455a04dcaaeb644 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Mon, 19 Dec 2022 17:32:15 +0100 Subject: [PATCH] Allow ratio or percentage in (#162) --- src/progress_bar.rs | 50 +++++++++++++++++++----- yewprint-doc/src/lib.rs | 7 ++-- yewprint-doc/src/progress_bar/example.rs | 2 +- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/progress_bar.rs b/src/progress_bar.rs index 766808e..645af0d 100644 --- a/src/progress_bar.rs +++ b/src/progress_bar.rs @@ -1,20 +1,54 @@ use crate::Intent; +use yew::html::IntoPropValue; use yew::prelude::*; -#[derive(Clone, PartialEq, Properties)] +#[derive(Debug, Clone, PartialEq, Properties)] pub struct ProgressBarProps { #[prop_or_default] pub animate: bool, #[prop_or_default] pub stripes: bool, - #[prop_or_default] - pub value: Option, + #[prop_or(RatioOrPercentage(1.0))] + pub value: RatioOrPercentage, #[prop_or_default] pub intent: Option, #[prop_or_default] pub class: Classes, } +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct RatioOrPercentage(f64); + +impl IntoPropValue for f64 { + fn into_prop_value(self) -> RatioOrPercentage { + RatioOrPercentage(self) + } +} + +impl IntoPropValue for f32 { + fn into_prop_value(self) -> RatioOrPercentage { + RatioOrPercentage(self.into()) + } +} + +macro_rules! generate_into_prop_value_integers { + ($($ty:ty,)*) => { + $( + impl IntoPropValue for $ty { + fn into_prop_value(self) -> RatioOrPercentage { + RatioOrPercentage(self as f64 / 100.0) + } + } + )* + }; +} + +#[rustfmt::skip] +generate_into_prop_value_integers!( + u8, u16, u32, + i8, i16, i32, +); + #[function_component(ProgressBar)] pub fn progress_bar( ProgressBarProps { @@ -25,12 +59,8 @@ pub fn progress_bar( class, }: &ProgressBarProps, ) -> Html { - let style = if let Some(value) = value { - let percent = value * 100.0; - AttrValue::from(format!("width: {}%;", percent)) - } else { - "".into() - }; + let percent = (value.0 * 100.0).clamp(0.0, 100.0); + let style = format!("width: {}%", percent); html! {
-
+
} } diff --git a/yewprint-doc/src/lib.rs b/yewprint-doc/src/lib.rs index b537e51..a941922 100644 --- a/yewprint-doc/src/lib.rs +++ b/yewprint-doc/src/lib.rs @@ -86,9 +86,10 @@ macro_rules! build_source_code_component { .to_str() .unwrap(); - if let (Some(actor), Some(branch)) = - (option_env!("GITHUB_ACTOR"), option_env!("GITHUB_HEAD_REF")) - { + if let (Some(actor), Some(branch)) = ( + option_env!("GITHUB_ACTOR").filter(|x| !x.is_empty()), + option_env!("GITHUB_HEAD_REF").filter(|x| !x.is_empty()), + ) { format!("https://github.com/{actor}/yewprint/blob/{branch}/src/{component}.rs") } else { format!("https://github.com/yewprint/yewprint/blob/HEAD/src/{component}.rs") diff --git a/yewprint-doc/src/progress_bar/example.rs b/yewprint-doc/src/progress_bar/example.rs index b022a15..636bf83 100644 --- a/yewprint-doc/src/progress_bar/example.rs +++ b/yewprint-doc/src/progress_bar/example.rs @@ -15,7 +15,7 @@ pub fn example(props: &ExampleProps) -> Html { animate={props.animate} stripes={props.stripes} intent={props.intent} - value=0.3 + value=30_u8 /> } }