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 Aug 4, 2019
1 parent fea73fe commit 63b36a9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/components/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! }

use crate::callback::Callback;
use crate::html::{ChangeData, Component, ComponentLink, Html, Renderable, ShouldRender};
use crate::html::{ChangeData, Component, ComponentLink, Html, ShouldRender};
use crate::macros::{html, Properties};

/// `Select` component.
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct Props<T> {

impl<T> Component for Select<T>
where
T: PartialEq + Clone + 'static,
T: ToString + PartialEq + Clone + 'static,
{
type Message = Msg;
type Properties = Props<T>;
Expand All @@ -73,13 +73,8 @@ where
self.props = props;
true
}
}

impl<T> Renderable<Select<T>> for Select<T>
where
T: ToString + PartialEq + Clone + 'static,
{
fn view(&self) -> Html<Self> {
fn render(&self) -> Html<Self> {
let selected = self.props.selected.as_ref();
let view_option = |value: &T| {
let flag = selected == Some(value);
Expand Down
8 changes: 8 additions & 0 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub trait Component: Sized + 'static {
fn change(&mut self, _: Self::Properties) -> ShouldRender {
true
}
/// Called by rendering loop.
fn render(&self) -> Html<Self>;
/// Called for finalization on the final point of the component's lifetime.
fn destroy(&mut self) {} // TODO Replace with `Drop`
}
Expand Down Expand Up @@ -68,6 +70,12 @@ pub trait Renderable<COMP: Component> {
fn view(&self) -> Html<COMP>;
}

impl<COMP: Component> Renderable<COMP> for COMP {
fn view(&self) -> Html<COMP> {
self.render()
}
}

/// Updates for a `Components` instance. Used by scope sender.
pub(crate) enum ComponentUpdate<COMP: Component> {
/// Wraps messages for a component.
Expand Down

0 comments on commit 63b36a9

Please sign in to comment.