Skip to content

Commit 479618e

Browse files
crisbetoandrewseguin
authored andcommitted
fix(slide-toggle): able to receive focus while disabled on click (#15501)
Along the same lines as #15499. Fixes the slide toggle being focusable via click when it's disabled.
1 parent f2f1232 commit 479618e

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/lib/slide-toggle/slide-toggle.spec.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,24 @@ describe('MatSlideToggle without forms', () => {
368368
.toBe(5, 'Expected tabIndex property to have been set based on the native attribute');
369369
}));
370370

371-
it('should clear the tabindex from the host element', fakeAsync(() => {
371+
it('should set the tabindex of the host element to -1', fakeAsync(() => {
372372
const fixture = TestBed.createComponent(SlideToggleWithTabindexAttr);
373373

374374
fixture.detectChanges();
375375

376376
const slideToggle = fixture.debugElement.query(By.directive(MatSlideToggle)).nativeElement;
377377
expect(slideToggle.getAttribute('tabindex')).toBe('-1');
378378
}));
379+
380+
it('should remove the tabindex from the host element when disabled', fakeAsync(() => {
381+
const fixture = TestBed.createComponent(SlideToggleWithTabindexAttr);
382+
383+
fixture.componentInstance.disabled = true;
384+
fixture.detectChanges();
385+
386+
const slideToggle = fixture.debugElement.query(By.directive(MatSlideToggle)).nativeElement;
387+
expect(slideToggle.hasAttribute('tabindex')).toBe(false);
388+
}));
379389
});
380390

381391
describe('custom action configuration', () => {
@@ -1119,10 +1129,10 @@ class SlideToggleWithFormControl {
11191129
formControl = new FormControl();
11201130
}
11211131

1122-
@Component({
1123-
template: `<mat-slide-toggle tabindex="5"></mat-slide-toggle>`
1124-
})
1125-
class SlideToggleWithTabindexAttr {}
1132+
@Component({template: `<mat-slide-toggle tabindex="5" [disabled]="disabled"></mat-slide-toggle>`})
1133+
class SlideToggleWithTabindexAttr {
1134+
disabled = false;
1135+
}
11261136

11271137
@Component({
11281138
template: `<mat-slide-toggle>{{label}}</mat-slide-toggle>`

src/lib/slide-toggle/slide-toggle.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ export const _MatSlideToggleMixinBase:
8686
host: {
8787
'class': 'mat-slide-toggle',
8888
'[id]': 'id',
89-
'[attr.tabindex]': '-1', // Needs to be `-1` so it can still receive programmatic focus.
89+
// Needs to be `-1` so it can still receive programmatic focus.
90+
'[attr.tabindex]': 'disabled ? null : -1',
9091
'[class.mat-checked]': 'checked',
9192
'[class.mat-disabled]': 'disabled',
9293
'[class.mat-slide-toggle-label-before]': 'labelPosition == "before"',
@@ -101,8 +102,10 @@ export const _MatSlideToggleMixinBase:
101102
changeDetection: ChangeDetectionStrategy.OnPush,
102103
})
103104
export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestroy, AfterContentInit,
104-
ControlValueAccessor, CanDisable, CanColor, HasTabIndex, CanDisableRipple {
105-
105+
ControlValueAccessor,
106+
CanDisable, CanColor,
107+
HasTabIndex,
108+
CanDisableRipple {
106109
private onChange = (_: any) => {};
107110
private onTouched = () => {};
108111

0 commit comments

Comments
 (0)