Skip to content

fix(datepicker): toggle not forwarding focus to underlying button #14020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/datepicker/datepicker-toggle.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<button
#button
mat-icon-button
type="button"
aria-haspopup="true"
Expand Down
11 changes: 9 additions & 2 deletions src/lib/datepicker/datepicker-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import {
OnDestroy,
SimpleChanges,
ViewEncapsulation,
ViewChild,
} from '@angular/core';
import {MatButton} from '@angular/material/button';
import {merge, of as observableOf, Subscription} from 'rxjs';
import {MatDatepicker} from './datepicker';
import {MatDatepickerIntl} from './datepicker-intl';
Expand All @@ -40,11 +42,13 @@ export class MatDatepickerToggleIcon {}
styleUrls: ['datepicker-toggle.css'],
host: {
'class': 'mat-datepicker-toggle',
// Clear out the native tabindex here since we forward it to the underlying button
'[attr.tabindex]': 'null',
// Always set the tabindex to -1 so that it doesn't overlap with any custom tabindex the
// consumer may have provided, while still being able to receive focus.
'[attr.tabindex]': '-1',
'[class.mat-datepicker-toggle-active]': 'datepicker && datepicker.opened',
'[class.mat-accent]': 'datepicker && datepicker.color === "accent"',
'[class.mat-warn]': 'datepicker && datepicker.color === "warn"',
'(focus)': '_button.focus()',
},
exportAs: 'matDatepickerToggle',
encapsulation: ViewEncapsulation.None,
Expand Down Expand Up @@ -72,6 +76,9 @@ export class MatDatepickerToggle<D> implements AfterContentInit, OnChanges, OnDe
/** Custom icon set by the consumer. */
@ContentChild(MatDatepickerToggleIcon) _customIcon: MatDatepickerToggleIcon;

/** Underlying button element. */
@ViewChild('button') _button: MatButton;

constructor(
public _intl: MatDatepickerIntl,
private _changeDetectorRef: ChangeDetectorRef,
Expand Down
17 changes: 16 additions & 1 deletion src/lib/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1043,8 +1043,23 @@ describe('MatDatepicker', () => {

const host = fixture.nativeElement.querySelector('.mat-datepicker-toggle');

expect(host.hasAttribute('tabindex')).toBe(false);
expect(host.getAttribute('tabindex')).toBe('-1');
});

it('should forward focus to the underlying button when the host is focused', () => {
const fixture = createComponent(DatepickerWithTabindexOnToggle, [MatNativeDateModule]);
fixture.detectChanges();

const host = fixture.nativeElement.querySelector('.mat-datepicker-toggle');
const button = host.querySelector('button');

expect(document.activeElement).not.toBe(button);

host.focus();

expect(document.activeElement).toBe(button);
});

});

describe('datepicker inside mat-form-field', () => {
Expand Down