Skip to content

Commit d7943ac

Browse files
authored
feat: add server-side fallback parser (#6743)
1 parent 0f85176 commit d7943ac

File tree

7 files changed

+510
-21
lines changed

7 files changed

+510
-21
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2000-2024 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.datepicker;
17+
18+
import java.time.LocalDate;
19+
20+
import com.vaadin.flow.component.html.Div;
21+
import com.vaadin.flow.component.html.NativeButton;
22+
import com.vaadin.flow.data.binder.Result;
23+
import com.vaadin.flow.router.Route;
24+
25+
import elemental.json.Json;
26+
import elemental.json.JsonObject;
27+
28+
@Route("vaadin-date-picker/fallback-parser")
29+
public class DatePickerFallbackParserPage extends Div {
30+
public DatePickerFallbackParserPage() {
31+
DatePicker datePicker = new DatePicker();
32+
datePicker.setFallbackParser((s) -> {
33+
if (s.equals("newyear")) {
34+
return Result.ok(LocalDate.of(2024, 1, 1));
35+
} else if (s.equals("exception")) {
36+
throw new RuntimeException("Exception in fallback parser");
37+
} else {
38+
return Result.error("Invalid date format");
39+
}
40+
});
41+
42+
Div valueChangeLog = new Div();
43+
valueChangeLog.setId("value-change-log");
44+
45+
datePicker.addValueChangeListener(event -> {
46+
JsonObject record = Json.createObject();
47+
record.put("eventFromClient", event.isFromClient());
48+
record.put("eventOldValue", formatDate(event.getOldValue()));
49+
record.put("eventNewValue", formatDate(event.getValue()));
50+
record.put("componentValue", formatDate(datePicker.getValue()));
51+
record.put("componentValueProperty",
52+
datePicker.getElement().getProperty("value", ""));
53+
54+
valueChangeLog.add(new Div(record.toString()));
55+
});
56+
57+
NativeButton clearValueChangeLog = new NativeButton(
58+
"Clear value change log", event -> {
59+
valueChangeLog.removeAll();
60+
});
61+
clearValueChangeLog.setId("clear-value-change-log");
62+
63+
NativeButton setValue = new NativeButton("Set value", event -> {
64+
datePicker.setValue(LocalDate.now());
65+
});
66+
67+
add(datePicker, valueChangeLog, clearValueChangeLog, setValue);
68+
}
69+
70+
private String formatDate(LocalDate date) {
71+
return date == null ? "" : date.toString();
72+
}
73+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2000-2024 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.datepicker;
17+
18+
import java.util.List;
19+
20+
import org.junit.Assert;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import com.vaadin.flow.component.datepicker.testbench.DatePickerElement;
25+
import com.vaadin.flow.testutil.TestPath;
26+
import com.vaadin.testbench.TestBenchElement;
27+
import com.vaadin.tests.AbstractComponentIT;
28+
29+
import elemental.json.Json;
30+
import elemental.json.JsonObject;
31+
32+
@TestPath("vaadin-date-picker/fallback-parser")
33+
public class DatePickerFallbackParserIT extends AbstractComponentIT {
34+
private DatePickerElement datePicker;
35+
private TestBenchElement valueChangeLog;
36+
37+
@Before
38+
public void init() {
39+
open();
40+
datePicker = $(DatePickerElement.class).first();
41+
valueChangeLog = $("div").id("value-change-log");
42+
}
43+
44+
@Test
45+
public void enterShortcutValue_clearShortcutValue() {
46+
datePicker.setInputValue("newyear");
47+
assertValueChange("", "2024-01-01");
48+
assertInputValue("1/1/2024");
49+
50+
datePicker.setInputValue("newyear");
51+
assertNoValueChange();
52+
assertInputValue("1/1/2024");
53+
54+
datePicker.setInputValue("");
55+
assertValueChange("2024-01-01", "");
56+
assertInputValue("");
57+
}
58+
59+
@Test
60+
public void enterUnparsableValue_enterShortcutValue() {
61+
datePicker.setInputValue("foo");
62+
assertNoValueChange();
63+
assertInputValue("foo");
64+
65+
datePicker.setInputValue("newyear");
66+
assertValueChange("", "2024-01-01");
67+
assertInputValue("1/1/2024");
68+
}
69+
70+
@Test
71+
public void enterParsableValue_enterShortcutValue() {
72+
datePicker.setInputValue("2/2/2000");
73+
assertValueChange("", "2000-02-02");
74+
assertInputValue("2/2/2000");
75+
76+
datePicker.setInputValue("newyear");
77+
assertValueChange("2000-02-02", "2024-01-01");
78+
assertInputValue("1/1/2024");
79+
}
80+
81+
@Test
82+
public void enterShortcutValueThrowingException_enterParsableValue() {
83+
datePicker.setInputValue("exception");
84+
assertNoValueChange();
85+
assertInputValue("exception");
86+
87+
datePicker.setInputValue("2/2/2000");
88+
assertValueChange("", "2000-02-02");
89+
assertInputValue("2/2/2000");
90+
}
91+
92+
private void assertValueChange(String expectedOldValue,
93+
String expectedNewValue) {
94+
List<TestBenchElement> records = valueChangeLog.$("div").all();
95+
Assert.assertEquals("ValueChangeEvent should be fired only once", 1,
96+
records.size());
97+
98+
JsonObject record = Json.parse(records.get(0).getText());
99+
100+
Assert.assertTrue("eventFromClient should be true",
101+
record.getBoolean("eventFromClient"));
102+
Assert.assertEquals("eventOldValue should contain old value",
103+
expectedOldValue, record.getString("eventOldValue"));
104+
Assert.assertEquals("eventNewValue should contain new value",
105+
expectedNewValue, record.getString("eventNewValue"));
106+
Assert.assertEquals("componentValue should contain new value",
107+
expectedNewValue, record.getString("componentValue"));
108+
Assert.assertEquals("componentValueProperty should contain new value",
109+
expectedNewValue, record.getString("componentValueProperty"));
110+
111+
$("button").id("clear-value-change-log").click();
112+
}
113+
114+
private void assertNoValueChange() {
115+
Assert.assertEquals("", valueChangeLog.getText());
116+
}
117+
118+
private void assertInputValue(String expected) {
119+
Assert.assertEquals(expected, datePicker.getInputValue());
120+
}
121+
}

0 commit comments

Comments
 (0)