Skip to content

Commit

Permalink
feat: add methods to configure Popover trigger mode (#6420)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Jul 10, 2024
1 parent 60244a7 commit 25bd4e4
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 25bd4e4

Please sign in to comment.