Skip to content

Commit 648eb4a

Browse files
crisbetojelbourn
authored andcommitted
fix(datepicker-toggle): forward tabindex to underlying button (#12461)
Forwards the tabindex of a `mat-button-toggle` to its underlying `button` and clears it from the host element. Fixes #12456.
1 parent 32456e3 commit 648eb4a

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/lib/datepicker/datepicker-toggle.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
type="button"
44
aria-haspopup="true"
55
[attr.aria-label]="_intl.openCalendarLabel"
6+
[attr.tabindex]="disabled ? -1 : tabIndex"
67
[disabled]="disabled"
78
(click)="_open($event)">
89

src/lib/datepicker/datepicker-toggle.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1010
import {
1111
AfterContentInit,
12+
Attribute,
1213
ChangeDetectionStrategy,
1314
ChangeDetectorRef,
1415
Component,
@@ -39,6 +40,8 @@ export class MatDatepickerToggleIcon {}
3940
styleUrls: ['datepicker-toggle.css'],
4041
host: {
4142
'class': 'mat-datepicker-toggle',
43+
// Clear out the native tabindex here since we forward it to the underlying button
44+
'[attr.tabindex]': 'null',
4245
'[class.mat-datepicker-toggle-active]': 'datepicker && datepicker.opened',
4346
'[class.mat-accent]': 'datepicker && datepicker.color === "accent"',
4447
'[class.mat-warn]': 'datepicker && datepicker.color === "warn"',
@@ -53,6 +56,9 @@ export class MatDatepickerToggle<D> implements AfterContentInit, OnChanges, OnDe
5356
/** Datepicker instance that the button will toggle. */
5457
@Input('for') datepicker: MatDatepicker<D>;
5558

59+
/** Tabindex for the toggle. */
60+
@Input() tabIndex: number | null;
61+
5662
/** Whether the toggle button is disabled. */
5763
@Input()
5864
get disabled(): boolean {
@@ -66,7 +72,14 @@ export class MatDatepickerToggle<D> implements AfterContentInit, OnChanges, OnDe
6672
/** Custom icon set by the consumer. */
6773
@ContentChild(MatDatepickerToggleIcon) _customIcon: MatDatepickerToggleIcon;
6874

69-
constructor(public _intl: MatDatepickerIntl, private _changeDetectorRef: ChangeDetectorRef) {}
75+
constructor(
76+
public _intl: MatDatepickerIntl,
77+
private _changeDetectorRef: ChangeDetectorRef,
78+
@Attribute('tabindex') defaultTabIndex: string) {
79+
80+
const parsedTabIndex = Number(defaultTabIndex);
81+
this.tabIndex = (parsedTabIndex || parsedTabIndex === 0) ? parsedTabIndex : null;
82+
}
7083

7184
ngOnChanges(changes: SimpleChanges) {
7285
if (changes.datepicker) {

src/lib/datepicker/datepicker.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,26 @@ describe('MatDatepicker', () => {
10081008
}));
10091009
});
10101010

1011+
describe('datepicker with tabindex on mat-datepicker-toggle', () => {
1012+
it('should forward the tabindex to the underlying button', () => {
1013+
const fixture = createComponent(DatepickerWithTabindexOnToggle, [MatNativeDateModule]);
1014+
fixture.detectChanges();
1015+
1016+
const button = fixture.nativeElement.querySelector('.mat-datepicker-toggle button');
1017+
1018+
expect(button.getAttribute('tabindex')).toBe('7');
1019+
});
1020+
1021+
it('should clear the tabindex from the mat-datepicker-toggle host', () => {
1022+
const fixture = createComponent(DatepickerWithTabindexOnToggle, [MatNativeDateModule]);
1023+
fixture.detectChanges();
1024+
1025+
const host = fixture.nativeElement.querySelector('.mat-datepicker-toggle');
1026+
1027+
expect(host.hasAttribute('tabindex')).toBe(false);
1028+
});
1029+
});
1030+
10111031
describe('datepicker inside mat-form-field', () => {
10121032
let fixture: ComponentFixture<FormFieldDatepicker>;
10131033
let testComponent: FormFieldDatepicker;
@@ -1875,3 +1895,16 @@ class DelayedDatepicker {
18751895
date: Date | null;
18761896
assignedDatepicker: MatDatepicker<Date>;
18771897
}
1898+
1899+
1900+
1901+
@Component({
1902+
template: `
1903+
<input [matDatepicker]="d">
1904+
<mat-datepicker-toggle tabIndex="7" [for]="d">
1905+
<div class="custom-icon" matDatepickerToggleIcon></div>
1906+
</mat-datepicker-toggle>
1907+
<mat-datepicker #d></mat-datepicker>
1908+
`,
1909+
})
1910+
class DatepickerWithTabindexOnToggle {}

0 commit comments

Comments
 (0)