From 25bd4e483d69e259b18cf3b03452f731a87daf8e Mon Sep 17 00:00:00 2001 From: Serhii Kulykov Date: Wed, 10 Jul 2024 09:41:06 +0300 Subject: [PATCH] feat: add methods to configure Popover trigger mode (#6420) --- .../flow/component/popover/Popover.java | 96 +++++++++++++++++++ .../flow/component/popover/PopoverTest.java | 83 ++++++++++++++++ 2 files changed, 179 insertions(+) diff --git a/vaadin-popover-flow-parent/vaadin-popover-flow/src/main/java/com/vaadin/flow/component/popover/Popover.java b/vaadin-popover-flow-parent/vaadin-popover-flow/src/main/java/com/vaadin/flow/component/popover/Popover.java index 319b2be3a72..fb237aedd5e 100644 --- a/vaadin-popover-flow-parent/vaadin-popover-flow/src/main/java/com/vaadin/flow/component/popover/Popover.java +++ b/vaadin-popover-flow-parent/vaadin-popover-flow/src/main/java/com/vaadin/flow/component/popover/Popover.java @@ -23,6 +23,9 @@ import java.util.Objects; import java.util.stream.Collectors; +import elemental.json.Json; +import elemental.json.JsonArray; + import com.vaadin.flow.component.AttachEvent; import com.vaadin.flow.component.Component; import com.vaadin.flow.component.HasComponents; @@ -55,11 +58,17 @@ public class Popover extends Component implements HasComponents { private Component target; private Registration targetAttachRegistration; + private boolean openOnClick = true; + private boolean openOnHover = false; + private boolean openOnFocus = false; + /** * Constructs an empty popover. */ public Popover() { getElement().getNode().addAttachListener(this::attachComponentRenderer); + + updateTrigger(); } /** @@ -171,6 +180,93 @@ public int getHideDelay() { return getElement().getProperty("hideDelay", 0); } + /** + * Sets whether the popover can be opened via target click. + * + * @param openOnClick + * {@code true} to allow opening the popover via target click, + * {@code false} to disallow it. + */ + public void setOpenOnClick(boolean openOnClick) { + this.openOnClick = openOnClick; + updateTrigger(); + } + + /** + * Gets whether the popover can be opened via target click, which is + * {@code true} by default. + * + * @return {@code true} if the popover can be opened with target click, + * {@code false} otherwise. + */ + public boolean isOpenOnClick() { + return this.openOnClick; + } + + /** + * Sets whether the popover can be opened via target focus. + * + * @param openOnFocus + * {@code true} to allow opening the popover via target focus, + * {@code false} to disallow it. + */ + public void setOpenOnFocus(boolean openOnFocus) { + this.openOnFocus = openOnFocus; + updateTrigger(); + } + + /** + * Gets whether the popover can be opened via target focus, which is + * {@code false} by default. + * + * @return {@code true} if the popover can be opened with target focus, + * {@code false} otherwise. + */ + public boolean isOpenOnFocus() { + return this.openOnFocus; + } + + /** + * Sets whether the popover can be opened via target hover. + * + * @param openOnHover + * {@code true} to allow opening the popover via target hover, + * {@code false} to disallow it. + */ + public void setOpenOnHover(boolean openOnHover) { + this.openOnHover = openOnHover; + updateTrigger(); + } + + /** + * Gets whether the popover can be opened via target hover, which is + * {@code false} by default. + * + * @return {@code true} if the popover can be opened with target hover, + * {@code false} otherwise. + */ + public boolean isOpenOnHover() { + return this.openOnHover; + } + + private void updateTrigger() { + JsonArray trigger = Json.createArray(); + + if (isOpenOnClick()) { + trigger.set(trigger.length(), "click"); + } + + if (isOpenOnHover()) { + trigger.set(trigger.length(), "hover"); + } + + if (isOpenOnFocus()) { + trigger.set(trigger.length(), "focus"); + } + + getElement().setPropertyJson("trigger", trigger); + } + /** * Sets the target component for this popover. *

diff --git a/vaadin-popover-flow-parent/vaadin-popover-flow/src/test/java/com/vaadin/flow/component/popover/PopoverTest.java b/vaadin-popover-flow-parent/vaadin-popover-flow/src/test/java/com/vaadin/flow/component/popover/PopoverTest.java index 03d148552ac..45bb4717f9a 100644 --- a/vaadin-popover-flow-parent/vaadin-popover-flow/src/test/java/com/vaadin/flow/component/popover/PopoverTest.java +++ b/vaadin-popover-flow-parent/vaadin-popover-flow/src/test/java/com/vaadin/flow/component/popover/PopoverTest.java @@ -16,6 +16,8 @@ */ package com.vaadin.flow.component.popover; +import elemental.json.JsonArray; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -95,4 +97,85 @@ public void setHideDelay_getHideDelay() { Assert.assertEquals(1000, popover.getElement().getProperty("hideDelay", 0)); } + + @Test + public void isOpenOnClick_trueByDefault() { + Assert.assertTrue(popover.isOpenOnClick()); + } + + @Test + public void isOpenOnFocus_falseByDefault() { + Assert.assertFalse(popover.isOpenOnFocus()); + } + + @Test + public void isOpenOnHover_falseByDefault() { + Assert.assertFalse(popover.isOpenOnHover()); + } + + @Test + public void setOpenOnClick_isOpenOnClick() { + popover.setOpenOnClick(false); + Assert.assertFalse(popover.isOpenOnClick()); + + popover.setOpenOnClick(true); + Assert.assertTrue(popover.isOpenOnClick()); + } + + @Test + public void setOpenOnFocus_isOpenOnFocus() { + popover.setOpenOnFocus(true); + Assert.assertTrue(popover.isOpenOnFocus()); + + popover.setOpenOnFocus(false); + Assert.assertFalse(popover.isOpenOnFocus()); + } + + @Test + public void setOpenOnHover_isOpenOnHover() { + popover.setOpenOnHover(true); + Assert.assertTrue(popover.isOpenOnHover()); + + popover.setOpenOnHover(false); + Assert.assertFalse(popover.isOpenOnHover()); + } + + @Test + public void getTriggerProperty_defaultValue_click() { + JsonArray jsonArray = (JsonArray) popover.getElement() + .getPropertyRaw("trigger"); + Assert.assertEquals(1, jsonArray.length()); + Assert.assertEquals("click", jsonArray.get(0).asString()); + } + + @Test + public void setOpenOnClick_triggerPropertyUpdated() { + popover.setOpenOnClick(false); + + JsonArray jsonArray = (JsonArray) popover.getElement() + .getPropertyRaw("trigger"); + Assert.assertEquals(0, jsonArray.length()); + } + + @Test + public void setOpenOnFocus_triggerPropertyUpdated() { + popover.setOpenOnFocus(true); + + JsonArray jsonArray = (JsonArray) popover.getElement() + .getPropertyRaw("trigger"); + Assert.assertEquals(2, jsonArray.length()); + Assert.assertEquals("click", jsonArray.get(0).asString()); + Assert.assertEquals("focus", jsonArray.get(1).asString()); + } + + @Test + public void setOpenOnHover_triggerPropertyUpdated() { + popover.setOpenOnHover(true); + + JsonArray jsonArray = (JsonArray) popover.getElement() + .getPropertyRaw("trigger"); + Assert.assertEquals(2, jsonArray.length()); + Assert.assertEquals("click", jsonArray.get(0).asString()); + Assert.assertEquals("hover", jsonArray.get(1).asString()); + } }