Skip to content

Commit

Permalink
refactor: improve keyboard navigation in year grid
Browse files Browse the repository at this point in the history
  • Loading branch information
motss committed May 26, 2021
1 parent c253e9e commit 8ecbf25
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
4 changes: 0 additions & 4 deletions src/date-picker/date-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ export class DatePicker extends DatePickerMixin(DatePickerMinMaxMixin(LitElement
@state()
private _selectedDate: Date;

// @state()
// private startView: CalendarView = 'calendar';
//#endregion private states

//#region private properties
#formatters: Formatters;
private _TODAY_DATE: Date;
Expand Down
23 changes: 12 additions & 11 deletions src/year-grid/year-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,21 @@ export class YearGrid extends LitElement implements YearGridProperties {
return this.data.formatters != null;
}

public willUpdate(changedProperties: YearGridChangedProperties): void {
if (changedProperties.has('data')) {
this.#selectedYear = this.data.date.getUTCFullYear();
}
}

protected async firstUpdated(): Promise<void> {
const selectedYearGridButton = await this.selectedYearGridButton;

if (selectedYearGridButton) {
selectedYearGridButton.focus();
selectedYearGridButton.scrollIntoView();
}
}

public willUpdate(changedProperties: YearGridChangedProperties): void {
if (changedProperties.has('data')) {
this.#selectedYear = this.data.date.getUTCFullYear();
}
}

protected render(): TemplateResult | typeof nothing {
const {
date,
Expand Down Expand Up @@ -110,8 +111,6 @@ export class YearGrid extends LitElement implements YearGridProperties {
}

#updateYear = (ev: MouseEvent | KeyboardEvent): void => {


if (['keydown', 'keyup'].includes(ev.type)) {
const { keyCode } = ev as KeyboardEvent;
const keyCodeNum = keyCode as typeof keyCode extends Set<infer U> ? U : never;
Expand All @@ -120,6 +119,9 @@ export class YearGrid extends LitElement implements YearGridProperties {
!(yearGridNavigationKeyCodeSet.has(keyCodeNum) && ev.type === 'keydown')
) return;

// Stop scrolling with arrow keys
ev.preventDefault();

// Focus new year with Home, End, and arrow keys
const {
max,
Expand All @@ -136,12 +138,11 @@ export class YearGrid extends LitElement implements YearGridProperties {
`button[data-year="${selectedYear}"]`
);

this.#selectedYear = selectedYear;

if (selectedYearGridButton) {
selectedYearGridButton.focus();
selectedYearGridButton.scrollIntoView();
}

this.#selectedYear = selectedYear;
} else {
const selectedYearGridButton = toClosestTarget(ev, `button[data-year]`);

Expand Down

0 comments on commit 8ecbf25

Please sign in to comment.