Skip to content

Commit 027df6a

Browse files
committed
fix(slide-toggle): able to receive focus while disabled on click
Along the same lines as #15499. Fixes the slide toggle being focusable via click when it's disabled.
1 parent 3b0f7fc commit 027df6a

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

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

Lines changed: 20 additions & 10 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(() => {
372-
const fixture = TestBed.createComponent(SlideToggleWithTabindexAttr);
371+
it('should set the tabindex of the host element to -1', fakeAsync(() => {
372+
const fixture = TestBed.createComponent(SlideToggleWithTabindexAttr);
373373

374-
fixture.detectChanges();
374+
fixture.detectChanges();
375375

376-
const slideToggle = fixture.debugElement.query(By.directive(MatSlideToggle)).nativeElement;
377-
expect(slideToggle.getAttribute('tabindex')).toBe('-1');
378-
}));
376+
const slideToggle = fixture.debugElement.query(By.directive(MatSlideToggle)).nativeElement;
377+
expect(slideToggle.getAttribute('tabindex')).toBe('-1');
378+
}));
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', () => {
@@ -1124,10 +1134,10 @@ class SlideToggleWithFormControl {
11241134
formControl = new FormControl();
11251135
}
11261136

1127-
@Component({
1128-
template: `<mat-slide-toggle tabindex="5"></mat-slide-toggle>`
1129-
})
1130-
class SlideToggleWithTabindexAttr {}
1137+
@Component({template: `<mat-slide-toggle tabindex="5" [disabled]="disabled"></mat-slide-toggle>`})
1138+
class SlideToggleWithTabindexAttr {
1139+
disabled = false;
1140+
}
11311141

11321142
@Component({
11331143
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)