From 4029de2d42f3b15368111c83e28fd4817c99c9d2 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Fri, 27 Oct 2023 21:10:09 +0000 Subject: [PATCH] feat: impl `IntoAttribute` for `TextProp` (#1925) --- leptos/src/lib.rs | 2 - leptos/src/text_prop.rs | 59 ---------------- leptos_dom/src/lib.rs | 8 ++- .../src/macro_helpers/into_attribute.rs | 10 ++- leptos_reactive/src/signal_wrappers_read.rs | 69 ++++++++++++++++++- 5 files changed, 84 insertions(+), 64 deletions(-) delete mode 100644 leptos/src/text_prop.rs diff --git a/leptos/src/lib.rs b/leptos/src/lib.rs index ce4f5436ef..e15b7142c7 100644 --- a/leptos/src/lib.rs +++ b/leptos/src/lib.rs @@ -202,10 +202,8 @@ pub use serde_json; pub use show::*; pub use suspense_component::*; mod suspense_component; -mod text_prop; mod transition; -pub use text_prop::TextProp; #[cfg(any(debug_assertions, feature = "ssr"))] #[doc(hidden)] pub use tracing; diff --git a/leptos/src/text_prop.rs b/leptos/src/text_prop.rs deleted file mode 100644 index d71531dc1e..0000000000 --- a/leptos/src/text_prop.rs +++ /dev/null @@ -1,59 +0,0 @@ -use leptos_reactive::Oco; -use std::{fmt::Debug, rc::Rc}; - -/// Describes a value that is either a static or a reactive string, i.e., -/// a [`String`], a [`&str`], or a reactive `Fn() -> String`. -#[derive(Clone)] -pub struct TextProp(Rc Oco<'static, str>>); - -impl TextProp { - /// Accesses the current value of the property. - #[inline(always)] - pub fn get(&self) -> Oco<'static, str> { - (self.0)() - } -} - -impl Debug for TextProp { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("TextProp").finish() - } -} - -impl From for TextProp { - fn from(s: String) -> Self { - let s: Oco<'_, str> = Oco::Counted(Rc::from(s)); - TextProp(Rc::new(move || s.clone())) - } -} - -impl From<&'static str> for TextProp { - fn from(s: &'static str) -> Self { - let s: Oco<'_, str> = s.into(); - TextProp(Rc::new(move || s.clone())) - } -} - -impl From> for TextProp { - fn from(s: Rc) -> Self { - let s: Oco<'_, str> = s.into(); - TextProp(Rc::new(move || s.clone())) - } -} - -impl From> for TextProp { - fn from(s: Oco<'static, str>) -> Self { - TextProp(Rc::new(move || s.clone())) - } -} - -impl From for TextProp -where - F: Fn() -> S + 'static, - S: Into>, -{ - #[inline(always)] - fn from(s: F) -> Self { - TextProp(Rc::new(move || s().into())) - } -} diff --git a/leptos_dom/src/lib.rs b/leptos_dom/src/lib.rs index 1053126097..4d26905fe1 100644 --- a/leptos_dom/src/lib.rs +++ b/leptos_dom/src/lib.rs @@ -38,11 +38,11 @@ pub use events::{typed as ev, typed::EventHandler}; pub use html::HtmlElement; use html::{AnyElement, ElementDescriptor}; pub use hydration::{HydrationCtx, HydrationKey}; -use leptos_reactive::Oco; #[cfg(not(feature = "nightly"))] use leptos_reactive::{ MaybeProp, MaybeSignal, Memo, ReadSignal, RwSignal, Signal, SignalGet, }; +use leptos_reactive::{Oco, TextProp}; pub use macro_helpers::*; pub use node_ref::*; #[cfg(all(target_arch = "wasm32", feature = "web"))] @@ -229,6 +229,12 @@ where } } +impl IntoView for TextProp { + fn into_view(self) -> View { + self.get().into_view() + } +} + /// Collects an iterator or collection into a [`View`]. pub trait CollectView { /// Collects an iterator or collection into a [`View`]. diff --git a/leptos_dom/src/macro_helpers/into_attribute.rs b/leptos_dom/src/macro_helpers/into_attribute.rs index 3cbd424854..dcae5cd3f1 100644 --- a/leptos_dom/src/macro_helpers/into_attribute.rs +++ b/leptos_dom/src/macro_helpers/into_attribute.rs @@ -1,8 +1,8 @@ -use leptos_reactive::Oco; #[cfg(not(feature = "nightly"))] use leptos_reactive::{ MaybeProp, MaybeSignal, Memo, ReadSignal, RwSignal, Signal, SignalGet, }; +use leptos_reactive::{Oco, TextProp}; use std::{borrow::Cow, rc::Rc}; #[cfg(all(target_arch = "wasm32", feature = "web"))] use wasm_bindgen::UnwrapThrowExt; @@ -270,6 +270,14 @@ impl IntoAttribute for Option> { impl_into_attr_boxed! {} } +impl IntoAttribute for TextProp { + fn into_attribute(self) -> Attribute { + self.get().into_attribute() + } + + impl_into_attr_boxed! {} +} + impl IntoAttribute for std::fmt::Arguments<'_> { fn into_attribute(self) -> Attribute { match self.as_str() { diff --git a/leptos_reactive/src/signal_wrappers_read.rs b/leptos_reactive/src/signal_wrappers_read.rs index 477062e9a4..050e32be24 100644 --- a/leptos_reactive/src/signal_wrappers_read.rs +++ b/leptos_reactive/src/signal_wrappers_read.rs @@ -1,8 +1,9 @@ use crate::{ create_isomorphic_effect, on_cleanup, runtime::untrack, store_value, Memo, - ReadSignal, RwSignal, SignalDispose, SignalGet, SignalGetUntracked, + Oco, ReadSignal, RwSignal, SignalDispose, SignalGet, SignalGetUntracked, SignalStream, SignalWith, SignalWithUntracked, StoredValue, }; +use std::{fmt::Debug, rc::Rc}; /// Helper trait for converting `Fn() -> T` closures into /// [`Signal`]. @@ -1297,3 +1298,69 @@ impl Fn<()> for MaybeProp { self.get() } } + +/// Describes a value that is either a static or a reactive string, i.e., +/// a [`String`], a [`&str`], or a reactive `Fn() -> String`. +#[derive(Clone)] +pub struct TextProp(Rc Oco<'static, str>>); + +impl TextProp { + /// Accesses the current value of the property. + #[inline(always)] + pub fn get(&self) -> Oco<'static, str> { + (self.0)() + } +} + +impl Debug for TextProp { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("TextProp").finish() + } +} + +impl From for TextProp { + fn from(s: String) -> Self { + let s: Oco<'_, str> = Oco::Counted(Rc::from(s)); + TextProp(Rc::new(move || s.clone())) + } +} + +impl From<&'static str> for TextProp { + fn from(s: &'static str) -> Self { + let s: Oco<'_, str> = s.into(); + TextProp(Rc::new(move || s.clone())) + } +} + +impl From> for TextProp { + fn from(s: Rc) -> Self { + let s: Oco<'_, str> = s.into(); + TextProp(Rc::new(move || s.clone())) + } +} + +impl From> for TextProp { + fn from(s: Oco<'static, str>) -> Self { + TextProp(Rc::new(move || s.clone())) + } +} + +impl From for MaybeProp +where + T: Into>, +{ + fn from(s: T) -> Self { + Self(Some(MaybeSignal::from(Some(s.into().into())))) + } +} + +impl From for TextProp +where + F: Fn() -> S + 'static, + S: Into>, +{ + #[inline(always)] + fn from(s: F) -> Self { + TextProp(Rc::new(move || s().into())) + } +}