Skip to content

Commit ac6f162

Browse files
committed
fix(datepicker-toggle): forward tabindex to underlying button
Forwards the tabindex of a `mat-button-toggle` to its underlying `button` and clears it from the host element. Fixes #12456.
1 parent 877de56 commit ac6f162

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-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]="tabIndex"
67
[disabled]="disabled"
78
(click)="_open($event)">
89

src/lib/datepicker/datepicker-toggle.ts

Lines changed: 12 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,12 @@ 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+
this.tabIndex = Number(defaultTabIndex) || null;
80+
}
7081

7182
ngOnChanges(changes: SimpleChanges) {
7283
if (changes.datepicker) {

src/lib/datepicker/datepicker.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,26 @@ describe('MatDatepicker', () => {
935935
}));
936936
});
937937

938+
describe('datepicker with tabindex on mat-datepicker-toggle', () => {
939+
it('should forward the tabindex to the underlying button', () => {
940+
const fixture = createComponent(DatepickerWithTabindexOnToggle, [MatNativeDateModule]);
941+
fixture.detectChanges();
942+
943+
const button = fixture.nativeElement.querySelector('.mat-datepicker-toggle button');
944+
945+
expect(button.getAttribute('tabindex')).toBe('7');
946+
});
947+
948+
it('should clear the tabindex from the mat-datepicker-toggle host', () => {
949+
const fixture = createComponent(DatepickerWithTabindexOnToggle, [MatNativeDateModule]);
950+
fixture.detectChanges();
951+
952+
const host = fixture.nativeElement.querySelector('.mat-datepicker-toggle');
953+
954+
expect(host.hasAttribute('tabindex')).toBe(false);
955+
});
956+
});
957+
938958
describe('datepicker inside mat-form-field', () => {
939959
let fixture: ComponentFixture<FormFieldDatepicker>;
940960
let testComponent: FormFieldDatepicker;
@@ -1787,3 +1807,15 @@ class DatepickerWithCustomHeader {
17871807
`,
17881808
})
17891809
class CustomHeaderForDatepicker {}
1810+
1811+
1812+
@Component({
1813+
template: `
1814+
<input [matDatepicker]="d">
1815+
<mat-datepicker-toggle tabIndex="7" [for]="d">
1816+
<div class="custom-icon" matDatepickerToggleIcon></div>
1817+
</mat-datepicker-toggle>
1818+
<mat-datepicker #d></mat-datepicker>
1819+
`,
1820+
})
1821+
class DatepickerWithTabindexOnToggle {}

0 commit comments

Comments
 (0)