Skip to content

Commit 7939263

Browse files
authored
fix: fire opened-change only when opened state changes (#8230)
1 parent 5fe1975 commit 7939263

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
@@ -23,7 +23,6 @@
2323
import com.vaadin.flow.component.Component;
2424
import com.vaadin.flow.component.ComponentEvent;
2525
import com.vaadin.flow.component.ComponentEventListener;
26-
import com.vaadin.flow.component.DomEvent;
2726
import com.vaadin.flow.component.HasAriaLabel;
2827
import com.vaadin.flow.component.HasComponents;
2928
import com.vaadin.flow.component.Synchronize;
@@ -78,6 +77,9 @@ public Popover() {
7877

7978
updateTrigger();
8079
setRole("dialog");
80+
81+
getElement().addPropertyChangeListener("opened", event -> fireEvent(
82+
new OpenedChangeEvent(this, event.isUserOriginated())));
8183
}
8284

8385
/**
@@ -177,7 +179,6 @@ private static void applyConfigurationForUI(UI ui) {
177179
* {@code opened-changed} event is sent when the popover opened state
178180
* changes.
179181
*/
180-
@DomEvent("opened-changed")
181182
public static class OpenedChangeEvent extends ComponentEvent<Popover> {
182183
private final boolean opened;
183184

@@ -200,7 +201,6 @@ public boolean isOpened() {
200201
public void setOpened(boolean opened) {
201202
if (opened != isOpened()) {
202203
getElement().setProperty("opened", opened);
203-
fireEvent(new OpenedChangeEvent(this, false));
204204
}
205205
}
206206

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,6 +25,10 @@
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.JacksonUtils;
31+
import com.vaadin.flow.internal.nodefeature.ElementListenerMap;
2832
import com.vaadin.flow.server.VaadinSession;
2933

3034
public class PopoverOpenedChangeListenerTest {
@@ -87,6 +91,19 @@ public void close() {
8791
assertListenerCalls(0);
8892
}
8993

94+
@Test
95+
public void openedChangeFromClient_noChangeEvent() {
96+
Element element = popover.getElement();
97+
element.getNode().getFeature(ElementListenerMap.class)
98+
.fireEvent(new DomEvent(element, "opened-changed",
99+
JacksonUtils.createObjectNode()));
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)