forked from yewstack/yew
-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
114 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "component" | ||
version = "0.1.0" | ||
authors = ["Ben Berman <[email protected]>"] | ||
|
||
[dependencies] | ||
yew = { path = "../.." } |
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,41 @@ | ||
#[macro_use] | ||
extern crate yew; | ||
|
||
mod my_component; | ||
|
||
use yew::html::*; | ||
use my_component::MyComponent; | ||
|
||
struct Model { | ||
name: String, | ||
} | ||
|
||
enum Msg { | ||
ChangeName(String), | ||
} | ||
|
||
fn update(_: &mut Context<Msg>, model: &mut Model, msg: Msg) { | ||
match msg { | ||
Msg::ChangeName(new_name) => { | ||
model.name = new_name; | ||
} | ||
} | ||
} | ||
|
||
fn view(model: &Model) -> Html<Msg> { | ||
html! { | ||
<div> | ||
<input oninput=|e: InputData| Msg::ChangeName(e.value), /> | ||
{ model.name.clone() } | ||
<MyComponent initial_value={ 0 }, /> | ||
</div> | ||
} | ||
} | ||
|
||
fn main() { | ||
let model = Model { | ||
name: "River".to_owned(), | ||
}; | ||
|
||
program(model, update, view); | ||
} |
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,57 @@ | ||
use yew::html::*; | ||
use yew::component::*; | ||
use yew::services::console::ConsoleService; | ||
|
||
pub struct MyComponent; | ||
|
||
pub struct MyModel { | ||
pub counter: i32, | ||
} | ||
|
||
// I'm thinking this could be included and used by the macro, but not by the user | ||
// so it could have to always be called "Props" so the macro knows what to use | ||
#[derive(PartialEq, Eq)] | ||
pub struct Props { | ||
// bad example, probably, because it would imply this is controlled while it's not | ||
pub initial_value: i32, | ||
} | ||
|
||
enum MyMsg { | ||
Increment, | ||
Decrement, | ||
Noop, | ||
} | ||
|
||
impl Component<MyModel, Props, MyMsg> for MyComponent { | ||
fn get_initial_model(&self, props: Props) -> MyModel { | ||
MyModel { counter: props.initial_value } | ||
} | ||
|
||
// other lifecycle hooks could definitely exist | ||
|
||
// I guess view would be called after every update, regardless of if it | ||
// updated the model or the view | ||
fn update(&self, context: &mut Context<MyMsg>, model: &mut MyModel, msg: Option<MyMsg>) { | ||
match msg.unwrap_or(MyMsg::Noop) { | ||
MyMsg::Increment => { | ||
model.counter = model.counter + 1; | ||
} | ||
MyMsg::Decrement => { | ||
model.counter = model.counter - 1; | ||
} | ||
MyMsg::Noop => { | ||
context.get_console().info("Maybe a lifecycle hook got called or something"); | ||
} | ||
} | ||
} | ||
|
||
fn view(&self, _: &Props, model: &MyModel) -> Html<MyMsg> { | ||
html! { | ||
<div> | ||
<p>{ "Counter:" } { model.counter }</p> | ||
<button onclick=|_| MyMsg::Increment,> { "Up" } </button> | ||
<button onclick=|_| MyMsg::Decrement,>{ "Down" }</button> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use html::{Context, Html}; | ||
|
||
pub trait Component<Model, Props, Msg> { | ||
fn get_initial_model(&self, props: Props) -> Model; | ||
fn update(&self, context: &mut Context<Msg>, model: &mut Model, msg: Option<Msg>); | ||
fn view(&self, props: &Props, model: &Model) -> Html<Msg>; | ||
} |
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 |
---|---|---|
|
@@ -10,4 +10,4 @@ pub mod macros; | |
pub mod html; | ||
pub mod services; | ||
pub mod virtual_dom; | ||
|
||
pub mod component; |
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