From 0f16c9aa2b44cf00ea5010cff06cf7153cce76c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Schwartz?= <85172116+remischwartz@users.noreply.github.com> Date: Mon, 26 Aug 2024 14:21:50 +0200 Subject: [PATCH] feat: improve a11y (#27) * feat: improve a11y * fix: adapt tests to new behavior --- .../wc-datepicker/wc-datepicker.spec.tsx | 41 ++++--------------- .../wc-datepicker/wc-datepicker.tsx | 36 +++++++++++++--- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/components/wc-datepicker/wc-datepicker.spec.tsx b/src/components/wc-datepicker/wc-datepicker.spec.tsx index 4ba9696..d1146bd 100644 --- a/src/components/wc-datepicker/wc-datepicker.spec.tsx +++ b/src/components/wc-datepicker/wc-datepicker.spec.tsx @@ -309,11 +309,6 @@ describe('wc-datepicker', () => { const monthSelect = page.root.querySelector( '.wc-datepicker__month-select' ); - - const header = page.root.querySelector( - '.wc-datepicker__header' - ); - const previousMonthButton = page.root.querySelector( '.wc-datepicker__previous-month-button' ); @@ -322,26 +317,21 @@ describe('wc-datepicker', () => { '.wc-datepicker__next-month-button' ); - expect(header.innerText.startsWith('January')).toBeTruthy(); - monthSelect.value = '5'; monthSelect.dispatchEvent(new Event('change')); await page.waitForChanges(); - expect(header.innerText.startsWith('May')).toBeTruthy(); expect(spy.mock.calls[0][0].detail).toEqual({ month: 5, year: 2022 }); previousMonthButton.click(); await page.waitForChanges(); - expect(header.innerText.startsWith('April')).toBeTruthy(); expect(spy.mock.calls[1][0].detail).toEqual({ month: 4, year: 2022 }); nextMonthButton.click(); await page.waitForChanges(); - expect(header.innerText.startsWith('May')).toBeTruthy(); expect(spy.mock.calls[2][0].detail).toEqual({ month: 5, year: 2022 }); }); @@ -359,10 +349,6 @@ describe('wc-datepicker', () => { '.wc-datepicker__year-select' ); - const header = page.root.querySelector( - '.wc-datepicker__header' - ); - const previousYearButton = page.root.querySelector( '.wc-datepicker__previous-year-button' ); @@ -371,26 +357,25 @@ describe('wc-datepicker', () => { '.wc-datepicker__next-year-button' ); - expect(header.innerText.includes('2022')).toBeTruthy(); + expect(yearSelect.value).toEqual('2022'); yearSelect.value = '1989'; yearSelect.dispatchEvent(new Event('change')); await page.waitForChanges(); - expect(header.innerText.includes('1989')).toBeTruthy(); expect(spy.mock.calls[0][0].detail).toEqual({ month: 1, year: 1989 }); previousYearButton.click(); await page.waitForChanges(); - expect(header.innerText.includes('1988')).toBeTruthy(); + expect(yearSelect.value).toEqual('1988'); expect(spy.mock.calls[1][0].detail).toEqual({ month: 1, year: 1988 }); nextYearButton.click(); await page.waitForChanges(); - expect(header.innerText.includes('1989')).toBeTruthy(); + expect(yearSelect.value).toEqual('1989'); expect(spy.mock.calls[2][0].detail).toEqual({ month: 1, year: 1989 }); }); @@ -405,26 +390,18 @@ describe('wc-datepicker', () => { '.wc-datepicker__today-button' ); - const header = page.root.querySelector( - '.wc-datepicker__header' - ); - const today = new Date(); - expect(header.innerText.includes('January 1, 1989')).toBeTruthy(); + const yearSelect = page.root.querySelector( + '.wc-datepicker__year-select' + ); + + expect(yearSelect.value).toEqual('1989'); todayButton.click(); await page.waitForChanges(); - expect( - header.innerText.includes( - Intl.DateTimeFormat('en-US', { - day: 'numeric', - month: 'long', - year: 'numeric' - }).format(today) - ) - ).toBeTruthy(); + expect(yearSelect.value).toEqual(today.getFullYear().toString()); }); it('clears its value', async () => { diff --git a/src/components/wc-datepicker/wc-datepicker.tsx b/src/components/wc-datepicker/wc-datepicker.tsx index e61a936..002245e 100644 --- a/src/components/wc-datepicker/wc-datepicker.tsx +++ b/src/components/wc-datepicker/wc-datepicker.tsx @@ -185,15 +185,36 @@ export class WCDatepicker { } private getTitle() { - if (!Boolean(this.currentDate)) { + if (!Boolean(this.value)) { return; } - return Intl.DateTimeFormat(this.locale, { - day: 'numeric', - month: 'long', - year: 'numeric' - }).format(this.currentDate); + if (this.isRangeValue(this.value)) { + const startDate = Intl.DateTimeFormat(this.locale, { + day: 'numeric', + month: 'long', + year: 'numeric' + }).format(this.value[0]); + const endDate = this.value[1] + ? Intl.DateTimeFormat(this.locale, { + day: 'numeric', + month: 'long', + year: 'numeric' + }).format(this.value[1]) + : undefined; + + if (Boolean(endDate)) { + return `${startDate} - ${endDate}`; + } else { + return startDate; + } + } else { + return Intl.DateTimeFormat(this.locale, { + day: 'numeric', + month: 'long', + year: 'numeric' + }).format(this.value); + } } private focusDate(date: Date) { @@ -471,6 +492,7 @@ export class WCDatepicker { )} {this.weekdays.map((weekday) => (