Skip to content

Commit 40740f4

Browse files
web-padawanclaude
andauthored
feat: add date metadata provider to DatePicker (#9865)
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 4a1e678 commit 40740f4

8 files changed

Lines changed: 845 additions & 22 deletions

File tree

‎vaadin-date-picker-flow-parent/vaadin-date-picker-flow-integration-tests/test/date-picker-connector.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from 'chai';
22
import { fixtureSync } from '@vaadin/testing-helpers';
3+
import * as sinon from 'sinon';
34
import dateFnsFormat from 'date-fns/format';
45
import { DatePickerDate } from '@vaadin/date-picker';
56
import { init, extractDateParts, datepickerConnector, type FlowDatePicker } from './shared.js';
@@ -158,5 +159,69 @@ describe('date-picker connector', () => {
158159
datePicker.$connector.setDateMetadataConfig({});
159160
expect(datePicker.isDateDisabled).to.be.undefined;
160161
});
162+
163+
// The range the web component would pass for a whole year, with 0-based months.
164+
const RANGE = {
165+
start: { year: 2024, month: 0, day: 1 },
166+
end: { year: 2024, month: 11, day: 31 }
167+
};
168+
169+
it('should not set dateMetadataProvider when hasProvider is false', () => {
170+
datePicker.$connector.setDateMetadataConfig({ hasProvider: false });
171+
expect(datePicker.dateMetadataProvider).to.be.null;
172+
});
173+
174+
it('should keep the same dateMetadataProvider reference across config updates', () => {
175+
// The web component compares the provider by reference and clears its cache when a new
176+
// function is assigned, so every config update has to reuse the same function object.
177+
datePicker.$connector.setDateMetadataConfig({ disabledDates: [[2024, 0, 2]], hasProvider: true });
178+
const provider = datePicker.dateMetadataProvider;
179+
expect(provider).to.be.a('function');
180+
181+
datePicker.$connector.setDateMetadataConfig({ disabledDates: [[2024, 0, 3]], hasProvider: true });
182+
expect(datePicker.dateMetadataProvider).to.equal(provider);
183+
});
184+
185+
it('should set dateMetadataProvider to null when hasProvider becomes false', () => {
186+
datePicker.$connector.setDateMetadataConfig({ hasProvider: true });
187+
expect(datePicker.dateMetadataProvider).to.be.a('function');
188+
189+
datePicker.$connector.setDateMetadataConfig({ hasProvider: false });
190+
expect(datePicker.dateMetadataProvider).to.be.null;
191+
});
192+
193+
it('should call $server.requestDateMetadata with ISO dates', () => {
194+
const requestDateMetadata = sinon.stub().resolves([]);
195+
datePicker.$server = { requestDateMetadata };
196+
datePicker.$connector.setDateMetadataConfig({ hasProvider: true });
197+
198+
datePicker.dateMetadataProvider!(RANGE);
199+
200+
expect(requestDateMetadata).to.be.calledOnce;
201+
expect(requestDateMetadata).to.be.calledWithExactly('2024-01-01', '2024-12-31');
202+
});
203+
204+
it('should pad the ISO dates for years below 100', () => {
205+
// An unpadded join would send "50-1-1", which the server cannot parse.
206+
const requestDateMetadata = sinon.stub().resolves([]);
207+
datePicker.$server = { requestDateMetadata };
208+
datePicker.$connector.setDateMetadataConfig({ hasProvider: true });
209+
210+
datePicker.dateMetadataProvider!({
211+
start: { year: 50, month: 0, day: 1 },
212+
end: { year: 50, month: 11, day: 31 }
213+
});
214+
215+
expect(requestDateMetadata).to.be.calledWithExactly('0050-01-01', '0050-12-31');
216+
});
217+
218+
it('should resolve the provider with the server response unchanged', async () => {
219+
const metadata = [{ year: 2024, month: 0, day: 2, disabled: true }];
220+
datePicker.$server = { requestDateMetadata: sinon.stub().resolves(metadata) };
221+
datePicker.$connector.setDateMetadataConfig({ hasProvider: true });
222+
223+
const result = await datePicker.dateMetadataProvider!(RANGE);
224+
expect(result).to.equal(metadata);
225+
});
161226
});
162227
});

‎vaadin-date-picker-flow-parent/vaadin-date-picker-flow-integration-tests/test/shared.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import './env-setup.js';
22
import '@vaadin/date-picker/src/vaadin-date-picker.js';
33
import '../frontend/generated/jar-resources/datepickerConnector.js';
4-
import type { DatePicker } from '@vaadin/date-picker/src/vaadin-date-picker.js';
4+
import type { DatePicker, DatePickerDateMetadata } from '@vaadin/date-picker/src/vaadin-date-picker.js';
55
export { extractDateParts } from '@vaadin/date-picker/src/vaadin-date-picker-helper.js';
66
import type {} from '@web/test-runner-mocha';
77

@@ -15,6 +15,8 @@ export type FlowDatePickerDateMetadataConfig = {
1515
disabledDates?: [number, number, number][];
1616
/** Disabled weekdays as ISO weekday numbers, Monday = 1 ... Sunday = 7. */
1717
disabledWeekdays?: number[];
18+
/** Whether a date metadata provider is set on the server. */
19+
hasProvider?: boolean;
1820
};
1921

2022
export type DatePickerConnector = {
@@ -23,8 +25,15 @@ export type DatePickerConnector = {
2325
setDateMetadataConfig: (config: FlowDatePickerDateMetadataConfig) => void;
2426
};
2527

28+
export type DatePickerServer = {
29+
/** The range is given as ISO 8601 dates, the entries come back with a 0-based month. */
30+
requestDateMetadata: (start: string, end: string) => Promise<DatePickerDateMetadata[]>;
31+
};
32+
2633
export type FlowDatePicker = DatePicker & {
2734
$connector: DatePickerConnector;
35+
// Assigned by Flow when the element is bound, and stubbed by the tests.
36+
$server?: DatePickerServer;
2837
};
2938

3039
type Vaadin = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.datepicker;
17+
18+
import java.io.Serializable;
19+
import java.time.LocalDate;
20+
import java.util.Objects;
21+
22+
/**
23+
* Metadata for a single date, returned from a {@link DateMetadataProvider}.
24+
*
25+
* @param date
26+
* the date the metadata applies to, not {@code null}
27+
* @param disabled
28+
* whether the date cannot be selected
29+
*
30+
* @author Vaadin Ltd
31+
* @since 25.3
32+
*/
33+
public record DateMetadata(LocalDate date,
34+
boolean disabled) implements Serializable {
35+
36+
/**
37+
* Creates new metadata for the given date.
38+
*
39+
* @param date
40+
* the date the metadata applies to, not {@code null}
41+
* @param disabled
42+
* whether the date cannot be selected
43+
*/
44+
public DateMetadata {
45+
Objects.requireNonNull(date, "Date cannot be null");
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.datepicker;
17+
18+
import java.io.Serializable;
19+
import java.util.Collection;
20+
21+
/**
22+
* A callback that provides metadata for a range of dates, for example to mark
23+
* dates as not selectable.
24+
* <p>
25+
* The callback runs on the server. The calendar calls it with the range of
26+
* dates it is about to show, which covers whole months, so a single query can
27+
* answer for all of them. Server-side validation calls it with the single date
28+
* being validated. Return an entry only for the dates that have metadata; dates
29+
* that are not mentioned have none.
30+
* <p>
31+
* Until the callback has answered, the affected dates render in a loading state
32+
* but <b>stay selectable</b>, so a slow callback does not make the calendar
33+
* unusable. A date selected before the answer arrives is reported invalid by
34+
* server-side validation.
35+
* <p>
36+
* Results are cached per month in the browser, but not on the server, so an
37+
* expensive implementation should cache its own results. Call
38+
* {@link DatePicker#refreshDateMetadata()} when the data behind the callback
39+
* has changed.
40+
*
41+
* @author Vaadin Ltd
42+
* @since 25.3
43+
*/
44+
@FunctionalInterface
45+
public interface DateMetadataProvider extends Serializable {
46+
47+
/**
48+
* Gets the metadata for the dates in the given range.
49+
*
50+
* @param range
51+
* the range of dates to provide metadata for, covering whole
52+
* months, never {@code null}
53+
* @return the metadata entries for the dates that have metadata, or an
54+
* empty collection if none have
55+
*/
56+
Collection<DateMetadata> getDateMetadata(DateRange range);
57+
}

0 commit comments

Comments
 (0)