From bb1f347699c401843da16a647655da5a3e0d0542 Mon Sep 17 00:00:00 2001 From: ByoungYong Kim Date: Mon, 16 Sep 2024 11:12:10 +0200 Subject: [PATCH] fix the bug that disabled date becomes central date (#2361) * fix the bug that disabled date becomes central date * add test * Fix tests * Fix lint error * Add changeset * Fix a typo --- .changeset/serious-mails-matter.md | 5 +++ .../components/calendar/src/LionCalendar.js | 14 ++++--- .../calendar/test/lion-calendar.test.js | 39 ++++++++++++++++--- 3 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 .changeset/serious-mails-matter.md diff --git a/.changeset/serious-mails-matter.md b/.changeset/serious-mails-matter.md new file mode 100644 index 000000000..9bfaf2c42 --- /dev/null +++ b/.changeset/serious-mails-matter.md @@ -0,0 +1,5 @@ +--- +'@lion/ui': patch +--- + +[calendar] Now central date would be nearest enabled date if today is disabled diff --git a/packages/ui/components/calendar/src/LionCalendar.js b/packages/ui/components/calendar/src/LionCalendar.js index a92f818f3..4627a2b48 100644 --- a/packages/ui/components/calendar/src/LionCalendar.js +++ b/packages/ui/components/calendar/src/LionCalendar.js @@ -270,11 +270,6 @@ export class LionCalendar extends LocalizeMixin(LitElement) { this.__connectedCallbackDone = true; - this.__calculateInitialCentralDate(); - - // setup data for initial render - this.__data = this.__createData(); - /** * This logic needs to happen on firstUpdated, but every time the DOM node is moved as well * since firstUpdated only runs once, this logic is moved here, but after updateComplete @@ -305,6 +300,13 @@ export class LionCalendar extends LocalizeMixin(LitElement) { } } + firstUpdated() { + this.__calculateInitialCentralDate(); + + // setup data for initial render + this.__data = this.__createData(); + } + disconnectedCallback() { super.disconnectedCallback(); if (this.__contentWrapperElement) { @@ -372,6 +374,8 @@ export class LionCalendar extends LocalizeMixin(LitElement) { if (this.centralDate === this.__today && this.selectedDate) { // initialized with selectedDate only if user didn't provide another one this.centralDate = this.selectedDate; + } else if (!this.__isEnabledDate(this.centralDate)) { + this.centralDate = this.findNearestEnabledDate(this.centralDate); } /** @type {Date} */ this.__initialCentralDate = this.centralDate; diff --git a/packages/ui/components/calendar/test/lion-calendar.test.js b/packages/ui/components/calendar/test/lion-calendar.test.js index 1e0daa591..41e9760cd 100644 --- a/packages/ui/components/calendar/test/lion-calendar.test.js +++ b/packages/ui/components/calendar/test/lion-calendar.test.js @@ -465,9 +465,8 @@ describe('', () => { `); clock.restore(); - expect(isSameDate(el.centralDate, new Date('2019/06/03')), 'central date').to.be.true; - expect(isSameDate(elSetting.centralDate, new Date('2019/07/03')), 'central date').to.be - .true; + expect(isSameDate(el.centralDate, new Date('2019/07/03'))).to.be.true; + expect(isSameDate(elSetting.centralDate, new Date('2019/07/03'))).to.be.true; }); describe('Normalization', () => { @@ -573,7 +572,7 @@ describe('', () => { .disableDates="${/** @param {Date} d */ d => d.getDate() === 15}" > `); - el.focusDate(el.findNearestEnabledDate()); + el.focusDate(el.findNearestEnabledDate(new Date())); await el.updateComplete; const elObj = new CalendarObject(el); @@ -590,7 +589,7 @@ describe('', () => { .disableDates="${/** @param {Date} d */ d => d.getFullYear() > 1998}" > `); - el.focusDate(el.findNearestEnabledDate()); + el.focusDate(el.findNearestEnabledDate(new Date())); await el.updateComplete; expect(el.centralDate.getFullYear()).to.equal(1998); @@ -609,7 +608,7 @@ describe('', () => { > `); - el.focusDate(el.findNearestEnabledDate()); + el.focusDate(el.findNearestEnabledDate(new Date())); await el.updateComplete; expect(el.centralDate.getFullYear()).to.equal(2002); @@ -1262,6 +1261,34 @@ describe('', () => { clock.restore(); }); + + it('is nearest to today if no selected date is available and today is disabled', async () => { + const clock = sinon.useFakeTimers({ now: new Date('2000/12/15').getTime() }); + + const calWithMin = await fixture( + html``, + ); + const calObjWithMin = new CalendarObject(calWithMin); + expect(calObjWithMin.centralDayObj?.monthday).to.equal(25); + + const calWithMax = await fixture( + html``, + ); + const calObjWithMax = new CalendarObject(calWithMax); + expect(calObjWithMax.centralDayObj?.monthday).to.equal(5); + + const calWithDisabled = await fixture( + html` date.getDate() === 15} + >`, + ); + await calWithDisabled.updateComplete; + + const calObjWithDisabled = new CalendarObject(calWithDisabled); + expect(calObjWithDisabled.centralDayObj?.monthday).to.equal(16); + + clock.restore(); + }); }); /**