Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ members = [
"examples/file_upload",
"examples/futures",
"examples/game_of_life",
"examples/hello",
"examples/inner_html",
"examples/js_callback",
"examples/keyed_list",
Expand Down
9 changes: 9 additions & 0 deletions examples/hello/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "hello"
version = "0.1.0"
authors = ["Yozhgoor <yozhgoor@outlook.com>"]
edition = "2018"

[dependencies]
js-sys = "0.3"
yew = { path = "../../yew" }
9 changes: 9 additions & 0 deletions examples/hello/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Hello Example

[![Demo](https://img.shields.io/website?label=demo&url=https%3A%2F%2Fexamples.yew.rs%2Fhello)](https://examples.yew.rs/hello)

A "Hello world!" example.

## Concepts

Pass component as property.
9 changes: 9 additions & 0 deletions examples/hello/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Yew • Hello</title>
</head>

<body></body>
</html>
85 changes: 85 additions & 0 deletions examples/hello/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use yew::prelude::*;

pub struct Model {
link: ComponentLink<Self>,
value: String,
}

impl Component for Model {
type Message = ChangeData;
type Properties = ();

fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self {
Self {
link,
value: String::from(""),
}
}

fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
ChangeData::Value(value) => self.value = value,
_ => unreachable!(),
}
true
}

fn change(&mut self, _props: Self::Properties) -> ShouldRender {
false
}

fn view(&self) -> Html {
html! {
<div>
<input onchange=self.link.callback(|x| x) />
<Hello name=self.value.clone() />
<Hello name="world!" />
<Hello name=html!(<strong>{"WORLD"}</strong>) />
</div>
}
}
}

struct Hello {
props: HelloProps,
}

#[derive(Clone, PartialEq, Properties)]
struct HelloProps {
name: yew::virtual_dom::VNode,
}

impl Component for Hello {
type Message = ();
type Properties = HelloProps;

fn create(props: Self::Properties, _link: ComponentLink<Self>) -> Self {
Self { props }
}

fn update(&mut self, _msg: Self::Message) -> ShouldRender {
true
}

fn change(&mut self, props: Self::Properties) -> ShouldRender {
if props != self.props {
self.props = props;
true
} else {
false
}
}

fn view(&self) -> Html {
return html! {
<div>
{"Hello "}
{self.props.name.clone()}
</div>
};
}
}

fn main() {
yew::start_app::<Model>();
}
14 changes: 13 additions & 1 deletion yew/src/virtual_dom/vnode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module contains the implementation of abstract virtual node.

use super::{Key, VChild, VComp, VDiff, VList, VTag, VText};
use super::{Key, Transformer, VChild, VComp, VDiff, VList, VTag, VText};
use crate::html::{AnyScope, Component, NodeRef};
use cfg_if::cfg_if;
use cfg_match::cfg_match;
Expand Down Expand Up @@ -218,6 +218,18 @@ impl PartialEq for VNode {
}
}

impl Transformer<&str, VNode> for VComp {
fn transform(from: &str) -> VNode {
VNode::VText(VText::new(from.to_string()))
}
}

impl Transformer<String, VNode> for VComp {
fn transform(from: String) -> VNode {
VNode::VText(VText::new(from))
}
}

#[cfg(all(test, feature = "web_sys"))]
mod layout_tests {
use super::*;
Expand Down