Skip to content

Commit

Permalink
Move components out of core crate (#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Apr 25, 2020
1 parent 037d18c commit 8cda60d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 41 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
@@ -1,6 +1,7 @@
[workspace]
members = [
"yew",
"yew-components",
"yew-functional",
"yew-macro",

Expand Down
23 changes: 23 additions & 0 deletions yew-components/Cargo.toml
@@ -0,0 +1,23 @@
[package]
name = "yew-components"
version = "0.1.0"
edition = "2018"
authors = [
"Yew Maintainers <maintainers@yew.rs>",
]
repository = "https://github.com/yewstack/yew"
homepage = "https://github.com/yewstack/yew"
documentation = "https://docs.rs/yew-components/"
license = "MIT/Apache-2.0"
readme = "README.md"
keywords = ["web", "asmjs", "webasm", "javascript"]
categories = ["gui", "web-programming"]
description = "A collection of community-created Yew components"

[badges]
travis-ci = { repository = "yewstack/yew" }

[dependencies]
wasm-bindgen = "0.2.60"
web-sys = "0.3"
yew = { path = "../yew" }
8 changes: 8 additions & 0 deletions yew-components/src/lib.rs
@@ -0,0 +1,8 @@
//! This crate contains useful Yew components.
//! At this moment it only includes typed `Select`.

#[doc(hidden)]
pub mod select;

#[doc(inline)]
pub use self::select::Select;
45 changes: 14 additions & 31 deletions yew/src/components/select.rs → yew-components/src/select.rs
Expand Up @@ -28,23 +28,15 @@
//! fn view(link: ComponentLink<Model>) -> Html {
//! let scenes = vec![Scene::First, Scene::Second];
//! html! {
//! <Select<Scene> options=scenes onchange=link.callback(|_| ()) />
//! <Select<Scene> options=scenes on_change=link.callback(|_| ()) />
//! }
//! }
//! ```

use crate::callback::Callback;
use crate::html::{ChangeData, Component, ComponentLink, Html, NodeRef, ShouldRender};
use crate::macros::{html, Properties};
use cfg_if::cfg_if;
use cfg_match::cfg_match;
cfg_if! {
if #[cfg(feature = "std_web")] {
use stdweb::web::html_element::SelectElement;
} else if #[cfg(feature = "web_sys")] {
use web_sys::HtmlSelectElement as SelectElement;
}
}
use web_sys::HtmlSelectElement;
use yew::callback::Callback;
use yew::html::{ChangeData, Component, ComponentLink, Html, NodeRef, ShouldRender};
use yew::macros::{html, Properties};

/// `Select` component.
#[derive(Debug)]
Expand Down Expand Up @@ -74,7 +66,7 @@ pub struct Props<T: Clone> {
#[prop_or_default]
pub options: Vec<T>,
/// Callback to handle changes.
pub onchange: Callback<T>,
pub on_change: Callback<T>,
}

impl<T> Component for Select<T>
Expand All @@ -96,9 +88,9 @@ where
match msg {
Msg::Selected(value) => {
if let Some(idx) = value {
let item = self.props.options.get(idx - 1).cloned();
let item = self.props.options.get(idx - 1);
if let Some(value) = item {
self.props.onchange.emit(value);
self.props.on_change.emit(value.clone());
}
}
}
Expand All @@ -108,16 +100,13 @@ where

fn change(&mut self, props: Self::Properties) -> ShouldRender {
if self.props.selected != props.selected {
if let Some(select) = self.select_ref.cast::<SelectElement>() {
if let Some(select) = self.select_ref.cast::<HtmlSelectElement>() {
let val = props
.selected
.as_ref()
.map(|v| v.to_string())
.unwrap_or_default();
cfg_match! {
feature = "std_web" => select.set_raw_value(&val),
feature = "web_sys" => select.set_value(&val),
}
select.set_value(&val);
}
}
self.props = props;
Expand All @@ -134,7 +123,7 @@ where
};

html! {
<select ref=self.select_ref.clone() disabled=self.props.disabled onchange=self.onchange()>
<select ref=self.select_ref.clone() disabled=self.props.disabled onchange=self.on_change()>
<option value="" disabled=true selected=selected.is_none()>
{ "↪" }
</option>
Expand All @@ -148,19 +137,13 @@ impl<T> Select<T>
where
T: ToString + PartialEq + Clone + 'static,
{
fn onchange(&self) -> Callback<ChangeData> {
fn on_change(&self) -> Callback<ChangeData> {
self.link.callback(|event| match event {
ChangeData::Select(elem) => {
let value = elem.selected_index();
let value = cfg_match! {
feature = "std_web" => value.map(|x| x as usize),
feature = "web_sys" => Some(value as usize),
};
Msg::Selected(value)
}
_ => {
unreachable!();
Msg::Selected(Some(value as usize))
}
_ => unreachable!(),
})
}
}
2 changes: 1 addition & 1 deletion yew-stdweb/Cargo.toml
Expand Up @@ -8,7 +8,7 @@ authors = [
]
repository = "https://github.com/yewstack/yew"
homepage = "https://github.com/yewstack/yew"
documentation = "https://docs.rs/yew/"
documentation = "https://docs.rs/yew-stdweb/"
license = "MIT/Apache-2.0"
readme = "README.md"
keywords = ["web", "asmjs", "webasm", "javascript"]
Expand Down
8 changes: 0 additions & 8 deletions yew/src/components/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion yew/src/lib.rs
Expand Up @@ -103,7 +103,6 @@ pub mod macros {

pub mod app;
pub mod callback;
pub mod components;
pub mod format;
pub mod html;
mod scheduler;
Expand Down

0 comments on commit 8cda60d

Please sign in to comment.