Skip to content

Commit

Permalink
fix: ensure DatePicker initializes client side value property (#2878) (
Browse files Browse the repository at this point in the history
…#2880)

Co-authored-by: Sascha Ißbrücker <[email protected]>
Co-authored-by: Sergey Vinogradov <[email protected]>
  • Loading branch information
3 people authored Mar 18, 2022
1 parent 6c28ac0 commit 7e8e4f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1197,8 +1197,13 @@ public <P> GeneratedVaadinDatePicker(T initialValue, T defaultValue,
boolean isInitialValueOptional) {
super("value", defaultValue, elementPropertyType, presentationToModel,
modelToPresentation);
// Only apply initial value if the element does not already have a value
// (this can be the case when binding to an existing element from a Lit
// template), or if isInitialValueOptional enforces setting the initial
// value, which is the case when calling a DatePicker constructor with a
// custom initial value.
if ((getElement().getProperty("value") == null
|| !isInitialValueOptional) && initialValue != null) {
|| !isInitialValueOptional)) {
setPresentationValue(initialValue);
}
}
Expand All @@ -1217,9 +1222,7 @@ public <P> GeneratedVaadinDatePicker(T initialValue, T defaultValue,
public GeneratedVaadinDatePicker(T initialValue, T defaultValue,
boolean acceptNullValues) {
super("value", defaultValue, acceptNullValues);
if (initialValue != null) {
setPresentationValue(initialValue);
}
setPresentationValue(initialValue);
}

/**
Expand Down Expand Up @@ -1247,9 +1250,7 @@ public <P> GeneratedVaadinDatePicker(T initialValue, T defaultValue,
SerializableBiFunction<R, T, P> modelToPresentation) {
super("value", defaultValue, elementPropertyType, presentationToModel,
modelToPresentation);
if (initialValue != null) {
setPresentationValue(initialValue);
}
setPresentationValue(initialValue);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ public class DatePickerTest {

private static final String OPENED_PROPERTY_NOT_UPDATED = "The server-side \"opened\"-property was not updated synchronously";

private static final LocalDate TEST_VALUE = LocalDate.now();

private static class TestDatePicker
extends GeneratedVaadinDatePicker<TestDatePicker, LocalDate> {
@Test
public void initialValueIsNotSpecified_valuePropertyHasEmptyString() {
DatePicker picker = new DatePicker();
Assert.assertNull(picker.getValue());
Assert.assertEquals("", picker.getElement().getProperty("value"));
}

TestDatePicker() {
super(TEST_VALUE, null, String.class, value -> null, value -> null,
true);
}
@Test
public void initialValueIsNull_valuePropertyHasEmptyString() {
DatePicker picker = new DatePicker((LocalDate) null);
Assert.assertNull(picker.getValue());
Assert.assertEquals("", picker.getElement().getProperty("value"));
}

@Test
public void datePicker_basicCases() {
DatePicker picker = new DatePicker();

Assert.assertNull(picker.getValue());
Assert.assertFalse(picker.getElement().hasProperty("value"));

picker.setValue(LocalDate.of(2018, 4, 25));
Assert.assertEquals("2018-04-25",
picker.getElement().getProperty("value"));
Expand All @@ -69,13 +69,6 @@ public void datePicker_basicCases() {
Assert.assertNull(picker.getValue());
}

@Test
public void defaultCtor_does_not_update_values() {
DatePicker picker = new DatePicker();
Assert.assertNull(picker.getValue());
Assert.assertNull(picker.getElement().getProperty("value"));
}

@Test
public void setInitialValue() {
DatePicker picker = new DatePicker(LocalDate.of(2018, 4, 25));
Expand Down Expand Up @@ -142,12 +135,12 @@ public void elementHasValue_wrapIntoField_propertyIsNotSetToInitialValue() {

Mockito.when(service.getInstantiator()).thenReturn(instantiator);

Mockito.when(instantiator.createComponent(TestDatePicker.class))
.thenAnswer(invocation -> new TestDatePicker());
Mockito.when(instantiator.createComponent(DatePicker.class))
.thenAnswer(invocation -> new DatePicker());

TestDatePicker field = Component.from(element, TestDatePicker.class);
DatePicker field = Component.from(element, DatePicker.class);
Assert.assertEquals("2007-12-03",
field.getElement().getPropertyRaw("value"));
field.getElement().getProperty("value"));
}

public void assertClearButtonPropertyValueEquals(DatePicker picker,
Expand Down

0 comments on commit 7e8e4f2

Please sign in to comment.