Skip to content

Commit

Permalink
When ComboBox popup opens to the left accommodate margin/border/paddi…
Browse files Browse the repository at this point in the history
…ng. (#11755) (#11762)

Fixes #11718
  • Loading branch information
Ansku authored Oct 24, 2019
1 parent c6bd401 commit 0d19979
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
18 changes: 10 additions & 8 deletions client/src/main/java/com/vaadin/client/ui/VComboBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,8 @@ public void setPosition(int offsetWidth, int offsetHeight) {

updateMenuWidth(desiredWidth, naturalMenuWidth);

double menuMarginBorderPaddingWidth = getMarginBorderPaddingWidth(
menu.getElement());
if (BrowserInfo.get().isIE()
&& BrowserInfo.get().getBrowserMajorVersion() < 11) {
// Must take margin,border,padding manually into account for
Expand All @@ -805,15 +807,13 @@ public void setPosition(int offsetWidth, int offsetHeight) {
.getStyle().getVisibility();
menu.getElement().getParentElement().getStyle()
.setVisibility(Visibility.VISIBLE);
naturalMenuOuterWidth = WidgetUtil
.getRequiredWidthDouble(menuFirstChild)
+ getMarginBorderPaddingWidth(menu.getElement());
naturalMenuOuterWidth = WidgetUtil.getRequiredWidthDouble(
menuFirstChild) + menuMarginBorderPaddingWidth;
menu.getElement().getParentElement().getStyle()
.setProperty("visibility", before);
} else {
naturalMenuOuterWidth = WidgetUtil
.getRequiredWidthDouble(menuFirstChild)
+ getMarginBorderPaddingWidth(menu.getElement());
naturalMenuOuterWidth = WidgetUtil.getRequiredWidthDouble(
menuFirstChild) + menuMarginBorderPaddingWidth;
}

/*
Expand Down Expand Up @@ -893,9 +893,11 @@ public void setPosition(int offsetWidth, int offsetHeight) {
}
}

if (offsetWidth + left > Window.getClientWidth()) {
if (offsetWidth + menuMarginBorderPaddingWidth + left > Window
.getClientWidth()) {
left = VComboBox.this.getAbsoluteLeft()
+ VComboBox.this.getOffsetWidth() - offsetWidth;
+ VComboBox.this.getOffsetWidth() - offsetWidth
- (int) menuMarginBorderPaddingWidth;
if (left < 0) {
left = 0;
menu.setWidth(Window.getClientWidth() + "px");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.vaadin.tests.components.combobox;

import java.util.Arrays;

import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.VerticalLayout;

public class ComboBoxAtRightEdge extends AbstractTestUI {

@Override
protected void setup(VaadinRequest request) {
ComboBox<String> comboBox = new ComboBox<>("Long items?");
comboBox.setPopupWidth(null);
comboBox.setItems(Arrays.asList("First Very long item to add",
"Second very long item to add", "Third very long item to add"));
comboBox.addStyleName("positionRight");

addComponent(comboBox);
getLayout().setComponentAlignment(comboBox, Alignment.BOTTOM_RIGHT);

((VerticalLayout) getLayout().getParent()).setMargin(false);
}

@Override
protected Integer getTicketNumber() {
return 11718;
}

@Override
protected String getTestDescription() {
return "ComboBox popup should fit completely in view, margin/border/padding included.";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.vaadin.tests.components.combobox;

import org.junit.Test;
import org.openqa.selenium.WebElement;

import com.vaadin.testbench.elements.ComboBoxElement;
import com.vaadin.tests.tb3.MultiBrowserTest;

public class ComboBoxAtRightEdgeTest extends MultiBrowserTest {

@Test
public void ensurePopupInView() {
openTestURL();

ComboBoxElement cb = $(ComboBoxElement.class).first();
cb.openPopup();
WebElement popup = cb.getSuggestionPopup();

int cbRight = cb.getLocation().getX() + cb.getSize().getWidth();
int popupRight = popup.getLocation().getX()
+ popup.getSize().getWidth();
assertGreaterOrEqual(String.format(
"Popup should not reach further right than the ComboBox at the "
+ "right edge of the viewport. ComboBox: %s, Popup: %s",
cbRight, popupRight), cbRight, popupRight);
}
}

0 comments on commit 0d19979

Please sign in to comment.