Skip to content

Commit 43e6b14

Browse files
web-padawanclaude
andauthored
feat: add date metadata support to DateTimePicker (#9885)
## Description Date Picker can disable dates through a fixed list, disabled weekdays and a date metadata provider. Date Time Picker, which contains a date picker, could not. ```java dateTimePicker.setDisabledWeekdays(List.of(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY)); dateTimePicker.setDateMetadataProvider(DateMetadataProvider.perDate( date -> isFullyBooked(date) ? new DateMetadata(date, true) : null)); ``` - `setDisabledDates`, `setDisabledWeekdays` and `setDateMetadataProvider`, with their getters, delegate to the inner date picker, which drives the calendar overlay on its own. - `refreshDateMetadata()` delegates the cache drop, then runs this component's own validation and fires the validation status change event, so a Binder-bound field re-validates. - Validation does not delegate. The inner date picker's `validate()` deliberately never changes the invalid state, so a disabled date would render as disabled in the calendar while validating as valid. Date Time Picker checks the disabled date in its own validator instead — both when only a date has been entered and when there is a full value, after the min/max checks in each case, so an out-of-range date still reports the min/max message. - `DateTimePickerI18n.setDisabledDateErrorMessage(...)` provides the error message. - The inner date picker subclass reaches the disabled-date check through a small bridge, the same way it already reaches `isInputUnparsable`. - No client-side changes: the inner date picker is a full `<vaadin-date-picker>` with its own connector, so the configuration push and the metadata requests already work through it. - Integration tests mirror the ones #9878 added for Date Picker. The Test Bench day cell API needed no changes, since `DateTimePickerElement.getDatePicker()` already returns a `DatePickerElement`. Part of vaadin/platform#2867 ## Type of change - [ ] Bugfix - [x] Feature ## How to test ``` mvn verify -am -pl vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow-integration-tests \ -Dit.test='DateTimePickerDateMetadataIT,BasicValidationIT' -DskipUnitTests ``` By hand, open `vaadin-date-time-picker/date-metadata` (view `DateTimePickerDateMetadataPage`). In January 2023 the calendar shows the 10th and the 20th, both weekends, and the 17th from the provider as disabled, and the 25th with a `busy` part name. "Refresh date metadata" also disables the 18th without the provider being set again. On `vaadin-date-time-picker/validation/basic`, set a disabled date, then type it into the date field: the field goes invalid with the message even before a time is picked. Setting a min date past the typed date reports the min message instead. --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 4f185a0 commit 43e6b14

6 files changed

Lines changed: 729 additions & 1 deletion

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2000-2026 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.datetimepicker;
17+
18+
import java.time.DayOfWeek;
19+
import java.time.LocalDate;
20+
import java.time.LocalDateTime;
21+
import java.util.ArrayList;
22+
import java.util.Collection;
23+
import java.util.LinkedHashSet;
24+
import java.util.List;
25+
import java.util.Set;
26+
27+
import com.vaadin.flow.component.datepicker.DateMetadata;
28+
import com.vaadin.flow.component.datepicker.DateRange;
29+
import com.vaadin.flow.component.html.Div;
30+
import com.vaadin.flow.component.html.NativeButton;
31+
import com.vaadin.flow.router.Route;
32+
33+
@Route("vaadin-date-time-picker/date-metadata")
34+
public class DateTimePickerDateMetadataPage extends Div {
35+
36+
// January 2023 starts on a Sunday, so the weekend days are 7, 8, 14, 15,
37+
// 21, 22, 28 and 29. The days below are picked so that each one is disabled
38+
// by exactly one constraint.
39+
public static final LocalDateTime INITIAL_VALUE = LocalDate.of(2023, 1, 5)
40+
.atTime(12, 0);
41+
public static final String MONTH_HEADER = "January 2023";
42+
public static final int ENABLED_DAY = 12;
43+
public static final int FIXED_DISABLED_DAY = 10;
44+
public static final int OTHER_FIXED_DISABLED_DAY = 20;
45+
public static final int SATURDAY_DAY = 7;
46+
public static final int SUNDAY_DAY = 8;
47+
public static final int PROVIDER_DISABLED_DAY = 17;
48+
public static final int REFRESHED_DISABLED_DAY = 18;
49+
public static final int PART_NAME_DAY = 25;
50+
public static final String PART_NAME = "busy";
51+
private final Set<Integer> disabledDays = new LinkedHashSet<>(
52+
List.of(PROVIDER_DISABLED_DAY));
53+
54+
public DateTimePickerDateMetadataPage() {
55+
DateTimePicker dateTimePicker = new DateTimePicker();
56+
dateTimePicker.setId("date-time-picker");
57+
dateTimePicker.setValue(INITIAL_VALUE);
58+
dateTimePicker.setDisabledDates(
59+
List.of(LocalDate.of(2023, 1, FIXED_DISABLED_DAY),
60+
LocalDate.of(2023, 1, OTHER_FIXED_DISABLED_DAY)));
61+
dateTimePicker.setDisabledWeekdays(
62+
List.of(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY));
63+
dateTimePicker.setDateMetadataProvider(this::getDateMetadata);
64+
65+
NativeButton refresh = new NativeButton("Refresh date metadata",
66+
event -> {
67+
disabledDays.add(REFRESHED_DISABLED_DAY);
68+
dateTimePicker.refreshDateMetadata();
69+
});
70+
refresh.setId("refresh");
71+
72+
add(dateTimePicker, refresh);
73+
}
74+
75+
/**
76+
* Answers from the mutable set of disabled days, so that a refresh can
77+
* change the answer without the provider being set again.
78+
*/
79+
private Collection<DateMetadata> getDateMetadata(DateRange range) {
80+
List<DateMetadata> metadata = new ArrayList<>();
81+
for (LocalDate date = range.start(); !date
82+
.isAfter(range.end()); date = date.plusDays(1)) {
83+
if (disabledDays.contains(date.getDayOfMonth())) {
84+
metadata.add(new DateMetadata(date, true));
85+
} else if (date.getDayOfMonth() == PART_NAME_DAY) {
86+
metadata.add(new DateMetadata(date, PART_NAME));
87+
}
88+
}
89+
return metadata;
90+
}
91+
92+
}

‎vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow-integration-tests/src/main/java/com/vaadin/flow/component/datetimepicker/validation/BasicValidationPage.java‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package com.vaadin.flow.component.datetimepicker.validation;
1717

18+
import java.time.LocalDate;
1819
import java.time.LocalDateTime;
20+
import java.util.List;
1921

22+
import com.vaadin.flow.component.datepicker.DateMetadata;
23+
import com.vaadin.flow.component.datepicker.DateMetadataProvider;
2024
import com.vaadin.flow.component.datetimepicker.DateTimePicker;
2125
import com.vaadin.flow.router.Route;
2226
import com.vaadin.tests.validation.AbstractValidationPage;
@@ -27,13 +31,16 @@ public class BasicValidationPage
2731
public static final String REQUIRED_BUTTON = "required-button";
2832
public static final String MIN_INPUT = "min-input";
2933
public static final String MAX_INPUT = "max-input";
34+
public static final String DISABLED_DATES_INPUT = "disabled-dates-input";
35+
public static final String METADATA_PROVIDER_INPUT = "metadata-provider-input";
3036
public static final String CLEAR_VALUE_BUTTON = "clear-value-button";
3137

3238
public static final String REQUIRED_ERROR_MESSAGE = "Field is required";
3339
public static final String BAD_INPUT_ERROR_MESSAGE = "Invalid date format";
3440
public static final String INCOMPLETE_INPUT_ERROR_MESSAGE = "Must fill in both date and time";
3541
public static final String MIN_ERROR_MESSAGE = "Date is too early";
3642
public static final String MAX_ERROR_MESSAGE = "Date is too late";
43+
public static final String DISABLED_DATE_ERROR_MESSAGE = "Date is disabled";
3744

3845
public BasicValidationPage() {
3946
super();
@@ -43,7 +50,8 @@ public BasicValidationPage() {
4350
.setIncompleteInputErrorMessage(INCOMPLETE_INPUT_ERROR_MESSAGE)
4451
.setBadInputErrorMessage(BAD_INPUT_ERROR_MESSAGE)
4552
.setMinErrorMessage(MIN_ERROR_MESSAGE)
46-
.setMaxErrorMessage(MAX_ERROR_MESSAGE));
53+
.setMaxErrorMessage(MAX_ERROR_MESSAGE)
54+
.setDisabledDateErrorMessage(DISABLED_DATE_ERROR_MESSAGE));
4755

4856
add(createButton(REQUIRED_BUTTON, "Enable required", event -> {
4957
testField.setRequiredIndicatorVisible(true);
@@ -59,6 +67,20 @@ public BasicValidationPage() {
5967
testField.setMax(value);
6068
}));
6169

70+
add(createInput(DISABLED_DATES_INPUT, "Set disabled dates", event -> {
71+
var value = LocalDate.parse(event.getValue());
72+
testField.setDisabledDates(List.of(value));
73+
}));
74+
75+
add(createInput(METADATA_PROVIDER_INPUT,
76+
"Set date metadata provider disabling a date", event -> {
77+
var disabledDate = LocalDate.parse(event.getValue());
78+
testField.setDateMetadataProvider(DateMetadataProvider
79+
.perDate(date -> date.equals(disabledDate)
80+
? new DateMetadata(date, true)
81+
: null));
82+
}));
83+
6284
add(createButton(CLEAR_VALUE_BUTTON, "Clear value", event -> {
6385
testField.clear();
6486
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2000-2026 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.datetimepicker;
17+
18+
import static com.vaadin.flow.component.datetimepicker.DateTimePickerDateMetadataPage.ENABLED_DAY;
19+
import static com.vaadin.flow.component.datetimepicker.DateTimePickerDateMetadataPage.FIXED_DISABLED_DAY;
20+
import static com.vaadin.flow.component.datetimepicker.DateTimePickerDateMetadataPage.MONTH_HEADER;
21+
import static com.vaadin.flow.component.datetimepicker.DateTimePickerDateMetadataPage.OTHER_FIXED_DISABLED_DAY;
22+
import static com.vaadin.flow.component.datetimepicker.DateTimePickerDateMetadataPage.PART_NAME;
23+
import static com.vaadin.flow.component.datetimepicker.DateTimePickerDateMetadataPage.PART_NAME_DAY;
24+
import static com.vaadin.flow.component.datetimepicker.DateTimePickerDateMetadataPage.PROVIDER_DISABLED_DAY;
25+
import static com.vaadin.flow.component.datetimepicker.DateTimePickerDateMetadataPage.REFRESHED_DISABLED_DAY;
26+
import static com.vaadin.flow.component.datetimepicker.DateTimePickerDateMetadataPage.SATURDAY_DAY;
27+
import static com.vaadin.flow.component.datetimepicker.DateTimePickerDateMetadataPage.SUNDAY_DAY;
28+
29+
import java.util.List;
30+
31+
import org.junit.Assert;
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
35+
import com.vaadin.flow.component.datepicker.testbench.DatePickerElement;
36+
import com.vaadin.flow.component.datepicker.testbench.DatePickerElement.DayElement;
37+
import com.vaadin.flow.component.datepicker.testbench.DatePickerElement.MonthCalendarElement;
38+
import com.vaadin.flow.component.datepicker.testbench.DatePickerElement.OverlayContentElement;
39+
import com.vaadin.flow.component.datetimepicker.testbench.DateTimePickerElement;
40+
import com.vaadin.flow.testutil.TestPath;
41+
import com.vaadin.tests.AbstractComponentIT;
42+
43+
/**
44+
* Integration tests for the {@link DateTimePickerDateMetadataPage}.
45+
*/
46+
@TestPath("vaadin-date-time-picker/date-metadata")
47+
public class DateTimePickerDateMetadataIT extends AbstractComponentIT {
48+
49+
private DatePickerElement datePicker;
50+
51+
@Before
52+
public void init() {
53+
open();
54+
datePicker = $(DateTimePickerElement.class).id("date-time-picker")
55+
.getDatePicker();
56+
}
57+
58+
@Test
59+
public void openOverlay_fixedDatesAreDisabled() {
60+
MonthCalendarElement calendar = openCalendar(datePicker, MONTH_HEADER);
61+
62+
Assert.assertTrue(isDayDisabled(calendar, FIXED_DISABLED_DAY));
63+
Assert.assertTrue(isDayDisabled(calendar, OTHER_FIXED_DISABLED_DAY));
64+
Assert.assertFalse(isDayDisabled(calendar, ENABLED_DAY));
65+
}
66+
67+
@Test
68+
public void openOverlay_weekendsAreDisabled() {
69+
MonthCalendarElement calendar = openCalendar(datePicker, MONTH_HEADER);
70+
71+
Assert.assertTrue(isDayDisabled(calendar, SATURDAY_DAY));
72+
Assert.assertTrue(isDayDisabled(calendar, SUNDAY_DAY));
73+
}
74+
75+
@Test
76+
public void openOverlay_providerDisablesDate() {
77+
MonthCalendarElement calendar = openCalendar(datePicker, MONTH_HEADER);
78+
79+
waitUntil(driver -> isDayDisabled(calendar, PROVIDER_DISABLED_DAY));
80+
Assert.assertFalse(isDayDisabled(calendar, ENABLED_DAY));
81+
}
82+
83+
@Test
84+
public void openOverlay_providerAddsPartName() {
85+
MonthCalendarElement calendar = openCalendar(datePicker, MONTH_HEADER);
86+
87+
waitUntil(driver -> hasDayPart(calendar.getDay(PART_NAME_DAY),
88+
PART_NAME));
89+
Assert.assertFalse(isDayDisabled(calendar, PART_NAME_DAY));
90+
}
91+
92+
@Test
93+
public void refreshDateMetadata_disabledDatesAreUpdated() {
94+
MonthCalendarElement calendar = openCalendar(datePicker, MONTH_HEADER);
95+
waitUntil(driver -> isDayDisabled(calendar, PROVIDER_DISABLED_DAY));
96+
Assert.assertFalse(isDayDisabled(calendar, REFRESHED_DISABLED_DAY));
97+
98+
datePicker.close();
99+
clickElementWithJs("refresh");
100+
101+
MonthCalendarElement refreshedCalendar = openCalendar(datePicker,
102+
MONTH_HEADER);
103+
waitUntil(driver -> isDayDisabled(refreshedCalendar,
104+
REFRESHED_DISABLED_DAY));
105+
}
106+
107+
private boolean isDayDisabled(MonthCalendarElement calendar, int day) {
108+
return calendar.getDay(day).hasAttribute("disabled");
109+
}
110+
111+
private boolean hasDayPart(DayElement day, String partName) {
112+
return List.of(day.getDomAttribute("part").split(" "))
113+
.contains(partName);
114+
}
115+
116+
private MonthCalendarElement openCalendar(DatePickerElement picker,
117+
String monthHeader) {
118+
picker.open();
119+
return getCalendar(picker, monthHeader);
120+
}
121+
122+
private MonthCalendarElement getCalendar(DatePickerElement picker,
123+
String monthHeader) {
124+
OverlayContentElement overlayContent = picker.getOverlayContent();
125+
return waitUntil(driver -> overlayContent
126+
.getVisibleMonthCalendars().stream().filter(calendar -> calendar
127+
.getHeaderText().contains(monthHeader))
128+
.findFirst().orElse(null));
129+
}
130+
131+
}

‎vaadin-date-time-picker-flow-parent/vaadin-date-time-picker-flow-integration-tests/src/test/java/com/vaadin/flow/component/datetimepicker/validation/BasicValidationIT.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717

1818
import static com.vaadin.flow.component.datetimepicker.validation.BasicValidationPage.BAD_INPUT_ERROR_MESSAGE;
1919
import static com.vaadin.flow.component.datetimepicker.validation.BasicValidationPage.CLEAR_VALUE_BUTTON;
20+
import static com.vaadin.flow.component.datetimepicker.validation.BasicValidationPage.DISABLED_DATES_INPUT;
21+
import static com.vaadin.flow.component.datetimepicker.validation.BasicValidationPage.DISABLED_DATE_ERROR_MESSAGE;
2022
import static com.vaadin.flow.component.datetimepicker.validation.BasicValidationPage.INCOMPLETE_INPUT_ERROR_MESSAGE;
2123
import static com.vaadin.flow.component.datetimepicker.validation.BasicValidationPage.MAX_ERROR_MESSAGE;
2224
import static com.vaadin.flow.component.datetimepicker.validation.BasicValidationPage.MAX_INPUT;
25+
import static com.vaadin.flow.component.datetimepicker.validation.BasicValidationPage.METADATA_PROVIDER_INPUT;
2326
import static com.vaadin.flow.component.datetimepicker.validation.BasicValidationPage.MIN_ERROR_MESSAGE;
2427
import static com.vaadin.flow.component.datetimepicker.validation.BasicValidationPage.MIN_INPUT;
2528
import static com.vaadin.flow.component.datetimepicker.validation.BasicValidationPage.REQUIRED_BUTTON;
@@ -128,6 +131,33 @@ public void min_changeValue_assertValidity() {
128131
assertValidation(true, "");
129132
}
130133

134+
@Test
135+
public void disabledDates_changeValue_assertValidity() {
136+
$("input").id(DISABLED_DATES_INPUT).sendKeys("2000-02-02", Keys.ENTER);
137+
138+
// The disabled date is reported even though the time is still missing
139+
setDateInputValue("2/2/2000");
140+
assertValidation(false, DISABLED_DATE_ERROR_MESSAGE);
141+
142+
setTimeInputValue("12:00");
143+
assertValidation(false, DISABLED_DATE_ERROR_MESSAGE);
144+
145+
setDateInputValue("2/3/2000");
146+
assertValidation(true, "");
147+
}
148+
149+
@Test
150+
public void dateMetadataProvider_changeValue_assertValidity() {
151+
$("input").id(METADATA_PROVIDER_INPUT).sendKeys("2000-02-02",
152+
Keys.ENTER);
153+
154+
setValue("2/2/2000", "12:00");
155+
assertValidation(false, DISABLED_DATE_ERROR_MESSAGE);
156+
157+
setDateInputValue("2/3/2000");
158+
assertValidation(true, "");
159+
}
160+
131161
@Test
132162
public void max_changeDateInputValue_assertValidity() {
133163
$("input").id(MAX_INPUT).sendKeys("2000-02-02T12:00", Keys.ENTER);

0 commit comments

Comments
 (0)