From 6d57e94dff0af50c385224519571923e2c72415f Mon Sep 17 00:00:00 2001 From: aidataba Date: Tue, 3 Nov 2026 15:29:25 +0100 Subject: [PATCH] feat(core): accessibility date picker --- .../catalyst/src/lib/directives/proxies.ts | 4 ++ core/src/components.d.ts | 16 ++++++++ .../cat-date-inline/cat-date-inline.e2e.tsx | 40 +++++++++++++++++++ .../cat-date-inline/cat-date-inline.spec.tsx | 19 +++++++++ .../cat-date-inline/cat-date-inline.tsx | 10 ++++- core/src/components/cat-date-inline/readme.md | 1 + .../src/components/cat-date/cat-date.spec.tsx | 7 ++++ core/src/components/cat-date/cat-date.tsx | 6 +++ core/src/components/cat-date/readme.md | 1 + 9 files changed, 103 insertions(+), 1 deletion(-) diff --git a/angular/projects/catalyst/src/lib/directives/proxies.ts b/angular/projects/catalyst/src/lib/directives/proxies.ts index 36b9ad2d..f9d51621 100644 --- a/angular/projects/catalyst/src/lib/directives/proxies.ts +++ b/angular/projects/catalyst/src/lib/directives/proxies.ts @@ -318,6 +318,7 @@ export declare interface CatCheckbox extends Components.CatCheckbox { inputs: [ 'autoComplete', 'clearable', + 'dayNativeAttributes', 'dayVariants', 'disabled', 'errorUpdate', @@ -353,6 +354,7 @@ export declare interface CatCheckbox extends Components.CatCheckbox { inputs: [ 'autoComplete', 'clearable', + 'dayNativeAttributes', 'dayVariants', 'disabled', 'errorUpdate', @@ -421,6 +423,7 @@ export declare interface CatDate extends Components.CatDate { @ProxyCmp({ inputs: [ 'a11yLabel', + 'dayNativeAttributes', 'dayVariants', 'hint', 'identifier', @@ -445,6 +448,7 @@ export declare interface CatDate extends Components.CatDate { // eslint-disable-next-line @angular-eslint/no-inputs-metadata-property inputs: [ 'a11yLabel', + 'dayNativeAttributes', 'dayVariants', 'hint', 'identifier', diff --git a/core/src/components.d.ts b/core/src/components.d.ts index a6182a4c..a7b79ced 100644 --- a/core/src/components.d.ts +++ b/core/src/components.d.ts @@ -386,6 +386,10 @@ export namespace Components { * @default false */ "clearable": boolean; + /** + * Attributes for native day buttons, keyed by local ISO date (YYYY-MM-DD). + */ + "dayNativeAttributes"?: { [date: string]: { [key: string]: string } }; /** * Maps local ISO dates (YYYY-MM-DD) to semantic theme variants, e.g. `{ '2026-10-20': 'danger' }`. */ @@ -517,6 +521,10 @@ export namespace Components { * Clear the picker. */ "clear": () => Promise; + /** + * Attributes for native day buttons, keyed by local ISO date (YYYY-MM-DD). + */ + "dayNativeAttributes"?: { [date: string]: { [key: string]: string } }; /** * Maps local ISO dates (YYYY-MM-DD) to semantic theme variants, e.g. `{ '2026-10-20': 'danger' }`. */ @@ -3407,6 +3415,10 @@ declare namespace LocalJSX { * @default false */ "clearable"?: boolean; + /** + * Attributes for native day buttons, keyed by local ISO date (YYYY-MM-DD). + */ + "dayNativeAttributes"?: { [date: string]: { [key: string]: string } }; /** * Maps local ISO dates (YYYY-MM-DD) to semantic theme variants, e.g. `{ '2026-10-20': 'danger' }`. */ @@ -3541,6 +3553,10 @@ declare namespace LocalJSX { * Adds accessible label for the datepicker. The label will be read by assistive technology upon focusing the datepicker. */ "a11yLabel"?: string; + /** + * Attributes for native day buttons, keyed by local ISO date (YYYY-MM-DD). + */ + "dayNativeAttributes"?: { [date: string]: { [key: string]: string } }; /** * Maps local ISO dates (YYYY-MM-DD) to semantic theme variants, e.g. `{ '2026-10-20': 'danger' }`. */ diff --git a/core/src/components/cat-date-inline/cat-date-inline.e2e.tsx b/core/src/components/cat-date-inline/cat-date-inline.e2e.tsx index e7283da6..6cb2de7c 100644 --- a/core/src/components/cat-date-inline/cat-date-inline.e2e.tsx +++ b/core/src/components/cat-date-inline/cat-date-inline.e2e.tsx @@ -1,9 +1,49 @@ import { describe, it, expect } from 'vitest'; import { render, h } from '@stencil/vitest'; +import { userEvent } from '@vitest/browser/context'; describe('cat-date-inline', () => { it('renders', async () => { const { root } = await render(); await expect.element(root).toHaveClass('hydrated'); }); + + it('supports day descriptions during keyboard navigation', async () => { + const { root } = await render( + + ); + const picker = root as HTMLCatDateInlineElement; + const next = root.shadowRoot!.querySelector('[data-date="2026-10-21"]')!; + const button = next.shadowRoot!.querySelector('button')!; + + await picker.doFocus(); + await userEvent.keyboard('{ArrowRight}'); + expect(root.shadowRoot!.activeElement).toBe(next); + expect(picker.value).toBe('2026-10-20'); + await expect.element(button).toHaveAccessibleName(next.a11yLabel); + await expect.element(button).toHaveAccessibleDescription('Special day'); + await userEvent.keyboard('{Enter}'); + expect(picker.value).toBe('2026-10-21'); + }); + + it('updates day descriptions without moving focus', async () => { + const { root } = await render(); + const picker = root as HTMLCatDateInlineElement; + const selected = root.shadowRoot!.querySelector('[data-date="2026-10-20"]')!; + const button = selected.shadowRoot!.querySelector('button')!; + await picker.doFocus(); + + picker.dayNativeAttributes = { '2026-10-20': { 'aria-description': 'Special day' } }; + await expect.element(button).toHaveAccessibleDescription('Special day'); + expect(root.shadowRoot!.activeElement).toBe(selected); + + picker.dayNativeAttributes = {}; + await expect.element(button).not.toHaveAttribute('aria-description'); + expect(root.shadowRoot!.activeElement).toBe(selected); + }); }); diff --git a/core/src/components/cat-date-inline/cat-date-inline.spec.tsx b/core/src/components/cat-date-inline/cat-date-inline.spec.tsx index d650610f..a9c52a54 100644 --- a/core/src/components/cat-date-inline/cat-date-inline.spec.tsx +++ b/core/src/components/cat-date-inline/cat-date-inline.spec.tsx @@ -10,6 +10,7 @@ vi.mock('../cat-i18n/cat-i18n-registry', () => ({ })); import '../cat-input/cat-input'; +import '../cat-button/cat-button'; import './cat-date-inline'; describe('cat-date-inline', () => { @@ -48,6 +49,24 @@ describe('cat-date-inline', () => { ); }); + it('passes day attributes while preserving labels and tab order', async () => { + const { root } = await render( + + ); + const selected = root.shadowRoot?.querySelector('[data-date="2026-10-20"]'); + const next = root.shadowRoot?.querySelector('[data-date="2026-10-21"]'); + + expect(selected?.a11yLabel).toBe('Tuesday, October 20, 2026'); + expect(selected?.nativeAttributes).toEqual({ 'aria-description': 'Special day', tabindex: '0' }); + expect(next?.nativeAttributes).toEqual({ tabindex: '-1' }); + }); + it('does not emit a range change when day variants trigger a render', async () => { const { root, waitForChanges } = await render(); const viewChange = vi.fn(); diff --git a/core/src/components/cat-date-inline/cat-date-inline.tsx b/core/src/components/cat-date-inline/cat-date-inline.tsx index ea3c1d7a..4eaefd9e 100644 --- a/core/src/components/cat-date-inline/cat-date-inline.tsx +++ b/core/src/components/cat-date-inline/cat-date-inline.tsx @@ -110,6 +110,11 @@ export class CatDateInline { */ @Prop({ mutable: true }) value?: string; + /** + * Attributes for native day buttons, keyed by local ISO date (YYYY-MM-DD). + */ + @Prop() dayNativeAttributes?: { [date: string]: { [key: string]: string } }; + /** * Maps local ISO dates (YYYY-MM-DD) to semantic theme variants, e.g. `{ '2026-10-20': 'danger' }`. */ @@ -361,7 +366,10 @@ export class CatDateInline { 'date-focusable': this.canFocus(day), 'date-disabled': !this.canClick(day) }} - nativeAttributes={!this.canFocus(day) ? { tabindex: '-1' } : {}} + nativeAttributes={{ + ...this.dayNativeAttributes?.[date], + tabindex: this.canFocus(day) ? '0' : '-1' + }} variant={isStartDate || isEndDate ? 'filled' : isToday ? 'outlined' : 'text'} a11yLabel={this.locale.toLocalStr(day)} active={isStartDate || isEndDate || isRange} diff --git a/core/src/components/cat-date-inline/readme.md b/core/src/components/cat-date-inline/readme.md index d000dcaf..e5ef2c63 100644 --- a/core/src/components/cat-date-inline/readme.md +++ b/core/src/components/cat-date-inline/readme.md @@ -34,6 +34,7 @@ An inline date picker component to select a date. | Property | Attribute | Description | Type | Default | | ---------------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | ------------ | | `a11yLabel` | `a11y-label` | Adds accessible label for the datepicker. The label will be read by assistive technology upon focusing the datepicker. | `string \| undefined` | `undefined` | +| `dayNativeAttributes` | -- | Attributes for native day buttons, keyed by local ISO date (YYYY-MM-DD). | `undefined \| { [date: string]: { [key: string]: string; }; }` | `undefined` | | `dayVariants` | -- | Maps local ISO dates (YYYY-MM-DD) to semantic theme variants, e.g. `{ '2026-10-20': 'danger' }`. | `undefined \| { [date: string]: CatDateDayVariant; }` | `undefined` | | `hint` | `hint` | Shows an arrow keys navigation hint. | `boolean` | `false` | | `identifier` | `identifier` | A unique identifier for the input. | `string \| undefined` | `undefined` | diff --git a/core/src/components/cat-date/cat-date.spec.tsx b/core/src/components/cat-date/cat-date.spec.tsx index 5967d3b0..51c3a2b4 100644 --- a/core/src/components/cat-date/cat-date.spec.tsx +++ b/core/src/components/cat-date/cat-date.spec.tsx @@ -23,6 +23,13 @@ describe('cat-date', () => { expect(root.shadowRoot?.querySelector('cat-date-inline')?.dayVariants).toEqual(dayVariants); }); + it('forwards day native attributes to the inline date picker', async () => { + const dayNativeAttributes = { '2026-10-20': { 'aria-description': 'Special day' } }; + const { root } = await render(); + + expect(root.shadowRoot?.querySelector('cat-date-inline')?.dayNativeAttributes).toEqual(dayNativeAttributes); + }); + it('forwards the visible date range from cat-date-inline', async () => { const { root } = await render(); const viewChange = vi.fn(); diff --git a/core/src/components/cat-date/cat-date.tsx b/core/src/components/cat-date/cat-date.tsx index a521e01e..648643fa 100644 --- a/core/src/components/cat-date/cat-date.tsx +++ b/core/src/components/cat-date/cat-date.tsx @@ -135,6 +135,11 @@ export class CatDate { */ @Prop({ mutable: true }) value?: string; + /** + * Attributes for native day buttons, keyed by local ISO date (YYYY-MM-DD). + */ + @Prop() dayNativeAttributes?: { [date: string]: { [key: string]: string } }; + /** * Maps local ISO dates (YYYY-MM-DD) to semantic theme variants, e.g. `{ '2026-10-20': 'danger' }`. */ @@ -323,6 +328,7 @@ export class CatDate { max={this.max} value={this.value} dayVariants={this.dayVariants} + dayNativeAttributes={this.dayNativeAttributes} hint weeks noClear diff --git a/core/src/components/cat-date/readme.md b/core/src/components/cat-date/readme.md index 63e6afc1..3b4a661c 100644 --- a/core/src/components/cat-date/readme.md +++ b/core/src/components/cat-date/readme.md @@ -35,6 +35,7 @@ A date input component to select a date from a calendar in a dropdown. | ------------------ | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------- | | `autoComplete` | `auto-complete` | Hint for form autofill feature. | `string` | `'off'` | | `clearable` | `clearable` | Whether the input should show a clear button. | `boolean` | `false` | +| `dayNativeAttributes` | -- | Attributes for native day buttons, keyed by local ISO date (YYYY-MM-DD). | `undefined \| { [date: string]: { [key: string]: string; }; }` | `undefined` | | `dayVariants` | -- | Maps local ISO dates (YYYY-MM-DD) to semantic theme variants, e.g. `{ '2026-10-20': 'danger' }`. | `undefined \| { [date: string]: CatDateDayVariant; }` | `undefined` | | `disabled` | `disabled` | Whether the input is disabled. | `boolean` | `false` | | `errorUpdate` | `error-update` | Fine-grained control over when the errors are shown. Can be `false` to never show errors, `true` to show errors on blur, or a number to show errors change with the given delay in milliseconds or immediately on blur. | `boolean \| number` | `0` |