Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Cleanup
  • Loading branch information
DanielleHuisman committed Feb 10, 2025
commit 3526d6dc2dab9ef90c8a1510fcc45b2c7cd2fc31
152 changes: 121 additions & 31 deletions packages/leptos-node-ref/src/any_node_ref.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::marker::PhantomData;

use leptos::{
attr::{Attribute, NextAttribute},
html::ElementType,
prelude::{
guards::{Derefable, ReadGuard},
DefinedAt, Get, NodeRef, ReadUntracked, RwSignal, Set, Track,
},
tachys::{
html::node_ref::NodeRefContainer,
renderer::types::Element,
},
tachys::{html::node_ref::NodeRefContainer, renderer::types::Element},
};
use send_wrapper::SendWrapper;

Expand All @@ -18,7 +16,7 @@ use send_wrapper::SendWrapper;
pub struct AnyNodeRef(RwSignal<Option<SendWrapper<Element>>>);

impl AnyNodeRef {
/// Creates a new `AnyNodeRef`.
/// Creates a new [`AnyNodeRef`].
#[track_caller]
pub fn new() -> Self {
Self(RwSignal::new(None))
Expand All @@ -45,6 +43,21 @@ impl DefinedAt for AnyNodeRef {
}
}

impl<T: ElementType> From<NodeRef<T>> for AnyNodeRef
where
NodeRef<T>: IntoAnyNodeRef,
{
fn from(value: NodeRef<T>) -> Self {
value.into_any()
}
}

impl<E: ElementType> NodeRefContainer<E> for AnyNodeRef {
fn load(self, el: &Element) {
self.0.set(Some(SendWrapper::new(el.clone())));
}
}

impl ReadUntracked for AnyNodeRef {
type Value = ReadGuard<Option<Element>, Derefable<Option<Element>>>;

Expand All @@ -61,18 +74,12 @@ impl Track for AnyNodeRef {
}
}

/// Allows converting any node reference into our type-erased `AnyNodeRef`.
/// Allows converting any node reference into our type-erased [`AnyNodeRef`].
pub trait IntoAnyNodeRef {
/// Converts `self` into an `AnyNodeRef`.
/// Converts `self` into an [`AnyNodeRef`].
fn into_any(self) -> AnyNodeRef;
}

impl<E: ElementType> NodeRefContainer<E> for AnyNodeRef {
fn load(self, el: &Element) {
self.0.set(Some(SendWrapper::new(el.clone())));
}
}

impl<E> IntoAnyNodeRef for NodeRef<E>
where
E: ElementType,
Expand All @@ -94,18 +101,9 @@ impl IntoAnyNodeRef for AnyNodeRef {
}
}

impl<T: ElementType> From<NodeRef<T>> for AnyNodeRef
where
NodeRef<T>: IntoAnyNodeRef,
{
fn from(value: NodeRef<T>) -> Self {
value.into_any()
}
}

/// Attribute wrapper for node refs that allows conditional rendering across elements.
/// Attribute wrapper for node references that allows conditional rendering across elements.
///
/// Useful when distributing node refs across multiple rendering branches.
/// Useful when distributing node references across multiple rendering branches.
#[derive(Debug)]
pub struct AnyNodeRefAttr<E, C> {
container: C,
Expand Down Expand Up @@ -187,17 +185,14 @@ where
{
type Output<NewAttr: Attribute> = (Self, NewAttr);

fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
fn add_any_attr<NewAttr: Attribute>(self, new_attr: NewAttr) -> Self::Output<NewAttr> {
(self, new_attr)
}
}

/// Constructs an attribute to attach an `AnyNodeRef` to an element.
/// Constructs an attribute to attach an [`AnyNodeRef`] to an element.
///
/// Enables adding node refs in conditional/dynamic rendering branches.
/// Enables adding node references in conditional/dynamic rendering branches.
pub fn any_node_ref<E, C>(container: C) -> AnyNodeRefAttr<E, C>
where
E: ElementType,
Expand All @@ -211,7 +206,102 @@ where

pub mod prelude {
pub use super::*;
pub use any_node_ref;
pub use AnyNodeRef;
pub use IntoAnyNodeRef;
pub use any_node_ref;
}

#[cfg(test)]
mod tests {
use leptos::{html, prelude::*};

use super::{any_node_ref, prelude::*};

#[test]
fn test_any_node_ref_creation() {
let node_ref = AnyNodeRef::new();
assert!(node_ref.get().is_none(), "New AnyNodeRef should be empty");
}

#[test]
fn test_to_any_node_ref() {
let div_ref: NodeRef<html::Div> = NodeRef::new();
let any_ref = div_ref.into_any();
assert!(
any_ref.get().is_none(),
"Converted AnyNodeRef should be initially empty"
);
}

#[test]
fn test_clone_and_copy() {
let node_ref = AnyNodeRef::new();
let cloned_ref = node_ref;
let _copied_ref = cloned_ref; // Should be copyable
assert!(
cloned_ref.get().is_none(),
"Cloned AnyNodeRef should be empty"
);
}

#[test]
fn test_default() {
let node_ref = AnyNodeRef::default();
assert!(
node_ref.get().is_none(),
"Default AnyNodeRef should be empty"
);
}

#[test]
fn test_into_any_node_ref_trait() {
let div_ref: NodeRef<html::Div> = NodeRef::new();
let _any_ref: AnyNodeRef = div_ref.into_any();

let input_ref: NodeRef<html::Input> = NodeRef::new();
let _any_input_ref: AnyNodeRef = input_ref.into_any();
}

#[test]
fn test_from_node_ref() {
let div_ref: NodeRef<html::Div> = NodeRef::new();
let _any_ref: AnyNodeRef = div_ref.into();
}

#[test]
fn test_any_node_ref_attr() {
let node_ref = AnyNodeRef::new();
let _attr = any_node_ref::<html::Div, _>(node_ref);
}

#[test]
fn test_defined_at() {
let node_ref = AnyNodeRef::new();
assert!(node_ref.defined_at().is_some());
}

#[test]
fn test_track_and_untracked() {
let node_ref = AnyNodeRef::new();
// Just testing that these don't panic
node_ref.track();
let _untracked = node_ref.try_read_untracked();
}

#[test]
fn test_into_any_identity() {
let node_ref = AnyNodeRef::new();
let same_ref = node_ref.into_any();

// Instead of checking pointer equality, we should verify:
// 1. Both refs are initially empty
assert!(node_ref.get().is_none());
assert!(same_ref.get().is_none());

// 2. When we set one, both should reflect the change
// (This would require a mock Element to test properly)

// 3. They should have the same defined_at location
assert_eq!(node_ref.defined_at(), same_ref.defined_at());
}
}
81 changes: 0 additions & 81 deletions packages/leptos-node-ref/tests/any_node_ref.rs

This file was deleted.

Loading