Add fragments demo to the showcase

This commit is contained in:
Denis Kolodin 2018-04-10 10:38:14 +03:00
parent 47ba9a13df
commit 46af8ed056
5 changed files with 99 additions and 77 deletions

View File

@ -9,3 +9,4 @@ counter = { path = "sub/counter" }
crm = { path = "sub/crm" }
custom_components = { path = "sub/custom_components" }
dashboard = { path = "sub/dashboard" }
fragments = { path = "sub/fragments" }

View File

@ -4,6 +4,7 @@ extern crate counter;
extern crate crm;
extern crate custom_components;
extern crate dashboard;
extern crate fragments;
use yew::prelude::*;
use yew::services::console::ConsoleService;
@ -15,6 +16,7 @@ use counter::Model as Counter;
use crm::Model as Crm;
use custom_components::Model as CustomComponents;
use dashboard::Model as Dashboard;
use fragments::Model as Fragments;
struct Context {
console: ConsoleService,
@ -66,6 +68,7 @@ enum Scene {
Crm,
CustomComponents,
Dashboard,
Fragments,
}
enum Msg {
@ -99,6 +102,7 @@ impl Renderable<Context, Scene> for Scene {
<button onclick=|_| Msg::SwitchTo(Scene::Crm),>{ "Crm" }</button>
<button onclick=|_| Msg::SwitchTo(Scene::CustomComponents),>{ "CustomComponents" }</button>
<button onclick=|_| Msg::SwitchTo(Scene::Dashboard),>{ "Dashboard" }</button>
<button onclick=|_| Msg::SwitchTo(Scene::Fragments),>{ "Fragments" }</button>
{ self.view_scene() }
}
}
@ -132,6 +136,11 @@ impl Scene {
<Dashboard: />
}
}
Scene::Fragments => {
html! {
<Fragments: />
}
}
}
}
}

View File

@ -4,4 +4,4 @@ version = "0.1.0"
authors = ["Denis Kolodin <deniskolodin@gmail.com>"]
[dependencies]
yew = { path = "../.." }
yew = { path = "../../../.." }

View File

@ -0,0 +1,86 @@
#[macro_use]
extern crate yew;
use yew::prelude::*;
pub struct Model {
counter: usize,
}
pub enum Msg {
More,
Less,
}
impl<CTX> Component<CTX> for Model {
type Msg = Msg;
type Properties = ();
fn create(_: Self::Properties, _: &mut Env<CTX, Self>) -> Self {
Model {
counter: 0,
}
}
fn update(&mut self, msg: Self::Msg, _: &mut Env<CTX, Self>) -> ShouldRender {
match msg {
Msg::More => {
self.counter = self.counter + 1;
}
Msg::Less => {
if self.counter > 0 {
self.counter = self.counter - 1;
}
}
}
true
}
}
impl<CTX> Renderable<CTX, Model> for Model
where
CTX: 'static,
{
fn view(&self) -> Html<CTX, Self> {
html! {
<>
<nav class="menu",>{ self.view_menu() }</nav>
<table>
<tr>
{ self.view_cols() }
{ self.view_cols() }
{ self.view_cols() }
</tr>
</table>
</>
}
}
}
impl Model {
fn view_cols<CTX>(&self) -> Html<CTX, Self>
where
CTX: 'static,
{
let render = |idx| html! {
<td>{ idx }</td>
};
html! {
<>
{ for (0..self.counter).map(render) }
</>
}
}
fn view_menu<CTX>(&self) -> Html<CTX, Self>
where
CTX: 'static,
{
html! {
<>
<button onclick=|_| Msg::More,>{ "More" }</button>
<button onclick=|_| Msg::Less,>{ "Less" }</button>
</>
}
}
}

View File

@ -1,82 +1,8 @@
#[macro_use]
extern crate yew;
extern crate fragments;
use yew::prelude::*;
type Context = ();
struct Model {
counter: usize,
}
enum Msg {
More,
Less,
}
impl Component<Context> for Model {
type Msg = Msg;
type Properties = ();
fn create(_: Self::Properties, _: &mut Env<Context, Self>) -> Self {
Model {
counter: 0,
}
}
fn update(&mut self, msg: Self::Msg, _: &mut Env<Context, Self>) -> ShouldRender {
match msg {
Msg::More => {
self.counter = self.counter + 1;
}
Msg::Less => {
if self.counter > 0 {
self.counter = self.counter - 1;
}
}
}
true
}
}
impl Renderable<Context, Model> for Model {
fn view(&self) -> Html<Context, Self> {
html! {
<>
<nav class="menu",>{ self.view_menu() }</nav>
<table>
<tr>
{ self.view_cols() }
{ self.view_cols() }
{ self.view_cols() }
</tr>
</table>
</>
}
}
}
impl Model {
fn view_cols(&self) -> Html<Context, Self> {
let render = |idx| html! {
<td>{ idx }</td>
};
html! {
<>
{ for (0..self.counter).map(render) }
</>
}
}
fn view_menu(&self) -> Html<Context, Self> {
html! {
<>
<button onclick=|_| Msg::More,>{ "More" }</button>
<button onclick=|_| Msg::Less,>{ "Less" }</button>
</>
}
}
}
use fragments::Model;
fn main() {
yew::initialize();