Skip to content

Commit abc607f

Browse files
authored
fix: do not attempt to clear bad input when value is present (#4815)
1 parent fdf3ab4 commit abc607f

5 files changed

Lines changed: 50 additions & 8 deletions

File tree

‎vaadin-text-field-flow-parent/vaadin-text-field-flow-integration-tests/src/main/java/com/vaadin/flow/component/textfield/tests/IntegerFieldClearValuePage.java‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@
88
@Route("vaadin-integer-field/clear-value")
99
public class IntegerFieldClearValuePage extends Div {
1010
public static final String CLEAR_BUTTON = "clear-button";
11+
public static final String CLEAR_AND_SET_VALUE_BUTTON = "clear-and-set-value-button";
1112

1213
public IntegerFieldClearValuePage() {
1314
IntegerField integerField = new IntegerField();
1415

15-
NativeButton clearButton = new NativeButton("Clear value");
16+
NativeButton clearButton = new NativeButton("Clear value", event -> {
17+
integerField.clear();
18+
});
1619
clearButton.setId(CLEAR_BUTTON);
17-
clearButton.addClickListener(event -> integerField.clear());
1820

19-
add(integerField, clearButton);
21+
NativeButton clearAndSetValueButton = new NativeButton(
22+
"Clear and set value", event -> {
23+
integerField.clear();
24+
integerField.setValue(1234);
25+
});
26+
clearAndSetValueButton.setId(CLEAR_AND_SET_VALUE_BUTTON);
27+
28+
add(integerField, clearButton, clearAndSetValueButton);
2029
}
2130
}

‎vaadin-text-field-flow-parent/vaadin-text-field-flow-integration-tests/src/main/java/com/vaadin/flow/component/textfield/tests/NumberFieldClearValuePage.java‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@
88
@Route("vaadin-number-field/clear-value")
99
public class NumberFieldClearValuePage extends Div {
1010
public static final String CLEAR_BUTTON = "clear-button";
11+
public static final String CLEAR_AND_SET_VALUE_BUTTON = "clear-and-set-value-button";
1112

1213
public NumberFieldClearValuePage() {
1314
NumberField numberField = new NumberField();
1415

15-
NativeButton clearButton = new NativeButton("Clear value");
16+
NativeButton clearButton = new NativeButton("Clear value", event -> {
17+
numberField.clear();
18+
});
1619
clearButton.setId(CLEAR_BUTTON);
17-
clearButton.addClickListener(event -> numberField.clear());
1820

19-
add(numberField, clearButton);
21+
NativeButton clearAndSetValueButton = new NativeButton(
22+
"Clear and set value", event -> {
23+
numberField.clear();
24+
numberField.setValue(Double.valueOf(1234));
25+
});
26+
clearAndSetValueButton.setId(CLEAR_AND_SET_VALUE_BUTTON);
27+
28+
add(numberField, clearButton, clearAndSetValueButton);
2029
}
2130
}

‎vaadin-text-field-flow-parent/vaadin-text-field-flow-integration-tests/src/test/java/com/vaadin/flow/component/textfield/tests/IntegerFieldClearValueIT.java‎

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

1313
import static com.vaadin.flow.component.textfield.tests.IntegerFieldClearValuePage.CLEAR_BUTTON;
14+
import static com.vaadin.flow.component.textfield.tests.IntegerFieldClearValuePage.CLEAR_AND_SET_VALUE_BUTTON;
1415

1516
@TestPath("vaadin-integer-field/clear-value")
1617
public class IntegerFieldClearValueIT extends AbstractComponentIT {
@@ -50,4 +51,11 @@ public void badInput_setInputValue_clearValue_inputValueIsEmpty() {
5051
Assert.assertFalse((Boolean) executeScript(
5152
"return arguments[0].validity.badInput", input));
5253
}
54+
55+
@Test
56+
public void badInput_setInputValue_clearAndSetValue_inputValueIsPresent() {
57+
integerField.sendKeys("--2", Keys.ENTER);
58+
$("button").id(CLEAR_AND_SET_VALUE_BUTTON).click();
59+
Assert.assertEquals("1234", input.getPropertyString("value"));
60+
}
5361
}

‎vaadin-text-field-flow-parent/vaadin-text-field-flow-integration-tests/src/test/java/com/vaadin/flow/component/textfield/tests/NumberFieldClearValueIT.java‎

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

1313
import static com.vaadin.flow.component.textfield.tests.NumberFieldClearValuePage.CLEAR_BUTTON;
14+
import static com.vaadin.flow.component.textfield.tests.NumberFieldClearValuePage.CLEAR_AND_SET_VALUE_BUTTON;
1415

1516
@TestPath("vaadin-number-field/clear-value")
1617
public class NumberFieldClearValueIT extends AbstractComponentIT {
@@ -50,4 +51,11 @@ public void badInput_setInputValue_clearValue_inputValueIsEmpty() {
5051
Assert.assertFalse((Boolean) executeScript(
5152
"return arguments[0].validity.badInput", input));
5253
}
54+
55+
@Test
56+
public void badInput_setInputValue_clearAndSetValue_inputValueIsPresent() {
57+
numberField.sendKeys("--2", Keys.ENTER);
58+
$("button").id(CLEAR_AND_SET_VALUE_BUTTON).click();
59+
Assert.assertEquals("1234", input.getPropertyString("value"));
60+
}
5361
}

‎vaadin-text-field-flow-parent/vaadin-text-field-flow/src/main/java/com/vaadin/flow/component/textfield/AbstractNumberField.java‎

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

139139
super.setValue(value);
140140

141+
// Clear the input element from possible bad input.
141142
if (Objects.equals(oldValue, getEmptyValue())
142143
&& Objects.equals(value, getEmptyValue())
143144
&& isInputValuePresent()) {
144-
// Clear the input element from possible bad input.
145-
getElement().executeJs("this.inputElement.value = ''");
145+
// The check for value presence guarantees that a non-empty value
146+
// won't get cleared when setValue(null) and setValue(...) are
147+
// subsequently called within one round-trip.
148+
// Flow only sends the final component value to the client
149+
// when you update the value multiple times during a round-trip
150+
// and the final value is sent in place of the first one, so
151+
// `executeJs` can end up invoked after a non-empty value is set.
152+
getElement()
153+
.executeJs("if (!this.value) this._inputElementValue = ''");
146154
getElement().setProperty("_hasInputValue", false);
147155
fireEvent(new ClientValidatedEvent(this, false));
148156
}

0 commit comments

Comments
 (0)