Skip to content

Commit

Permalink
fix(datetime): correct year is set in wheel picker (#25896)
Browse files Browse the repository at this point in the history
resolves #25895
  • Loading branch information
liamdebeasi authored Sep 9, 2022
1 parent b598de6 commit fb653eb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
1 change: 0 additions & 1 deletion core/src/components/datetime/datetime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,6 @@ export class Datetime implements ComponentInterface {

const result = getCombinedDateColumnData(
locale,
workingParts,
todayParts,
min,
max,
Expand Down
24 changes: 23 additions & 1 deletion core/src/components/datetime/test/data.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateMonths, getDaysOfWeek, generateTime, getToday } from '../utils/data';
import { generateMonths, getDaysOfWeek, generateTime, getToday, getCombinedDateColumnData } from '../utils/data';

describe('generateMonths()', () => {
it('should generate correct month data', () => {
Expand Down Expand Up @@ -342,3 +342,25 @@ describe('getToday', () => {
expect(res).toEqual('2022-02-21T18:30:00.000Z');
});
});

describe('getCombinedDateColumnData', () => {
it('should return correct data with dates across years', () => {
const { parts, items } = getCombinedDateColumnData(
'en-US',
{ day: 1, month: 1, year: 2021 },
{ day: 31, month: 12, year: 2020 },
{ day: 2, month: 1, year: 2021 }
);

expect(parts).toEqual([
{ month: 12, year: 2020, day: 31 },
{ month: 1, year: 2021, day: 1 },
{ month: 1, year: 2021, day: 2 },
]);
expect(items).toEqual([
{ text: 'Thu, Dec 31', value: '2020-12-31' },
{ text: 'Today', value: '2021-1-1' },
{ text: 'Sat, Jan 2', value: '2021-1-2' },
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ test.describe('datetime: prefer wheel', () => {

const dateValues = page.locator('.date-column .picker-item:not(.picker-item-empty)');

expect(await dateValues.count()).toBe(427);
expect(await dateValues.count()).toBe(397);
});
});
test.describe('datetime: time-date wheel rendering', () => {
Expand Down Expand Up @@ -356,7 +356,7 @@ test.describe('datetime: prefer wheel', () => {

const dateValues = page.locator('.date-column .picker-item:not(.picker-item-empty)');

expect(await dateValues.count()).toBe(427);
expect(await dateValues.count()).toBe(397);
});
});
});
9 changes: 4 additions & 5 deletions core/src/components/datetime/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,6 @@ const getAllMonthsInRange = (currentParts: DatetimeParts, maxParts: DatetimePart
*/
export const getCombinedDateColumnData = (
locale: string,
refParts: DatetimeParts,
todayParts: DatetimeParts,
minParts: DatetimeParts,
maxParts: DatetimeParts,
Expand Down Expand Up @@ -473,7 +472,7 @@ export const getCombinedDateColumnData = (
* of work as the text.
*/
months.forEach((monthObject) => {
const referenceMonth = { month: monthObject.month, day: null, year: refParts.year };
const referenceMonth = { month: monthObject.month, day: null, year: monthObject.year };
const monthDays = getDayColumnData(locale, referenceMonth, minParts, maxParts, dayValues, {
month: 'short',
day: 'numeric',
Expand All @@ -492,7 +491,7 @@ export const getCombinedDateColumnData = (
*/
dateColumnItems.push({
text: isToday ? getTodayLabel(locale) : dayObject.text,
value: `${refParts.year}-${monthObject.month}-${dayObject.value}`,
value: `${referenceMonth.year}-${referenceMonth.month}-${dayObject.value}`,
});

/**
Expand All @@ -506,8 +505,8 @@ export const getCombinedDateColumnData = (
* updating the picker column value.
*/
dateParts.push({
month: monthObject.month,
year: refParts.year,
month: referenceMonth.month,
year: referenceMonth.year,
day: dayObject.value as number,
});
});
Expand Down

0 comments on commit fb653eb

Please sign in to comment.