Skip to content

Commit

Permalink
feat: add API to set Popover position
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan committed May 29, 2024
1 parent e600de8 commit 61ddb5c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.vaadin.flow.component.popover;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -61,6 +62,28 @@ public Popover() {
getElement().getNode().addAttachListener(this::attachComponentRenderer);
}

/**
* Sets position of the popover with respect to its target.
*
* @param position
* the position to set
*/
public void setPosition(PopoverPosition position) {
getElement().setProperty("position", position.getPosition());
}

/**
* Gets position of the popover with respect to its target.
*
* @return the position
*/
public PopoverPosition getPosition() {
var positionString = getElement().getProperty("position");
return Arrays.stream(PopoverPosition.values())
.filter(p -> p.getPosition().equals(positionString)).findFirst()
.orElse(null);
}

/**
* The {@code id} of the element to be used as the popover {@code target}
* value.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.vaadin.flow.component.popover;

/**
* Popover position in relation to the target element.
*/
public enum PopoverPosition {
//@formatter:off
TOP_START("top-start"),
TOP("top"),
TOP_END("top-end"),
BOTTOM_START("bottom-start"),
BOTTOM("bottom"),
BOTTOM_END("bottom-end"),
START_TOP("start-top"),
START("start"),
START_BOTTOM("start-bottom"),
END_TOP("end-top"),
END("end"),
END_BOTTOM("end-bottom");
//@formatter:off

private final String position;

PopoverPosition(String position) {
this.position = position;
}

public String getPosition() {
return position;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,17 @@ public void setTarget_textNodeAsComponent_throws() {
Text textNode = new Text("Text");
popover.setTarget(textNode);
}

@Test
public void setPosition_getPosition() {
popover.setPosition(PopoverPosition.END);
Assert.assertEquals("end",
popover.getElement().getProperty("position"));
Assert.assertEquals(PopoverPosition.END, popover.getPosition());
}

@Test
public void defaultPosition_equalsNull() {
Assert.assertEquals(null, popover.getPosition());
}
}

0 comments on commit 61ddb5c

Please sign in to comment.