diff --git a/examples/Cargo.lock b/examples/Cargo.lock index 000cc51..d1c5cef 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -54,6 +54,9 @@ dependencies = [ name = "basics" version = "0.2.0" dependencies = [ + "gloo-console 0.3.0", + "wasm-bindgen", + "web-sys", "yew", "yew-bootstrap", ] diff --git a/examples/basics/Cargo.toml b/examples/basics/Cargo.toml index ff53be8..40687c5 100644 --- a/examples/basics/Cargo.toml +++ b/examples/basics/Cargo.toml @@ -6,5 +6,8 @@ edition = "2021" license = "MIT" [dependencies] +gloo-console = "0.3.0" yew = { version = "0.21", features = ["csr"] } -yew-bootstrap = { path = "../../packages/yew-bootstrap" } \ No newline at end of file +yew-bootstrap = { path = "../../packages/yew-bootstrap" } +wasm-bindgen = "0.2.*" +web-sys = { version = "0.3.*", features = ["Event", "HtmlElement"] } diff --git a/examples/basics/src/main.rs b/examples/basics/src/main.rs index 7b568bb..98e4602 100644 --- a/examples/basics/src/main.rs +++ b/examples/basics/src/main.rs @@ -3,6 +3,9 @@ use yew::prelude::*; use yew_bootstrap::component::*; use yew_bootstrap::icons::*; use yew_bootstrap::util::*; +use gloo_console::debug; +use wasm_bindgen::JsCast; +use web_sys::HtmlElement; enum Msg {} struct Model {} @@ -22,16 +25,32 @@ impl Component for Model { icon: BI::ROCKET, }; + // Show a message in the debug console whenever a NavItem or + // NavDropdownItem is clicked. + let onclick = Callback::from(move |event: MouseEvent| { + let Some(target) = event.target() else { + return; + }; + let Ok(target) = target.dyn_into::() else { + return; + }; + debug!("onclick for:", target.inner_text().trim()); + // Stop the browser from actually following the "#" link. + event.prevent_default(); + }); + html! { <> {include_inline()} {BIFiles::cdn()} - - - - - + + + + + + + @@ -45,7 +64,7 @@ impl Component for Model {
-

{ "Accordian" }

+

{ "Accordion" }

{"Some text inside "}{"THE BODY"}{" of the accordion item"}

diff --git a/packages/yew-bootstrap/src/component/navbar.rs b/packages/yew-bootstrap/src/component/navbar.rs index a974f8f..f3147ba 100644 --- a/packages/yew-bootstrap/src/component/navbar.rs +++ b/packages/yew-bootstrap/src/component/navbar.rs @@ -4,20 +4,30 @@ use crate::util::Dimension; use crate::icons::BI; /// # A singular dropdown item, child of [NavDropdown] -/// Used as a child of [NavDropdown] to create a dropdown menu. -/// +/// Used as a child of [NavDropdown] to create a dropdown menu. +/// /// See [NavDropdownItemProps] for a listing of properties. pub struct NavDropdownItem { } /// # Properties for [NavDropdown] -#[derive(Properties, Clone, PartialEq, Eq)] +#[derive(Properties, Clone, PartialEq)] pub struct NavDropdownItemProps { /// Item text #[prop_or_default] pub text: AttrValue, /// Link for the item #[prop_or_default] - pub url: AttrValue, + pub url: Option, + /// Callback when clicked. + /// + /// **Tip:** To make browsers show a "link" mouse cursor for the + /// [NavDropdownItem], set `url="#"` and call [`Event::prevent_default()`] + /// from your callback. + #[prop_or_default] + pub onclick: Callback, + /// Optional icon + #[prop_or_default] + pub icon: Option<&'static BI>, } impl Component for NavDropdownItem { @@ -33,7 +43,16 @@ impl Component for NavDropdownItem { html! {
  • - {props.text.clone()} + + if let Some(icon) = props.icon { + {icon}{" "} + } + {props.text.clone()} +
  • } } @@ -59,7 +78,10 @@ pub struct NavDropdownProps { pub text: AttrValue, /// Top level path is the currently active one #[prop_or_default] - pub active: bool + pub active: bool, + /// Optional icon + #[prop_or_default] + pub icon: Option<&'static BI>, } impl Component for NavDropdown { @@ -78,7 +100,7 @@ impl Component for NavDropdown { false => "false" }); - + let mut dropdown_toggle_classes = Classes::new(); dropdown_toggle_classes.push(String::from("nav-link")); dropdown_toggle_classes.push(String::from("dropdown-toggle")); @@ -90,6 +112,9 @@ impl Component for NavDropdown { html! {