diff --git a/Cargo.toml b/Cargo.toml index 7aa3f0b6032..4bc84e7cb2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "yew", + "yew-components", "yew-functional", "yew-macro", diff --git a/yew-components/Cargo.toml b/yew-components/Cargo.toml new file mode 100644 index 00000000000..56a2a2b5afe --- /dev/null +++ b/yew-components/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "yew-components" +version = "0.1.0" +edition = "2018" +authors = [ + "Yew Maintainers ", +] +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" } diff --git a/yew-components/src/lib.rs b/yew-components/src/lib.rs new file mode 100644 index 00000000000..1cec26830ed --- /dev/null +++ b/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; diff --git a/yew/src/components/select.rs b/yew-components/src/select.rs similarity index 74% rename from yew/src/components/select.rs rename to yew-components/src/select.rs index bae1b08c9ea..ae9982b7545 100644 --- a/yew/src/components/select.rs +++ b/yew-components/src/select.rs @@ -28,23 +28,15 @@ //! fn view(link: ComponentLink) -> Html { //! let scenes = vec![Scene::First, Scene::Second]; //! html! { -//! options=scenes onchange=link.callback(|_| ()) /> +//! 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)] @@ -74,7 +66,7 @@ pub struct Props { #[prop_or_default] pub options: Vec, /// Callback to handle changes. - pub onchange: Callback, + pub on_change: Callback, } impl Component for Select @@ -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()); } } } @@ -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::() { + if let Some(select) = self.select_ref.cast::() { 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; @@ -134,7 +123,7 @@ where }; html! { - @@ -148,19 +137,13 @@ impl Select where T: ToString + PartialEq + Clone + 'static, { - fn onchange(&self) -> Callback { + fn on_change(&self) -> Callback { 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!(), }) } } diff --git a/yew-stdweb/Cargo.toml b/yew-stdweb/Cargo.toml index f3fcc3f1286..d7ceb7c2937 100644 --- a/yew-stdweb/Cargo.toml +++ b/yew-stdweb/Cargo.toml @@ -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"] diff --git a/yew/src/components/mod.rs b/yew/src/components/mod.rs deleted file mode 100644 index d9233ee8fcf..00000000000 --- a/yew/src/components/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -//! This module contains useful components. -//! At this moment it includes typed `Select` only. - -#[doc(hidden)] -pub mod select; - -#[doc(inline)] -pub use self::select::Select; diff --git a/yew/src/lib.rs b/yew/src/lib.rs index d788171a1c8..cf1fcf13fa0 100644 --- a/yew/src/lib.rs +++ b/yew/src/lib.rs @@ -103,7 +103,6 @@ pub mod macros { pub mod app; pub mod callback; -pub mod components; pub mod format; pub mod html; mod scheduler;