Skip to content

Commit e0f053b

Browse files
fix: fire opened-change only when opened state changes (#8230) (#8234)
Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
1 parent 90cc126 commit e0f053b

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

vaadin-popover-flow-parent/vaadin-popover-flow/src/main/java/com/vaadin/flow/component/popover/Popover.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.vaadin.flow.component.Component;
2929
import com.vaadin.flow.component.ComponentEvent;
3030
import com.vaadin.flow.component.ComponentEventListener;
31-
import com.vaadin.flow.component.DomEvent;
3231
import com.vaadin.flow.component.HasAriaLabel;
3332
import com.vaadin.flow.component.HasComponents;
3433
import com.vaadin.flow.component.Synchronize;
@@ -93,6 +92,9 @@ public Popover() {
9392

9493
updateTrigger();
9594
setOverlayRole("dialog");
95+
96+
getElement().addPropertyChangeListener("opened", event -> fireEvent(
97+
new OpenedChangeEvent(this, event.isUserOriginated())));
9698
}
9799

98100
/**
@@ -192,7 +194,6 @@ private static void applyConfigurationForUI(UI ui) {
192194
* {@code opened-changed} event is sent when the overlay opened state
193195
* changes.
194196
*/
195-
@DomEvent("opened-changed")
196197
public static class OpenedChangeEvent extends ComponentEvent<Popover> {
197198
private final boolean opened;
198199

@@ -215,7 +216,6 @@ public boolean isOpened() {
215216
public void setOpened(boolean opened) {
216217
if (opened != isOpened()) {
217218
getElement().setProperty("opened", opened);
218-
fireEvent(new OpenedChangeEvent(this, false));
219219
}
220220
}
221221

vaadin-popover-flow-parent/vaadin-popover-flow/src/test/java/com/vaadin/flow/component/popover/PopoverOpenedChangeListenerTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@
2525

2626
import com.vaadin.flow.component.ComponentEventListener;
2727
import com.vaadin.flow.component.UI;
28+
import com.vaadin.flow.dom.DomEvent;
29+
import com.vaadin.flow.dom.Element;
30+
import com.vaadin.flow.internal.nodefeature.ElementListenerMap;
2831
import com.vaadin.flow.server.VaadinSession;
2932

33+
import elemental.json.Json;
34+
3035
public class PopoverOpenedChangeListenerTest {
3136
private final UI ui = new UI();
3237
private Popover popover;
@@ -87,6 +92,18 @@ public void close() {
8792
assertListenerCalls(0);
8893
}
8994

95+
@Test
96+
public void openedChangeFromClient_noChangeEvent() {
97+
Element element = popover.getElement();
98+
element.getNode().getFeature(ElementListenerMap.class).fireEvent(
99+
new DomEvent(element, "opened-changed", Json.createObject()));
100+
101+
// The event should only be fired when the opened state changes, not for
102+
// any opened-changed event from the client
103+
Assert.assertNull(event.get());
104+
assertListenerCalls(0);
105+
}
106+
90107
private void assertListenerCalls(int expectedCount) {
91108
Mockito.verify(mockListener, Mockito.times(expectedCount))
92109
.onComponentEvent(Mockito.any());

0 commit comments

Comments
 (0)