Skip to content

fix(material-experimental/mdc-slide-toggle): initial checked and disabled state not reflected visually #17991

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
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
4 changes: 4 additions & 0 deletions src/dev-app/mdc-slide-toggle/mdc-slide-toggle-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
Disabled Slide Toggle
</mat-slide-toggle>

<mat-slide-toggle disabled>
Always disabled (no value binding)
</mat-slide-toggle>

<mat-slide-toggle [disabled]="firstToggle">
Disable Bound
</mat-slide-toggle>
Expand Down
6 changes: 2 additions & 4 deletions src/material-experimental/mdc-slide-toggle/slide-toggle.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="mdc-form-field"
[class.mdc-form-field--align-end]="labelPosition == 'before'">
<div [ngClass]="_classes" #switch>
<div class="mdc-switch" #switch>
<div class="mdc-switch__track"></div>
<div class="mdc-switch__thumb-underlay">
<div class="mat-mdc-slide-toggle-ripple" mat-ripple
Expand Down Expand Up @@ -28,9 +28,7 @@
</div>
</div>

<label #label
[for]="inputId"
(click)="$event.stopPropagation()">
<label [for]="inputId" (click)="$event.stopPropagation()">
<ng-content></ng-content>
</label>
</div>
22 changes: 22 additions & 0 deletions src/material-experimental/mdc-slide-toggle/slide-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('MDC-based MatSlideToggle without forms', () => {
imports: [MatSlideToggleModule, BidiModule],
declarations: [
SlideToggleBasic,
SlideToggleCheckedAndDisabledAttr,
SlideToggleWithTabindexAttr,
SlideToggleWithoutLabel,
SlideToggleProjectedLabel,
Expand Down Expand Up @@ -320,6 +321,22 @@ describe('MDC-based MatSlideToggle without forms', () => {
.toBe(5, 'Expected tabIndex property to have been set based on the native attribute');
}));

it('should add the disabled class if disabled through attribute', () => {
const fixture = TestBed.createComponent(SlideToggleCheckedAndDisabledAttr);
fixture.detectChanges();

const switchEl = fixture.nativeElement.querySelector('.mdc-switch');
expect(switchEl.classList).toContain('mdc-switch--disabled');
});

it('should add the checked class if checked through attribute', () => {
const fixture = TestBed.createComponent(SlideToggleCheckedAndDisabledAttr);
fixture.detectChanges();

const switchEl = fixture.nativeElement.querySelector('.mdc-switch');
expect(switchEl.classList).toContain('mdc-switch--checked');
});

it('should remove the tabindex from the host element', fakeAsync(() => {
const fixture = TestBed.createComponent(SlideToggleWithTabindexAttr);

Expand Down Expand Up @@ -772,6 +789,11 @@ class SlideToggleWithModel {
isDisabled = false;
}

@Component({
template: `<mat-slide-toggle checked disabled>Label</mat-slide-toggle>`
})
class SlideToggleCheckedAndDisabledAttr {}

@Component({
template: `
<mat-slide-toggle [formControl]="formControl">
Expand Down
28 changes: 7 additions & 21 deletions src/material-experimental/mdc-slide-toggle/slide-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,15 @@ export class MatSlideToggle implements ControlValueAccessor, AfterViewInit, OnDe
private _checked: boolean = false;
private _foundation: MDCSwitchFoundation;
private _adapter: MDCSwitchAdapter = {
addClass: (className) => {
this._toggleClass(className, true);
},
removeClass: (className) => {
this._toggleClass(className, false);
},
setNativeControlChecked: (checked) => {
this._checked = checked;
},
setNativeControlDisabled: (disabled) => {
this._disabled = disabled;
},
addClass: className => this._switchElement.nativeElement.classList.add(className),
removeClass: className => this._switchElement.nativeElement.classList.remove(className),
setNativeControlChecked: checked => this._checked = checked,
setNativeControlDisabled: disabled => this._disabled = disabled,
};

/** Whether the slide toggle is currently focused. */
_focused: boolean;

/** The set of classes that should be applied to the native input. */
_classes: {[key: string]: boolean} = {'mdc-switch': true};

/** Configuration for the underlying ripple. */
_rippleAnimation: RippleAnimationConfig = {
enterDuration: numbers.DEACTIVATION_TIMEOUT_MS,
Expand Down Expand Up @@ -206,6 +195,9 @@ export class MatSlideToggle implements ControlValueAccessor, AfterViewInit, OnDe
/** Reference to the underlying input element. */
@ViewChild('input') _inputElement: ElementRef<HTMLInputElement>;

/** Reference to the MDC switch element. */
@ViewChild('switch') _switchElement: ElementRef<HTMLElement>;

constructor(private _changeDetectorRef: ChangeDetectorRef,
@Attribute('tabindex') tabIndex: string,
@Inject(MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS)
Expand Down Expand Up @@ -311,12 +303,6 @@ export class MatSlideToggle implements ControlValueAccessor, AfterViewInit, OnDe
});
}

/** Toggles a class on the switch element. */
private _toggleClass(cssClass: string, active: boolean) {
this._classes[cssClass] = active;
this._changeDetectorRef.markForCheck();
}

static ngAcceptInputType_tabIndex: NumberInput;
static ngAcceptInputType_required: BooleanInput;
static ngAcceptInputType_checked: BooleanInput;
Expand Down