Skip to content

Commit

Permalink
Add render method to Component and auto implement Renderable
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Sep 23, 2019
1 parent 1375714 commit b6bd146
Show file tree
Hide file tree
Showing 47 changed files with 178 additions and 249 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Yew implements strict application state management based on message passing and
`src/main.rs`

```rust
use yew::{html, Component, ComponentLink, Html, Renderable, ShouldRender};
use yew::{html, Component, ComponentLink, Html, ShouldRender};

struct Model { }

Expand All @@ -82,10 +82,8 @@ impl Component for Model {
}
}
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
// Render your model here
<button onclick=|_| Msg::DoIt>{ "Click me!" }</button>
Expand Down
4 changes: 1 addition & 3 deletions crates/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
//! # fn update(&mut self, msg: Self::Message) -> ShouldRender {
//! # unimplemented!()
//! # }
//! # }
//! #
//! # impl Renderable<Component> for Component {
//! # fn view(&self) -> Html<Self> {
//! # fn render(&self) -> Html<Self> {
//! #
//! // ...
//!
Expand Down
6 changes: 2 additions & 4 deletions examples/counter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use stdweb::web::Date;
use yew::services::ConsoleService;
use yew::{html, Component, ComponentLink, Html, Renderable, ShouldRender};
use yew::{html, Component, ComponentLink, Html, ShouldRender};

pub struct Model {
console: ConsoleService,
Expand Down Expand Up @@ -45,10 +45,8 @@ impl Component for Model {
}
true
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<div>
<nav class="menu">
Expand Down
4 changes: 1 addition & 3 deletions examples/crm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,8 @@ impl Component for Model {
}
true
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
match self.scene {
Scene::ClientsList => html! {
<div class="crm">
Expand Down
4 changes: 1 addition & 3 deletions examples/custom_components/src/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ impl Component for Barrier {
self.onsignal = props.onsignal;
true
}
}

impl Renderable<Barrier> for Barrier {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<div class="barrier">
<p>{ format!("{} on {} clicked", self.counter, self.limit) }</p>
Expand Down
4 changes: 1 addition & 3 deletions examples/custom_components/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ impl Component for Button {
self.onsignal = props.onsignal;
true
}
}

impl Renderable<Button> for Button {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<button onclick=|_| Msg::Clicked>{ &self.title }</button>
}
Expand Down
4 changes: 1 addition & 3 deletions examples/custom_components/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ impl Component for Counter {
self.onclick = props.onclick;
true
}
}

impl Renderable<Counter> for Counter {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
let colorize = {
match self.color {
Color::Red => "background: red;",
Expand Down
4 changes: 1 addition & 3 deletions examples/custom_components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ impl Component for Model {
Msg::ChildClicked(_value) => false,
}
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
let counter = |x| {
html! {
<Counter initial=x color=&self.color onclick=Msg::ChildClicked />
Expand Down
6 changes: 2 additions & 4 deletions examples/dashboard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use yew::format::{Json, Nothing, Toml};
use yew::services::fetch::{FetchService, FetchTask, Request, Response};
use yew::services::websocket::{WebSocketService, WebSocketStatus, WebSocketTask};
use yew::services::Task;
use yew::{html, Component, ComponentLink, Html, Renderable, ShouldRender};
use yew::{html, Component, ComponentLink, Html, ShouldRender};

type AsBinary = bool;

Expand Down Expand Up @@ -167,10 +167,8 @@ impl Component for Model {
}
true
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<div>
<nav class="menu">
Expand Down
6 changes: 2 additions & 4 deletions examples/file_upload/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![recursion_limit = "256"]

use yew::services::reader::{File, FileChunk, FileData, ReaderService, ReaderTask};
use yew::{html, ChangeData, Component, ComponentLink, Html, Renderable, ShouldRender};
use yew::{html, ChangeData, Component, ComponentLink, Html, ShouldRender};

pub struct Model {
link: ComponentLink<Model>,
Expand Down Expand Up @@ -64,10 +64,8 @@ impl Component for Model {
}
true
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
let flag = self.by_chunks;
html! {
<div>
Expand Down
6 changes: 2 additions & 4 deletions examples/fragments/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![recursion_limit = "128"]

use yew::{html, Component, ComponentLink, Html, Renderable, ShouldRender};
use yew::{html, Component, ComponentLink, Html, ShouldRender};

pub struct Model {
counter: usize,
Expand Down Expand Up @@ -32,10 +32,8 @@ impl Component for Model {
}
true
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<>
<nav class="menu">{ self.view_menu() }</nav>
Expand Down
6 changes: 2 additions & 4 deletions examples/game_of_life/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use log::info;
use rand::Rng;
use std::time::Duration;
use yew::services::{IntervalService, Task};
use yew::{html, Component, ComponentLink, Html, Renderable, ShouldRender};
use yew::{html, Component, ComponentLink, Html, ShouldRender};

#[derive(Clone, Copy, PartialEq)]
enum LifeState {
Expand Down Expand Up @@ -205,10 +205,8 @@ impl Component for Model {
}
true
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<div>
<section class="game-container">
Expand Down
22 changes: 10 additions & 12 deletions examples/inner_html/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ extern crate stdweb;
use stdweb::unstable::TryFrom;
use stdweb::web::Node;
use yew::virtual_dom::VNode;
use yew::{Component, ComponentLink, Html, Renderable, ShouldRender};
use yew::{Component, ComponentLink, Html, ShouldRender};

const SVG: &str = r#"
<h2>Inline SVG or <i>any</i> HTML:</h2>
<svg height="250" width="500">
<polygon points="220,10 300,210 170,250 123,234" style="fill:lime;stroke:purple;stroke-width:1" />
Sorry, your browser does not support inline SVG.
</svg>
"#;

pub struct Model {
pub value: i64,
Expand All @@ -24,18 +32,8 @@ impl Component for Model {
fn update(&mut self, _: Self::Message) -> ShouldRender {
true
}
}

const SVG: &str = r#"
<h2>Inline SVG or <i>any</i> HTML:</h2>
<svg height="250" width="500">
<polygon points="220,10 300,210 170,250 123,234" style="fill:lime;stroke:purple;stroke-width:1" />
Sorry, your browser does not support inline SVG.
</svg>
"#;

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
let js_svg = js! {
var div = document.createElement("div");
div.innerHTML = @{SVG.to_string()};
Expand Down
4 changes: 1 addition & 3 deletions examples/js_callback/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ impl Component for Model {
fn change(&mut self, _: Self::Properties) -> ShouldRender {
false
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<div>
<textarea oninput=|input| Msg::Payload(input.value)
Expand Down
20 changes: 9 additions & 11 deletions examples/large_table/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This demo originally created by https://github.com/qthree
//! Source: https://github.com/qthree/yew_table100x100_test

use yew::{html, Component, ComponentLink, Html, Renderable, ShouldRender};
use yew::{html, Component, ComponentLink, Html, ShouldRender};

pub struct Model {
selected: Option<(u32, u32)>,
Expand All @@ -28,6 +28,14 @@ impl Component for Model {
}
true
}

fn render(&self) -> Html<Self> {
html! {
<table>
{ (0..99).map(|row| view_row(self.selected, row)).collect::<Html<Self>>() }
</table>
}
}
}

fn square_class(this: (u32, u32), selected: Option<(u32, u32)>) -> &'static str {
Expand All @@ -54,13 +62,3 @@ fn view_row(selected: Option<(u32, u32)>, row: u32) -> Html<Model> {
</tr>
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
html! {
<table>
{ (0..99).map(|row| view_row(self.selected, row)).collect::<Html<Self>>() }
</table>
}
}
}
6 changes: 2 additions & 4 deletions examples/minimal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use yew::{html, Component, ComponentLink, Html, Renderable, ShouldRender};
use yew::{html, Component, ComponentLink, Html, ShouldRender};

pub struct Model {}

Expand All @@ -20,10 +20,8 @@ impl Component for Model {
}
true
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<div>
<button onclick=|_| Msg::Click>{ "Click" }</button>
Expand Down
6 changes: 2 additions & 4 deletions examples/mount_point/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use yew::{html, Component, ComponentLink, Html, Renderable, ShouldRender};
use yew::{html, Component, ComponentLink, Html, ShouldRender};

pub struct Model {
name: String,
Expand Down Expand Up @@ -26,10 +26,8 @@ impl Component for Model {
}
true
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<div>
<input value=&self.name oninput=|e| Msg::UpdateName(e.value) />
Expand Down
6 changes: 2 additions & 4 deletions examples/multi_thread/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod native_worker;

use log::info;
use yew::worker::*;
use yew::{html, Component, ComponentLink, Html, Renderable, ShouldRender};
use yew::{html, Component, ComponentLink, Html, ShouldRender};

pub struct Model {
worker: Box<Bridge<native_worker::Worker>>,
Expand Down Expand Up @@ -65,10 +65,8 @@ impl Component for Model {
}
true
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<div>
<nav class="menu">
Expand Down
4 changes: 1 addition & 3 deletions examples/nested_list/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ impl Component for ListHeader {
}
false
}
}

impl Renderable<ListHeader> for ListHeader {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<div class="list-header" onmouseover=|_| Msg::Hover>
{ &self.props.text }
Expand Down
4 changes: 1 addition & 3 deletions examples/nested_list/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ impl Component for ListItem {
}
false
}
}

impl Renderable<ListItem> for ListItem {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<div class="list-item" onmouseover=|_| Msg::Hover>
{ &self.props.name }
Expand Down
4 changes: 1 addition & 3 deletions examples/nested_list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ impl Component for Model {
fn update(&mut self, _: Self::Message) -> ShouldRender {
true
}
}

impl Renderable<Model> for Model {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<div class="main">
<h1>{ "Nested List Demo" }</h1>
Expand Down
4 changes: 1 addition & 3 deletions examples/nested_list/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ impl Component for List {
}
true
}
}

impl Renderable<List> for List {
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
html! {
<div
class="list-container"
Expand Down
Loading

0 comments on commit b6bd146

Please sign in to comment.