Skip to content

Commit

Permalink
fix: ensure DateTimePicker initializes client side value property (#2884
Browse files Browse the repository at this point in the history
)

* fix: ensure DateTimePicker initializes client side value property

* chore: apply code review suggestions

* chore: run formatter

Co-authored-by: Sascha Ißbrücker <[email protected]>
  • Loading branch information
2 people authored and vaadin-bot committed Mar 18, 2022
1 parent 9588671 commit 5942430
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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"));
}
}

0 comments on commit 5942430

Please sign in to comment.