Skip to content

Commit

Permalink
fix(datepicker-toggle): forward tabindex to underlying button
Browse files Browse the repository at this point in the history
Forwards the tabindex of a `mat-button-toggle` to its underlying `button` and clears it from the host element.

Fixes #12456.
  • Loading branch information
crisbeto authored and jelbourn committed Aug 24, 2018
1 parent 89d16b2 commit 6f00c35
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/datepicker/datepicker-toggle.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
type="button"
aria-haspopup="true"
[attr.aria-label]="_intl.openCalendarLabel"
[attr.tabindex]="disabled ? -1 : tabIndex"
[disabled]="disabled"
(click)="_open($event)">

Expand Down
15 changes: 14 additions & 1 deletion src/lib/datepicker/datepicker-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {
AfterContentInit,
Attribute,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Expand Down Expand Up @@ -39,6 +40,8 @@ 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',
'[class.mat-datepicker-toggle-active]': 'datepicker && datepicker.opened',
'[class.mat-accent]': 'datepicker && datepicker.color === "accent"',
'[class.mat-warn]': 'datepicker && datepicker.color === "warn"',
Expand All @@ -53,6 +56,9 @@ export class MatDatepickerToggle<D> implements AfterContentInit, OnChanges, OnDe
/** Datepicker instance that the button will toggle. */
@Input('for') datepicker: MatDatepicker<D>;

/** Tabindex for the toggle. */
@Input() tabIndex: number | null;

/** Whether the toggle button is disabled. */
@Input()
get disabled(): boolean {
Expand All @@ -66,7 +72,14 @@ export class MatDatepickerToggle<D> implements AfterContentInit, OnChanges, OnDe
/** Custom icon set by the consumer. */
@ContentChild(MatDatepickerToggleIcon) _customIcon: MatDatepickerToggleIcon;

constructor(public _intl: MatDatepickerIntl, private _changeDetectorRef: ChangeDetectorRef) {}
constructor(
public _intl: MatDatepickerIntl,
private _changeDetectorRef: ChangeDetectorRef,
@Attribute('tabindex') defaultTabIndex: string) {

const parsedTabIndex = Number(defaultTabIndex);
this.tabIndex = (parsedTabIndex || parsedTabIndex === 0) ? parsedTabIndex : null;
}

ngOnChanges(changes: SimpleChanges) {
if (changes.datepicker) {
Expand Down
33 changes: 33 additions & 0 deletions src/lib/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,26 @@ describe('MatDatepicker', () => {
}));
});

describe('datepicker with tabindex on mat-datepicker-toggle', () => {
it('should forward the tabindex to the underlying button', () => {
const fixture = createComponent(DatepickerWithTabindexOnToggle, [MatNativeDateModule]);
fixture.detectChanges();

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

expect(button.getAttribute('tabindex')).toBe('7');
});

it('should clear the tabindex from the mat-datepicker-toggle host', () => {
const fixture = createComponent(DatepickerWithTabindexOnToggle, [MatNativeDateModule]);
fixture.detectChanges();

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

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

describe('datepicker inside mat-form-field', () => {
let fixture: ComponentFixture<FormFieldDatepicker>;
let testComponent: FormFieldDatepicker;
Expand Down Expand Up @@ -1875,3 +1895,16 @@ class DelayedDatepicker {
date: Date | null;
assignedDatepicker: MatDatepicker<Date>;
}



@Component({
template: `
<input [matDatepicker]="d">
<mat-datepicker-toggle tabIndex="7" [for]="d">
<div class="custom-icon" matDatepickerToggleIcon></div>
</mat-datepicker-toggle>
<mat-datepicker #d></mat-datepicker>
`,
})
class DatepickerWithTabindexOnToggle {}

0 comments on commit 6f00c35

Please sign in to comment.