-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suspense example with struct component (#3375)
* Add example for struct component context consumer * Replace untested docs example with one from examples dir Fixes #3351
- Loading branch information
Showing
4 changed files
with
102 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use web_sys::HtmlTextAreaElement; | ||
use yew::prelude::*; | ||
|
||
use crate::use_sleep; | ||
|
||
#[function_component] | ||
pub fn WithSleep<Comp>() -> HtmlResult | ||
where | ||
Comp: BaseComponent<Properties = AppContentProps>, | ||
{ | ||
let sleep = use_sleep()?; | ||
let sleep = Callback::from(move |_| sleep()); | ||
Ok(yew::virtual_dom::VChild::<Comp>::new(AppContentProps { resleep: sleep }, None).into()) | ||
} | ||
|
||
#[derive(Debug, PartialEq, Properties)] | ||
pub struct AppContentProps { | ||
pub resleep: Callback<()>, | ||
} | ||
|
||
pub type AppContent = WithSleep<BaseAppContent>; | ||
|
||
pub enum Msg { | ||
ValueUpdate(String), | ||
TakeABreak, | ||
} | ||
|
||
pub struct BaseAppContent { | ||
value: String, | ||
} | ||
|
||
impl Component for BaseAppContent { | ||
type Message = Msg; | ||
type Properties = AppContentProps; | ||
|
||
fn create(_ctx: &Context<Self>) -> Self { | ||
Self { | ||
value: "I am writing a long story...".to_string(), | ||
} | ||
} | ||
|
||
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool { | ||
match msg { | ||
Msg::ValueUpdate(v) => { | ||
self.value = v; | ||
} | ||
Msg::TakeABreak => { | ||
ctx.props().resleep.emit(()); | ||
} | ||
}; | ||
true | ||
} | ||
|
||
fn view(&self, ctx: &Context<Self>) -> Html { | ||
let oninput = ctx.link().callback(|e: InputEvent| { | ||
let input: HtmlTextAreaElement = e.target_unchecked_into::<HtmlTextAreaElement>(); | ||
Msg::ValueUpdate(input.value()) | ||
}); | ||
let on_take_a_break = ctx.link().callback(|_| Msg::TakeABreak); | ||
html! { | ||
<div class="content-area"> | ||
<textarea value={self.value.clone()} {oninput}></textarea> | ||
<div class="action-area"> | ||
<button onclick={on_take_a_break}>{"Take a break!"}</button> | ||
<div class="hint">{"You can take a break at anytime"}<br />{"and your work will be preserved."}</div> | ||
</div> | ||
</div> | ||
} | ||
} | ||
} |
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