Skip to content

Commit

Permalink
feat: impl IntoAttribute for TextProp (#1925)
Browse files Browse the repository at this point in the history
  • Loading branch information
SleeplessOne1917 authored Oct 27, 2023
1 parent 7770956 commit 4029de2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 64 deletions.
2 changes: 0 additions & 2 deletions leptos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 0 additions & 59 deletions leptos/src/text_prop.rs

This file was deleted.

8 changes: 7 additions & 1 deletion leptos_dom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down Expand Up @@ -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`].
Expand Down
10 changes: 9 additions & 1 deletion leptos_dom/src/macro_helpers/into_attribute.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -270,6 +270,14 @@ impl IntoAttribute for Option<Box<dyn IntoAttribute>> {
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() {
Expand Down
69 changes: 68 additions & 1 deletion leptos_reactive/src/signal_wrappers_read.rs
Original file line number Diff line number Diff line change
@@ -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<T>`].
Expand Down Expand Up @@ -1297,3 +1298,69 @@ impl<T: Clone> Fn<()> for MaybeProp<T> {
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<dyn Fn() -> 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<String> 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<Rc<str>> for TextProp {
fn from(s: Rc<str>) -> Self {
let s: Oco<'_, str> = s.into();
TextProp(Rc::new(move || s.clone()))
}
}

impl From<Oco<'static, str>> for TextProp {
fn from(s: Oco<'static, str>) -> Self {
TextProp(Rc::new(move || s.clone()))
}
}

impl<T> From<T> for MaybeProp<TextProp>
where
T: Into<Oco<'static, str>>,
{
fn from(s: T) -> Self {
Self(Some(MaybeSignal::from(Some(s.into().into()))))
}
}

impl<F, S> From<F> for TextProp
where
F: Fn() -> S + 'static,
S: Into<Oco<'static, str>>,
{
#[inline(always)]
fn from(s: F) -> Self {
TextProp(Rc::new(move || s().into()))
}
}

0 comments on commit 4029de2

Please sign in to comment.