public inbox for yew-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: yew-devel@lists.proxmox.com
Subject: [yew-devel] [PATCH yew-widget-toolkit 2/2] ui: list tile: implement on_activate callback
Date: Fri, 29 Aug 2025 15:46:18 +0200	[thread overview]
Message-ID: <20250829134625.2653743-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20250829134625.2653743-1-d.csapak@proxmox.com>

sometimes it's useful to click a full list tile, so implement the `on_activate`
callback like we do for e.g. buttons, triggers, etc.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/widget/list/list_tile.rs | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/src/widget/list/list_tile.rs b/src/widget/list/list_tile.rs
index 2613fb1..3c373c3 100644
--- a/src/widget/list/list_tile.rs
+++ b/src/widget/list/list_tile.rs
@@ -1,10 +1,11 @@
 use std::borrow::Cow;
 
-use yew::html::IntoPropValue;
+use wasm_bindgen::JsCast;
+use yew::html::{IntoEventCallback, IntoPropValue};
 use yew::prelude::*;
 use yew::virtual_dom::VTag;
 
-use crate::props::{ListenersWrapper, WidgetStdProps};
+use crate::props::{EventSubscriber, ListenersWrapper, WidgetBuilder, WidgetStdProps};
 
 use pwt_macros::{builder, widget};
 
@@ -43,6 +44,11 @@ pub struct ListTile {
     #[prop_or_default]
     #[builder(IntoPropValue, into_prop_value)]
     force_height: Option<u32>,
+
+    /// Activate callback (click, enter, space)
+    #[builder_cb(IntoEventCallback, into_event_callback, Event)]
+    #[prop_or_default]
+    on_activate: Option<Callback<Event>>,
 }
 
 impl ListTile {
@@ -61,12 +67,36 @@ impl ListTile {
 }
 
 impl From<ListTile> for VTag {
-    fn from(val: ListTile) -> Self {
+    fn from(mut val: ListTile) -> Self {
         let classes = classes!(
             "pwt-list-tile",
             val.interactive.then_some("pwt-interactive"),
             val.disabled.then_some("disabled")
         );
+
+        if !val.disabled {
+            if let Some(on_activate) = val.on_activate.clone() {
+                val.set_tabindex(0);
+                val.add_onclick({
+                    let on_activate = on_activate.clone();
+                    move |event: MouseEvent| {
+                        event.stop_propagation();
+                        on_activate.emit(event.unchecked_into());
+                    }
+                });
+                val.add_onkeydown({
+                    let on_activate = on_activate.clone();
+                    move |event: KeyboardEvent| match event.key().as_str() {
+                        "Enter" | " " => {
+                            event.stop_propagation();
+                            on_activate.emit(event.unchecked_into());
+                        }
+                        _ => {}
+                    }
+                });
+            }
+        }
+
         val.std_props.into_vtag(
             Cow::Borrowed("div"),
             Some(classes),
-- 
2.47.2



_______________________________________________
yew-devel mailing list
yew-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/yew-devel


      reply	other threads:[~2025-08-29 13:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-29 13:46 [yew-devel] [PATCH yew-widget-toolkit 1/2] widget: list: expose `row-gap` and 'column-gap' property for grid layout Dominik Csapak
2025-08-29 13:46 ` Dominik Csapak [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250829134625.2653743-2-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=yew-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal