-
-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
55 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use leptos_dom::{IntoView, View}; | ||
use std::rc::Rc; | ||
|
||
/// New-type wrapper for the a function that returns a view with `From` and `Default` traits implemented | ||
/// to enable optional props in for example `<Show>` and `<Suspense>`. | ||
#[derive(Clone)] | ||
pub struct ViewFn(Rc<dyn Fn() -> View>); | ||
|
||
impl Default for ViewFn { | ||
fn default() -> Self { | ||
Self(Rc::new(|| ().into_view())) | ||
} | ||
} | ||
|
||
impl<F, IV> From<F> for ViewFn | ||
where | ||
F: Fn() -> IV + 'static, | ||
IV: IntoView, | ||
{ | ||
fn from(value: F) -> Self { | ||
Self(Rc::new(move || value().into_view())) | ||
} | ||
} | ||
|
||
impl ViewFn { | ||
/// Execute the wrapped function | ||
pub fn run(&self) -> View { | ||
(self.0)() | ||
} | ||
} |