From f82b5c18b82b5e6fd18c89b505fb65fdaac4732b Mon Sep 17 00:00:00 2001 From: Jorge del Casar <948953+jorgecasar@users.noreply.github.com> Date: Tue, 15 Dec 2020 19:30:19 +0100 Subject: [PATCH] fix(calendar): solve previousMonth and nextMonth conditions error --- .changeset/curly-ladybugs-grab.md | 5 ++++ packages/calendar/src/LionCalendar.js | 9 ++++-- packages/calendar/test/lion-calendar.test.js | 31 +++++++++++++++++++- 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 .changeset/curly-ladybugs-grab.md diff --git a/.changeset/curly-ladybugs-grab.md b/.changeset/curly-ladybugs-grab.md new file mode 100644 index 000000000..65fc34108 --- /dev/null +++ b/.changeset/curly-ladybugs-grab.md @@ -0,0 +1,5 @@ +--- +'@lion/calendar': patch +--- + +solve previousMonth and nextMonth conditions error diff --git a/packages/calendar/src/LionCalendar.js b/packages/calendar/src/LionCalendar.js index 77cceff3e..7d707036f 100644 --- a/packages/calendar/src/LionCalendar.js +++ b/packages/calendar/src/LionCalendar.js @@ -515,9 +515,12 @@ export class LionCalendar extends LocalizeMixin(LitElement) { const day = createDay(new Date(_day.date), _day); const today = normalizeDateTime(new Date()); day.central = isSameDate(day.date, this.centralDate); - day.previousMonth = currentMonth && day.date.getMonth() < currentMonth.getMonth(); - day.currentMonth = currentMonth && day.date.getMonth() === currentMonth.getMonth(); - day.nextMonth = currentMonth && day.date.getMonth() > currentMonth.getMonth(); + const dayYearMonth = `${day.date.getFullYear()}${`0${day.date.getMonth() + 1}`.slice(-2)}`; + const currentYearMonth = + currentMonth && `${currentMonth.getFullYear()}${`0${currentMonth.getMonth() + 1}`.slice(-2)}`; + day.previousMonth = currentMonth && dayYearMonth < currentYearMonth; + day.currentMonth = currentMonth && dayYearMonth === currentYearMonth; + day.nextMonth = currentMonth && dayYearMonth > currentYearMonth; day.selected = this.selectedDate ? isSameDate(day.date, this.selectedDate) : false; day.past = day.date < today; day.today = isSameDate(day.date, today); diff --git a/packages/calendar/test/lion-calendar.test.js b/packages/calendar/test/lion-calendar.test.js index 5ebaa268a..700a944e8 100644 --- a/packages/calendar/test/lion-calendar.test.js +++ b/packages/calendar/test/lion-calendar.test.js @@ -1206,7 +1206,6 @@ describe('', () => { `), ); const { previousMonthDayObjs, nextMonthDayObjs } = elObj; - expect(previousMonthDayObjs.length).to.equal(3); expect(previousMonthDayObjs[0].cellIndex).to.equal(0); expect(previousMonthDayObjs[0].monthday).to.equal(29); @@ -1222,6 +1221,36 @@ describe('', () => { expect(nextMonthDayObjs[1].monthday).to.equal(2); }); + it('renders days for next months in the last month of the year', async () => { + const elObj = new CalendarObject( + await fixture(html` + + `), + ); + const { nextMonthDayObjs } = elObj; + expect(nextMonthDayObjs.length).to.equal(2); + expect(nextMonthDayObjs[0].cellIndex).to.equal(5); + expect(nextMonthDayObjs[0].monthday).to.equal(1); + expect(nextMonthDayObjs[1].cellIndex).to.equal(6); + expect(nextMonthDayObjs[1].monthday).to.equal(2); + }); + + it('renders days for previous months in the first month of the year', async () => { + const elObj = new CalendarObject( + await fixture(html` + + `), + ); + const { previousMonthDayObjs } = elObj; + expect(previousMonthDayObjs.length).to.equal(3); + expect(previousMonthDayObjs[0].cellIndex).to.equal(0); + expect(previousMonthDayObjs[0].monthday).to.equal(29); + expect(previousMonthDayObjs[1].cellIndex).to.equal(1); + expect(previousMonthDayObjs[1].monthday).to.equal(30); + expect(previousMonthDayObjs[2].cellIndex).to.equal(2); + expect(previousMonthDayObjs[2].monthday).to.equal(31); + }); + it('sets aria-current="date" to todays button', async () => { const elObj = new CalendarObject(await fixture(html``)); const hasAriaCurrent = /** @param {DayObject} d */ d =>