Skip to content

fix(datepicker): able to focus disabled datepicker toggle while disabled via click #18231

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 1 commit into from
Jan 22, 2020
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
2 changes: 1 addition & 1 deletion src/material/datepicker/datepicker-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class MatDatepickerToggleIcon {}
'class': 'mat-datepicker-toggle',
// 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',
'[attr.tabindex]': 'disabled ? null : -1',
'[class.mat-datepicker-toggle-active]': 'datepicker && datepicker.opened',
'[class.mat-accent]': 'datepicker && datepicker.color === "accent"',
'[class.mat-warn]': 'datepicker && datepicker.color === "warn"',
Expand Down
18 changes: 15 additions & 3 deletions src/material/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ describe('MatDatepicker', () => {
expect(button.getAttribute('tabindex')).toBe('7');
});

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

Expand All @@ -1091,6 +1091,16 @@ describe('MatDatepicker', () => {
expect(document.activeElement).toBe(button);
});

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

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

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

});

describe('datepicker inside mat-form-field', () => {
Expand Down Expand Up @@ -2037,13 +2047,15 @@ class DelayedDatepicker {
@Component({
template: `
<input [matDatepicker]="d">
<mat-datepicker-toggle tabIndex="7" [for]="d">
<mat-datepicker-toggle tabIndex="7" [for]="d" [disabled]="disabled">
<div class="custom-icon" matDatepickerToggleIcon></div>
</mat-datepicker-toggle>
<mat-datepicker #d></mat-datepicker>
`,
})
class DatepickerWithTabindexOnToggle {}
class DatepickerWithTabindexOnToggle {
disabled = false;
}


@Component({
Expand Down