Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure DateTimePicker initializes client side value property #2884

Merged
merged 6 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}