feat: add `new` method to `NodeRef` (#1896)
This commit is contained in:
parent
d6a9d2efdf
commit
72ad1d7c68
|
@ -9,7 +9,6 @@ use std::cell::Cell;
|
|||
///
|
||||
/// ```
|
||||
/// # use leptos::{*, logging::log};
|
||||
///
|
||||
/// use leptos::html::Input;
|
||||
///
|
||||
/// #[component]
|
||||
|
@ -44,7 +43,6 @@ pub struct NodeRef<T: ElementDescriptor + 'static>(
|
|||
///
|
||||
/// ```
|
||||
/// # use leptos::{*, logging::log};
|
||||
///
|
||||
/// use leptos::html::Input;
|
||||
///
|
||||
/// #[component]
|
||||
|
@ -60,12 +58,11 @@ pub struct NodeRef<T: ElementDescriptor + 'static>(
|
|||
/// };
|
||||
///
|
||||
/// view! {
|
||||
/// cx,
|
||||
/// <div>
|
||||
/// // `node_ref` loads the input
|
||||
/// <input _ref=input_ref type="text"/>
|
||||
/// // the button consumes it
|
||||
/// <button on:click=on_click>"Click me"</button>
|
||||
/// // `node_ref` loads the input
|
||||
/// <input _ref=input_ref type="text"/>
|
||||
/// // the button consumes it
|
||||
/// <button on:click=on_click>"Click me"</button>
|
||||
/// </div>
|
||||
/// }
|
||||
/// }
|
||||
|
@ -76,6 +73,44 @@ pub fn create_node_ref<T: ElementDescriptor + 'static>() -> NodeRef<T> {
|
|||
}
|
||||
|
||||
impl<T: ElementDescriptor + 'static> NodeRef<T> {
|
||||
/// Creates a shared reference to a DOM node created while using the `view`
|
||||
/// macro to create your UI.
|
||||
///
|
||||
/// This is identical to [`create_node_ref`].
|
||||
///
|
||||
/// ```
|
||||
/// # use leptos::{*, logging::log};
|
||||
///
|
||||
/// use leptos::html::Input;
|
||||
///
|
||||
/// #[component]
|
||||
/// pub fn MyComponent() -> impl IntoView {
|
||||
/// let input_ref = NodeRef::<Input>::new();
|
||||
///
|
||||
/// let on_click = move |_| {
|
||||
/// let node =
|
||||
/// input_ref.get().expect("input_ref should be loaded by now");
|
||||
/// // `node` is strongly typed
|
||||
/// // it is dereferenced to an `HtmlInputElement` automatically
|
||||
/// log!("value is {:?}", node.value())
|
||||
/// };
|
||||
///
|
||||
/// view! {
|
||||
/// <div>
|
||||
/// // `node_ref` loads the input
|
||||
/// <input _ref=input_ref type="text"/>
|
||||
/// // the button consumes it
|
||||
/// <button on:click=on_click>"Click me"</button>
|
||||
/// </div>
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
#[track_caller]
|
||||
pub fn new() -> Self {
|
||||
create_node_ref()
|
||||
}
|
||||
|
||||
/// Gets the element that is currently stored in the reference.
|
||||
///
|
||||
/// This tracks reactively, so that node references can be used in effects.
|
||||
|
@ -150,6 +185,12 @@ impl<T: ElementDescriptor> Clone for NodeRef<T> {
|
|||
|
||||
impl<T: ElementDescriptor + 'static> Copy for NodeRef<T> {}
|
||||
|
||||
impl<T: ElementDescriptor + 'static> Default for NodeRef<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "nightly")] {
|
||||
impl<T: Clone + ElementDescriptor + 'static> FnOnce<()> for NodeRef<T> {
|
||||
|
|
Loading…
Reference in New Issue