public inbox for yew-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [PATCH yew-widget-toolkit] widget: dropdown: keep the input element when the triggers change
@ 2026-11-30 15:49 Dominik Csapak
  2026-12-01  8:05 ` applied: " Dietmar Maurer
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Csapak @ 2026-11-30 15:49 UTC (permalink / raw)
  To: yew-devel

Since the triggers were added as direct siblings of the input element,
adding or removing one changes the number of children. Yew matches
unkeyed children from the end, so it reuses the input element for the
new trigger and creates a new input element, which loses the focus.

This happens for example with a non-editable selector without the
'required' flag: after picking a value, it adds its clear trigger and
the focus goes to the document body. Components that react to the
focus leaving them, like a popup that closes then, behave as if the
user clicked somewhere else.

To fix that, wrap the triggers in 'display: contents' containers, like
the field already does, so the number of children stays the same.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/widget/dropdown.rs | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/src/widget/dropdown.rs b/src/widget/dropdown.rs
index 68ec2c3..9463b5d 100644
--- a/src/widget/dropdown.rs
+++ b/src/widget/dropdown.rs
@@ -519,22 +519,25 @@ impl Component for PwtDropdown {
             })
             .onclick(onclick);
 
-        for (trigger, right) in &props.trigger {
-            if !right {
-                let outer_class = "pwt-flex-fill-first-child pwt-d-flex pwt-align-self-center";
-                select.add_child(html! {<div class={outer_class}>{trigger}</div>});
-            }
-        }
-
-        select.add_child(input);
+        // Always create the same children, so that adding a trigger (e.g. the clear trigger of a
+        // selector) does not replace the input element, which would lose the focus.
+        let mut left_triggers = Container::new().style("display", "contents");
+        let mut right_triggers = Container::new().style("display", "contents");
 
         for (trigger, right) in &props.trigger {
+            let outer_class = "pwt-flex-fill-first-child pwt-d-flex pwt-align-self-center";
+            let trigger = html! {<div class={outer_class}>{trigger}</div>};
             if *right {
-                let outer_class = "pwt-flex-fill-first-child pwt-d-flex pwt-align-self-center";
-                select.add_child(html! {<div class={outer_class}>{trigger}</div>});
+                right_triggers.add_child(trigger);
+            } else {
+                left_triggers.add_child(trigger);
             }
         }
 
+        select.add_child(left_triggers);
+        select.add_child(input);
+        select.add_child(right_triggers);
+
         select
             .add_child(html! {<i onclick={trigger_onclick} tabindex="-1" class={trigger_cls}></i>});
 
-- 
2.47.3





^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01  8:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 15:49 [PATCH yew-widget-toolkit] widget: dropdown: keep the input element when the triggers change Dominik Csapak
2026-12-01  8:05 ` applied: " Dietmar Maurer

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