Fixes `DateField blur event fires when focus gets to the "calendar button"` #1008
renderCalendar();
}
}
-
}
/**
* Opens the calendar panel popup.
*/
public void openCalendarPanel() {
-
if (!open && !readonly && isEnabled()) {
open = true;
popup.setWidth("");
popup.setHeight("");
popup.setPopupPositionAndShow(new PopupPositionCallback());
+
+ checkGroupFocus(true);
} else {
getLogger().severe("Cannot reopen popup, it is already open!");
}
* Sets focus to Calendar panel.
*
* @param focus
+ * {@code true} for {@code focus}, {@code false} for {@code blur}
*/
public void setFocus(boolean focus) {
calendar.setFocus(focus);
// are in the lower right corner of the screen
if (overflow) {
return left;
- } else {
- return left + calendarToggle.getOffsetWidth();
- }
- } else {
- int[] margins = style.getMargin();
- int desiredLeftPosition = calendarToggle.getAbsoluteLeft()
- - width - margins[1] - margins[3];
- if (desiredLeftPosition >= 0) {
- return desiredLeftPosition;
- } else {
- return left;
}
+ return left + calendarToggle.getOffsetWidth();
}
+ int[] margins = style.getMargin();
+ int desiredLeftPosition = calendarToggle.getAbsoluteLeft() - width
+ - margins[1] - margins[3];
+ if (desiredLeftPosition >= 0) {
+ return desiredLeftPosition;
+ }
+ return left;
}
private boolean positionRightSide() {
}
}
+ @Override
+ protected boolean hasChildFocus() {
+ return open;
+ }
+
private static Logger getLogger() {
return Logger.getLogger(VAbstractPopupCalendar.class.getName());
}
import java.util.logging.Logger;
import com.google.gwt.aria.client.Roles;
+import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
/** For internal use only. May be removed or replaced in the future. */
private TimeZone timeZone;
+ /**
+ * Specifies whether the group of components has focus or not.
+ */
+ private boolean groupFocus;
+
public VAbstractTextualDate(R resoluton) {
super(resoluton);
text = new TextBox();
text.addChangeHandler(this);
text.addFocusHandler(
- event -> fireBlurFocusEvent(event, true, EventId.FOCUS));
+ event -> fireBlurFocusEvent(event, true));
text.addBlurHandler(
- event -> fireBlurFocusEvent(event, false, EventId.BLUR));
+ event -> fireBlurFocusEvent(event, false));
if (BrowserInfo.get().isIE()) {
addDomHandler(this, KeyDownEvent.getType());
}
}
private void fireBlurFocusEvent(DomEvent<?> event,
- boolean addFocusStyleName, String eventId) {
+ boolean focus) {
String styleName = VTextField.CLASSNAME + "-"
+ VTextField.CLASSNAME_FOCUS;
- if (addFocusStyleName) {
+ if (focus) {
text.addStyleName(styleName);
} else {
text.removeStyleName(styleName);
}
- if (getClient() != null && connector.hasEventListener(eventId)) {
- // may excessively send events if if focus went to another
- // sub-component
- if (EventId.FOCUS.equals(eventId)) {
+
+ Scheduler.get().scheduleDeferred(() -> checkGroupFocus(focus));
+
+ // Needed for tooltip event handling
+ fireEvent(event);
+ }
+
+ /**
+ * Checks if the group focus has changed, and sends to the server if needed.
+ *
+ * @param textFocus
+ * the focus of the {@link #text}
+ * @since
+ */
+ protected void checkGroupFocus(boolean textFocus) {
+ boolean newGroupFocus = textFocus | hasChildFocus();
+ if (getClient() != null
+ && connector.hasEventListener(
+ textFocus ? EventId.FOCUS : EventId.BLUR)
+ && groupFocus != newGroupFocus) {
+
+ if (newGroupFocus) {
rpc.focus();
} else {
rpc.blur();
}
sendBufferedValues();
+ groupFocus = newGroupFocus;
}
+ }
- // Needed for tooltip event handling
- fireEvent(event);
+ /**
+ * Returns whether any of the child components has focus.
+ *
+ * @return {@code true} if any of the child component has focus,
+ * {@code false} otherwise
+ * @since
+ */
+ protected boolean hasChildFocus() {
+ return false;
}
/**
private static Logger getLogger() {
return Logger.getLogger(VAbstractTextualDate.class.getName());
}
+
}
AbstractDateField<?, ?> df = new TestDateField("DateField");
l.addComponent(df);
- ComboBox cb = new ComboBox("ComboBox");
+ ComboBox<String> cb = new ComboBox<>("ComboBox");
l.addComponent(cb);
Button btn = new Button("Button");
ogm.addBlurListener(blurListener);
l.addComponent(messages);
-
}
private OptionGroup createOptionGroup(String caption) {
--- /dev/null
+/*
+ * Copyright 2000-2016 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.tests.components.datefield;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUIWithLog;
+import com.vaadin.ui.DateField;
+import com.vaadin.ui.TextField;
+
+public class DateFieldFocus extends AbstractTestUIWithLog {
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ DateField dateField = new DateField();
+ dateField.addFocusListener(e -> log("focused"));
+ dateField.addBlurListener(e -> log("blurred"));
+ addComponent(dateField);
+
+ TextField textField = new TextField();
+ textField.setCaption("second");
+ addComponent(textField);
+ }
+
+ @Override
+ protected String getTestDescription() {
+ return "DateField should not trigger events when nagivating between sub-components.";
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 1008;
+ }
+
+}
--- /dev/null
+package com.vaadin.tests.components.datefield;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import com.vaadin.testbench.elements.DateFieldElement;
+import com.vaadin.testbench.elements.TextFieldElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class DateFieldFocusTest extends MultiBrowserTest {
+
+ @Test
+ public void focus() {
+ openTestURL();
+
+ assertEquals(" ", getLogRow(0));
+ DateFieldElement dateField = $(DateFieldElement.class).first();
+ TextFieldElement textField = $(TextFieldElement.class).caption("second")
+ .first();
+
+ dateField.openPopup();
+ dateField.openPopup();
+
+ dateField.openPopup();
+ dateField.openPopup();
+
+ assertEquals("1. focused", getLogRow(0));
+
+ textField.focus();
+
+ waitUntil(input -> "2. blurred".equals(getLogRow(0)));
+ }
+}