Skip to content
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

fix(datepicker-toggle): forward tabindex to underlying button #12461

Merged
merged 1 commit into from
Aug 24, 2018
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
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 @@ -986,6 +986,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 @@ -1853,3 +1873,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 {}