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 DatePicker initializes client side value property #2878

Merged
merged 4 commits into from
Mar 17, 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 @@ -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,
vursen marked this conversation as resolved.
Show resolved Hide resolved
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