diff --git a/vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow/src/main/java/com/vaadin/flow/component/datetimepicker/DateTimePicker.java b/vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow/src/main/java/com/vaadin/flow/component/datetimepicker/DateTimePicker.java index e8be9cd1047..b996fab68a1 100644 --- a/vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow/src/main/java/com/vaadin/flow/component/datetimepicker/DateTimePicker.java +++ b/vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow/src/main/java/com/vaadin/flow/component/datetimepicker/DateTimePicker.java @@ -143,6 +143,16 @@ public DateTimePicker(LocalDateTime initialDateTime) { initialDateTime = sanitizeValue(initialDateTime); setPresentationValue(initialDateTime); synchronizeChildComponentValues(initialDateTime); + } else if (this.getElement().getProperty("value") == null) { + // Apply `null` as a value to force the client side `value` property + // to be initialized with an empty string. Having an empty string + // will prevent `ValueChangeEvent` which otherwise can be triggered + // in response to Polymer converting `null` to an empty string by + // itself. + // Only apply `null` if the element does not already have a value, + // which can be the case when binding to an existing element from a + // Lit template. + setPresentationValue(null); } addToSlot(datePicker, "date-picker"); diff --git a/vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow/src/test/java/com/vaadin/flow/component/datetimepicker/DateTimePickerTest.java b/vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow/src/test/java/com/vaadin/flow/component/datetimepicker/DateTimePickerTest.java index 5515259fb92..9400974f1a2 100644 --- a/vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow/src/test/java/com/vaadin/flow/component/datetimepicker/DateTimePickerTest.java +++ b/vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow/src/test/java/com/vaadin/flow/component/datetimepicker/DateTimePickerTest.java @@ -16,6 +16,10 @@ package com.vaadin.flow.component.datetimepicker; import com.vaadin.flow.component.datepicker.DatePicker; +import com.vaadin.flow.di.Instantiator; +import com.vaadin.flow.dom.Element; +import com.vaadin.flow.server.VaadinService; +import com.vaadin.flow.server.VaadinSession; import java.time.Duration; import java.time.LocalDateTime; @@ -25,9 +29,12 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; +import com.vaadin.flow.component.Component; import com.vaadin.flow.component.UI; +import org.junit.Assert; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -48,6 +55,30 @@ public void tearDown() { UI.setCurrent(null); } + @Test + public void initialValueIsNotSpecified_valuePropertyHasEmptyString() { + DateTimePicker picker = new DateTimePicker(); + Assert.assertNull(picker.getValue()); + Assert.assertEquals("", picker.getElement().getProperty("value")); + } + + @Test + public void initialValueIsNull_valuePropertyHasEmptyString() { + DateTimePicker picker = new DateTimePicker((LocalDateTime) null); + Assert.assertNull(picker.getValue()); + Assert.assertEquals("", picker.getElement().getProperty("value")); + } + + @Test + public void initialValueIsDateTime_valuePropertyHasInitialValue() { + DateTimePicker picker = new DateTimePicker( + LocalDateTime.of(2018, 4, 25, 13, 45, 10)); + Assert.assertEquals(LocalDateTime.of(2018, 4, 25, 13, 45, 10), + picker.getValue()); + Assert.assertEquals("2018-04-25T13:45:10", + picker.getElement().getProperty("value")); + } + @Test public void emptyField() { DateTimePicker picker = new DateTimePicker(); @@ -154,4 +185,28 @@ public void setAutoOpen() { assertTrue("Should be possible to enable auto-open", picker.isAutoOpen()); } + + @Test + public void elementHasValue_wrapIntoField_propertyIsNotSetToInitialValue() { + Element element = new Element("vaadin-date-time-picker"); + + String value = LocalDateTime.now().toString(); + element.setProperty("value", value); + UI ui = new UI(); + UI.setCurrent(ui); + VaadinSession session = Mockito.mock(VaadinSession.class); + ui.getInternals().setSession(session); + VaadinService service = Mockito.mock(VaadinService.class); + Mockito.when(session.getService()).thenReturn(service); + + Instantiator instantiator = Mockito.mock(Instantiator.class); + + Mockito.when(service.getInstantiator()).thenReturn(instantiator); + + Mockito.when(instantiator.createComponent(DateTimePicker.class)) + .thenAnswer(invocation -> new DateTimePicker()); + + DateTimePicker field = Component.from(element, DateTimePicker.class); + Assert.assertEquals(value, field.getElement().getProperty("value")); + } }