diff --git a/vaadin-date-picker-flow-parent/vaadin-date-picker-flow/src/main/java/com/vaadin/flow/component/datepicker/GeneratedVaadinDatePicker.java b/vaadin-date-picker-flow-parent/vaadin-date-picker-flow/src/main/java/com/vaadin/flow/component/datepicker/GeneratedVaadinDatePicker.java index be8a8d0b61c..061f714b847 100644 --- a/vaadin-date-picker-flow-parent/vaadin-date-picker-flow/src/main/java/com/vaadin/flow/component/datepicker/GeneratedVaadinDatePicker.java +++ b/vaadin-date-picker-flow-parent/vaadin-date-picker-flow/src/main/java/com/vaadin/flow/component/datepicker/GeneratedVaadinDatePicker.java @@ -1197,8 +1197,13 @@ public

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); } } @@ -1217,9 +1222,7 @@ public

GeneratedVaadinDatePicker(T initialValue, T defaultValue, public GeneratedVaadinDatePicker(T initialValue, T defaultValue, boolean acceptNullValues) { super("value", defaultValue, acceptNullValues); - if (initialValue != null) { - setPresentationValue(initialValue); - } + setPresentationValue(initialValue); } /** @@ -1247,9 +1250,7 @@ public

GeneratedVaadinDatePicker(T initialValue, T defaultValue, SerializableBiFunction modelToPresentation) { super("value", defaultValue, elementPropertyType, presentationToModel, modelToPresentation); - if (initialValue != null) { - setPresentationValue(initialValue); - } + setPresentationValue(initialValue); } /** diff --git a/vaadin-date-picker-flow-parent/vaadin-date-picker-flow/src/test/java/com/vaadin/flow/component/datepicker/DatePickerTest.java b/vaadin-date-picker-flow-parent/vaadin-date-picker-flow/src/test/java/com/vaadin/flow/component/datepicker/DatePickerTest.java index 55847081ad8..b6fe559d093 100644 --- a/vaadin-date-picker-flow-parent/vaadin-date-picker-flow/src/test/java/com/vaadin/flow/component/datepicker/DatePickerTest.java +++ b/vaadin-date-picker-flow-parent/vaadin-date-picker-flow/src/test/java/com/vaadin/flow/component/datepicker/DatePickerTest.java @@ -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 { + @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")); @@ -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)); @@ -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,