Skip to content

Commit be219a2

Browse files
authored
fix(datetime): years displayed now more consistent with v5 datetime, max and min are now accounted for in MD mode (#23616)
resolves #23615
1 parent 9ce57d2 commit be219a2

File tree

6 files changed

+22
-16
lines changed

6 files changed

+22
-16
lines changed

core/src/components/datetime/datetime.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,13 +1180,12 @@ export class Datetime implements ComponentInterface {
11801180
this.showMonthAndYear = !this.showMonthAndYear;
11811181
}
11821182

1183-
private renderMDYearView() {
1184-
return getCalendarYears(this.activeParts, true, undefined, undefined, this.parsedYearValues).map(year => {
1183+
private renderMDYearView(calendarYears: number[] = []) {
1184+
return calendarYears.map(year => {
11851185

1186-
const { isCurrentYear, isActiveYear, disabled, ariaSelected } = getCalendarYearState(year, this.workingParts, this.todayParts, this.minParts, this.maxParts);
1186+
const { isCurrentYear, isActiveYear, ariaSelected } = getCalendarYearState(year, this.workingParts, this.todayParts);
11871187
return (
11881188
<button
1189-
disabled={disabled}
11901189
aria-selected={ariaSelected}
11911190
class={{
11921191
'datetime-year-item': true,
@@ -1209,7 +1208,7 @@ export class Datetime implements ComponentInterface {
12091208
})
12101209
}
12111210

1212-
private renderiOSYearView() {
1211+
private renderiOSYearView(calendarYears: number[] = []) {
12131212
return [
12141213
<div class="datetime-picker-before"></div>,
12151214
<div class="datetime-picker-after"></div>,
@@ -1238,7 +1237,7 @@ export class Datetime implements ComponentInterface {
12381237
<div class="picker-col-item picker-col-item-empty">&nbsp;</div>
12391238
<div class="picker-col-item picker-col-item-empty">&nbsp;</div>
12401239
<div class="picker-col-item picker-col-item-empty">&nbsp;</div>
1241-
{getCalendarYears(this.workingParts, false, this.minParts, this.maxParts, this.parsedYearValues).map(year => {
1240+
{calendarYears.map(year => {
12421241
return (
12431242
<div
12441243
class="picker-col-item"
@@ -1258,10 +1257,11 @@ export class Datetime implements ComponentInterface {
12581257
}
12591258

12601259
private renderYearView(mode: Mode) {
1260+
const calendarYears = getCalendarYears(this.todayParts, this.minParts, this.maxParts, this.parsedYearValues);
12611261
return (
12621262
<div class="datetime-year">
12631263
<div class="datetime-year-body">
1264-
{mode === 'ios' ? this.renderiOSYearView() : this.renderMDYearView()}
1264+
{mode === 'ios' ? this.renderiOSYearView(calendarYears) : this.renderMDYearView(calendarYears)}
12651265
</div>
12661266
</div>
12671267
);

core/src/components/datetime/readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ always in the 24-hour format, so `00` is `12am` on a 12-hour clock, `13` means
4444

4545
## Min and Max Datetimes
4646

47-
By default, there is no maximum or minimum date a user can select. To customize the minimum and maximum datetime values, the `min` and `max` component properties can be provided which may make more sense for the app's use-case. Following the same IS0 8601 format listed in the table above, each component can restrict which dates can be selected by the user. By passing `2016` to the `min` property and `2020-10-31` to the `max` property, the datetime will restrict the date selection between the beginning of `2016`, and `October 31st of 2020`.
47+
Dates are infinite in either direction, so for a user's selection there should be at least some form of restricting the dates that can be selected. By default, the maximum date is to the end of the current year, and the minimum date is from the beginning of the year that was 100 years ago.
48+
49+
To customize the minimum and maximum datetime values, the `min` and `max` component properties can be provided which may make more sense for the app's use-case. Following the same IS0 8601 format listed in the table above, each component can restrict which dates can be selected by the user. By passing `2016` to the `min` property and `2020-10-31` to the `max` property, the datetime will restrict the date selection between the beginning of `2016`, and `October 31st of 2020`.
4850

4951
## Selecting Specific Values
5052

core/src/components/datetime/test/minmax/e2e.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ test('minmax', async () => {
99

1010
screenshotCompares.push(await page.compareScreenshot());
1111

12+
const monthYearItem = await page.find('ion-datetime#inside >>> .calendar-month-year');
13+
14+
await monthYearItem.click();
15+
await page.waitForChanges();
16+
17+
screenshotCompares.push(await page.compareScreenshot());
18+
1219
for (const screenshotCompare of screenshotCompares) {
1320
expect(screenshotCompare).toMatchScreenshot();
1421
}

core/src/components/datetime/test/minmax/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<div class="grid-item">
4444
<h2>Value inside Bounds</h2>
4545
<ion-datetime
46-
id="outside"
46+
id="inside"
4747
min="2021-06-05"
4848
max="2021-06-29"
4949
value="2021-06-20"
@@ -52,7 +52,7 @@ <h2>Value inside Bounds</h2>
5252
<div class="grid-item">
5353
<h2>Value Outside Bounds</h2>
5454
<ion-datetime
55-
id="inside"
55+
id="outside"
5656
min="2021-06-05"
5757
max="2021-06-19"
5858
value="2021-06-20"

core/src/components/datetime/utils/data.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ export const getPickerMonths = (
261261

262262
export const getCalendarYears = (
263263
refParts: DatetimeParts,
264-
showOutOfBoundsYears = false,
265264
minParts?: DatetimeParts,
266265
maxParts?: DatetimeParts,
267266
yearValues?: number[]
@@ -277,8 +276,8 @@ export const getCalendarYears = (
277276
return processedYears;
278277
} else {
279278
const { year } = refParts;
280-
const maxYear = (showOutOfBoundsYears) ? year + 20 : (maxParts?.year || year + 20)
281-
const minYear = (showOutOfBoundsYears) ? year - 20 : (minParts?.year || year - 20);
279+
const maxYear = (maxParts?.year || year);
280+
const minYear = (minParts?.year || year - 100);
282281

283282
const years = [];
284283
for (let i = maxYear; i >= minYear; i--) {

core/src/components/datetime/utils/state.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,11 @@ export const isDayDisabled = (
7777
return false;
7878
}
7979

80-
export const getCalendarYearState = (refYear: number, activeParts: DatetimeParts, todayParts: DatetimeParts, minParts?: DatetimeParts, maxParts?: DatetimeParts) => {
80+
export const getCalendarYearState = (refYear: number, activeParts: DatetimeParts, todayParts: DatetimeParts) => {
8181
const isActiveYear = refYear === activeParts.year;
8282
const isCurrentYear = refYear === todayParts.year;
83-
const disabled = isYearDisabled(refYear, minParts, maxParts);
8483

8584
return {
86-
disabled,
8785
isActiveYear,
8886
isCurrentYear,
8987
ariaSelected: isActiveYear ? 'true' : null

0 commit comments

Comments
 (0)