Skip to content

Commit 0053a64

Browse files
authored
fix: do not attempt to clear bad input when value is present (#4831)
1 parent ea98142 commit 0053a64

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

  • vaadin-date-picker-flow-parent
    • vaadin-date-picker-flow-integration-tests/src
    • vaadin-date-picker-flow/src/main/java/com/vaadin/flow/component/datepicker
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.vaadin.flow.component.datepicker;
22

3+
import java.time.LocalDate;
4+
35
import com.vaadin.flow.component.html.Div;
46
import com.vaadin.flow.component.html.NativeButton;
57
import com.vaadin.flow.router.Route;
68

79
@Route("vaadin-date-picker/clear-value")
810
public class ClearValuePage extends Div {
911
public static final String CLEAR_BUTTON = "clear-button";
12+
public static final String CLEAR_AND_SET_VALUE_BUTTON = "clear-and-set-value-button";
1013

1114
public ClearValuePage() {
1215
DatePicker datePicker = new DatePicker();
@@ -15,6 +18,13 @@ public ClearValuePage() {
1518
clearButton.setId(CLEAR_BUTTON);
1619
clearButton.addClickListener(event -> datePicker.clear());
1720

18-
add(datePicker, clearButton);
21+
NativeButton clearAndSetValueButton = new NativeButton(
22+
"Clear and set value", event -> {
23+
datePicker.clear();
24+
datePicker.setValue(LocalDate.of(2022, 1, 1));
25+
});
26+
clearAndSetValueButton.setId(CLEAR_AND_SET_VALUE_BUTTON);
27+
28+
add(datePicker, clearButton, clearAndSetValueButton);
1929
}
2030
}

‎vaadin-date-picker-flow-parent/vaadin-date-picker-flow-integration-tests/src/test/java/com/vaadin/flow/component/datepicker/ClearValueIT.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.vaadin.tests.AbstractComponentIT;
1111

1212
import static com.vaadin.flow.component.datepicker.ClearValuePage.CLEAR_BUTTON;
13+
import static com.vaadin.flow.component.datepicker.ClearValuePage.CLEAR_AND_SET_VALUE_BUTTON;
1314

1415
@TestPath("vaadin-date-picker/clear-value")
1516
public class ClearValueIT extends AbstractComponentIT {
@@ -38,4 +39,11 @@ public void setBadInputValue_clearValue_inputValueIsEmpty() {
3839
$("button").id(CLEAR_BUTTON).click();
3940
Assert.assertEquals("", datePicker.getInputValue());
4041
}
42+
43+
@Test
44+
public void badInput_setInputValue_clearAndSetValue_inputValueIsPresent() {
45+
datePicker.sendKeys("INVALID", Keys.ENTER);
46+
$("button").id(CLEAR_AND_SET_VALUE_BUTTON).click();
47+
Assert.assertEquals("1/1/2022", datePicker.getInputValue());
48+
}
4149
}

‎vaadin-date-picker-flow-parent/vaadin-date-picker-flow/src/main/java/com/vaadin/flow/component/datepicker/DatePicker.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,19 @@ public void setValue(LocalDate value) {
546546

547547
super.setValue(value);
548548

549+
// Clear the input element from possible bad input.
549550
if (Objects.equals(oldValue, getEmptyValue())
550551
&& Objects.equals(value, getEmptyValue())
551552
&& isInputValuePresent()) {
552-
// Clear the input element from possible bad input.
553-
getElement().executeJs("this.inputElement.value = ''");
553+
// The check for value presence guarantees that a non-empty value
554+
// won't get cleared when setValue(null) and setValue(...) are
555+
// subsequently called within one round-trip.
556+
// Flow only sends the final component value to the client
557+
// when you update the value multiple times during a round-trip
558+
// and the final value is sent in place of the first one, so
559+
// `executeJs` can end up invoked after a non-empty value is set.
560+
getElement()
561+
.executeJs("if (!this.value) this._inputElementValue = ''");
554562
getElement().setProperty("_hasInputValue", false);
555563
fireEvent(new ClientValidatedEvent(this, false));
556564
}

0 commit comments

Comments
 (0)