Skip to content

Commit

Permalink
Just playin
Browse files Browse the repository at this point in the history
  • Loading branch information
rivertam committed Dec 30, 2017
1 parent 248b797 commit 82db16d
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 4 deletions.
7 changes: 7 additions & 0 deletions examples/component/Cargo.toml
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 = "../.." }
41 changes: 41 additions & 0 deletions examples/component/src/main.rs
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);
}
57 changes: 57 additions & 0 deletions examples/component/src/my_component.rs
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>
}
}
}
7 changes: 7 additions & 0 deletions src/component.rs
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>;
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub mod macros;
pub mod html;
pub mod services;
pub mod virtual_dom;

pub mod component;
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use virtual_dom::{VTag, VNode, Listener};

#[macro_export]
macro_rules! html_impl {
// Start of openging tag
// Start of opening tag
($stack:ident (< $starttag:ident $($tail:tt)*)) => {
let node = $crate::virtual_dom::VTag::new(stringify!($starttag));
$stack.push(node);
Expand Down
2 changes: 0 additions & 2 deletions src/virtual_dom/vtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ impl<MSG> VTag<MSG> {
(&None, None) => None,
}
}
}

impl<MSG> VTag<MSG> {
pub fn render(&mut self, subject: &Element, mut opposite: Option<Self>, messages: Messages<MSG>) {
// TODO Replace self if tagName differs

Expand Down

0 comments on commit 82db16d

Please sign in to comment.