Skip to content

Commit

Permalink
simplify change
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen committed Nov 21, 2024
1 parent 5130a6d commit b6132ac
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ public void setValue(LocalDate value) {
boolean isOldValueEmpty = valueEquals(oldValue, getEmptyValue());
boolean isNewValueEmpty = valueEquals(value, getEmptyValue());
boolean isValueRemainedEmpty = isOldValueEmpty && isNewValueEmpty;
boolean isInputValuePresent = isInputValuePresent();
String oldInputElementValue = getInputElementValue();

// When the value is cleared programmatically, there is no change event
// that would synchronize _inputElementValue, so we reset it ourselves
Expand All @@ -772,7 +772,7 @@ public void setValue(LocalDate value) {

// Revalidate if setValue(null) didn't result in a value change but
// cleared bad input
if (isValueRemainedEmpty && isInputValuePresent) {
if (isValueRemainedEmpty && !oldInputElementValue.isEmpty()) {
validate();
fireValidationStatusChangeEvent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormatSymbols;
import java.time.LocalDate;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -166,13 +165,6 @@ public BigDecimalField() {
addValueChangeListener(e -> validate());
}

@Override
public void setValueChangeMode(ValueChangeMode valueChangeMode) {
super.setValueChangeMode(valueChangeMode);
getSynchronizationRegistration()
.synchronizeProperty("_inputElementValue");
}

/**
* Constructs an empty {@code BigDecimalField} with the given label.
*
Expand Down Expand Up @@ -318,20 +310,14 @@ public void setValue(BigDecimal value) {
boolean isOldValueEmpty = valueEquals(oldValue, getEmptyValue());
boolean isNewValueEmpty = valueEquals(value, getEmptyValue());
boolean isValueRemainedEmpty = isOldValueEmpty && isNewValueEmpty;
boolean isInputElementValueEmpty = getInputElementValue().isEmpty();

// When the value is cleared programmatically, there is no change event
// that would synchronize _inputElementValue, so we reset it ourselves
// to prevent the following validation from treating this as bad input.
if (isNewValueEmpty) {
setInputElementValue("");
}
String oldInputElementValue = getInputElementValue();

super.setValue(value);

// Revalidate if setValue(null) didn't result in a value change but
// cleared bad input
if (isValueRemainedEmpty && !isInputElementValueEmpty) {
// Revalidate and clear input element value if setValue(null) didn't
// result in a value change but there was bad input.
if (isValueRemainedEmpty && !oldInputElementValue.isEmpty()) {
setInputElementValue("");
validate();
fireValidationStatusChangeEvent();
}
Expand Down Expand Up @@ -404,27 +390,12 @@ private void fireValidationStatusChangeEvent() {
.forEach(listener -> listener.validationStatusChanged(event));
}

/**
* Gets the value of the input element. This value is updated on the server
* when the web component dispatches a `change` event. Except when clearing
* the value, {@link #setValue(LocalDate)} does not update the input element
* value on the server because it requires date formatting, which is
* implemented on the web component's side.
*
* @return the value of the input element
*/
private String getInputElementValue() {
return getElement().getProperty("_inputElementValue", "");
return getElement().getProperty("value", "");
}

/**
* Sets the value of the input element.
*
* @param value
* the value to set
*/
private void setInputElementValue(String value) {
getElement().setProperty("_inputElementValue", value);
getElement().setProperty("value", value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ public void addValidationStatusChangeListener_addAnotherListenerOnInvocation_noE
});

// Trigger ValidationStatusChangeEvent
fakeClientPropertyChange(testField, "_inputElementValue", "foo");
fakeClientPropertyChange(testField, "value", "foo");
testField.clear();
}

@Test
public void badInput_validate_emptyErrorMessageDisplayed() {
fakeClientPropertyChange(testField, "_inputElementValue", "foo");
fakeClientPropertyChange(testField, "value", "foo");
Assert.assertEquals("", testField.getErrorMessage());
}
Expand All @@ -51,7 +49,6 @@ public void badInput_validate_emptyErrorMessageDisplayed() {
public void badInput_setI18nErrorMessage_validate_i18nErrorMessageDisplayed() {
testField.setI18n(new BigDecimalField.BigDecimalFieldI18n()
.setBadInputErrorMessage("Value has invalid format"));
fakeClientPropertyChange(testField, "_inputElementValue", "foo");
fakeClientPropertyChange(testField, "value", "foo");
Assert.assertEquals("Value has invalid format",
testField.getErrorMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.io.Serializable;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
Expand Down Expand Up @@ -367,7 +366,7 @@ public void setValue(LocalTime value) {
boolean isOldValueEmpty = valueEquals(oldValue, getEmptyValue());
boolean isNewValueEmpty = valueEquals(value, getEmptyValue());
boolean isValueRemainedEmpty = isOldValueEmpty && isNewValueEmpty;
boolean isInputValuePresent = isInputValuePresent();
String oldInputElementValue = getInputElementValue();

// When the value is cleared programmatically, there is no change event
// that would synchronize _inputElementValue, so we reset it ourselves
Expand All @@ -380,7 +379,7 @@ public void setValue(LocalTime value) {

// Revalidate if setValue(null) didn't result in a value change but
// cleared bad input
if (isValueRemainedEmpty && isInputValuePresent) {
if (isValueRemainedEmpty && !oldInputElementValue.isEmpty()) {
validate();
fireValidationStatusChangeEvent();
}
Expand Down Expand Up @@ -454,7 +453,7 @@ protected boolean isInputValuePresent() {
/**
* Gets the value of the input element. This value is updated on the server
* when the web component dispatches a `change` or `unparsable-change`
* event. Except when clearing the value, {@link #setValue(LocalDate)} does
* event. Except when clearing the value, {@link #setValue(LocalTime)} does
* not update the input element value on the server because it requires date
* formatting, which is implemented on the web component's side.
*
Expand Down

0 comments on commit b6132ac

Please sign in to comment.