Skip to content

Commit 985f43f

Browse files
authored
fix: restore input value when setting a non-empty value (#4834)
1 parent 53e5ecd commit 985f43f

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.vaadin.flow.component.textfield.tests;
22

3+
import java.math.BigDecimal;
4+
35
import com.vaadin.flow.component.html.Div;
46
import com.vaadin.flow.component.html.NativeButton;
57
import com.vaadin.flow.router.Route;
@@ -8,6 +10,7 @@
810
@Route("vaadin-big-decimal-field/clear-value")
911
public class BigDecimalFieldClearValuePage extends Div {
1012
public static final String CLEAR_BUTTON = "clear-button";
13+
public static final String CLEAR_AND_SET_VALUE_BUTTON = "clear-and-set-value-button";
1114

1215
public BigDecimalFieldClearValuePage() {
1316
BigDecimalField bigDecimalField = new BigDecimalField();
@@ -16,6 +19,13 @@ public BigDecimalFieldClearValuePage() {
1619
clearButton.setId(CLEAR_BUTTON);
1720
clearButton.addClickListener(event -> bigDecimalField.clear());
1821

19-
add(bigDecimalField, clearButton);
22+
NativeButton clearAndSetValueButton = new NativeButton(
23+
"Clear and set value", event -> {
24+
bigDecimalField.clear();
25+
bigDecimalField.setValue(BigDecimal.valueOf(12.34));
26+
});
27+
clearAndSetValueButton.setId(CLEAR_AND_SET_VALUE_BUTTON);
28+
29+
add(bigDecimalField, clearButton, clearAndSetValueButton);
2030
}
2131
}

‎vaadin-text-field-flow-parent/vaadin-text-field-flow-integration-tests/src/test/java/com/vaadin/flow/component/textfield/tests/BigDecimalFieldClearValueIT.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.BigDecimalFieldClearValuePage.CLEAR_BUTTON;
14+
import static com.vaadin.flow.component.textfield.tests.BigDecimalFieldClearValuePage.CLEAR_AND_SET_VALUE_BUTTON;
1415

1516
@TestPath("vaadin-big-decimal-field/clear-value")
1617
public class BigDecimalFieldClearValueIT extends AbstractComponentIT {
@@ -42,4 +43,11 @@ public void badInput_setInputValue_clearValue_inputValueIsEmpty() {
4243
$("button").id(CLEAR_BUTTON).click();
4344
Assert.assertEquals("", input.getPropertyString("value"));
4445
}
46+
47+
@Test
48+
public void badInput_setInputValue_clearAndSetValue_inputValueIsPresent() {
49+
bigDecimalField.sendKeys("--2", Keys.ENTER);
50+
$("button").id(CLEAR_AND_SET_VALUE_BUTTON).click();
51+
Assert.assertEquals("12.34", input.getPropertyString("value"));
52+
}
4553
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,15 @@ public void setValue(BigDecimal value) {
224224
&& Objects.equals(value, getEmptyValue())
225225
&& isInputValuePresent()) {
226226
// Clear the input element from possible bad input.
227-
getElement().executeJs("this.inputElement.value = ''");
227+
getElement().executeJs("this._inputElementValue = ''");
228228
getElement().setProperty("_hasInputValue", false);
229229
fireEvent(new ClientValidatedEvent(this, false));
230+
} else {
231+
// Restore the input element's value in case it was cleared
232+
// in the above branch. That can happen when setValue(null)
233+
// and setValue(...) are subsequently called within one round-trip
234+
// and there was bad input.
235+
getElement().executeJs("this._inputElementValue = this.value");
230236
}
231237
}
232238

0 commit comments

Comments
 (0)