Skip to content

Commit 22860d1

Browse files
author
esuau
committed
fix(switch): #197 Use ngClass instead of renderer
1 parent b1811dd commit 22860d1

File tree

2 files changed

+9
-34
lines changed

2 files changed

+9
-34
lines changed

src/switch/switch.component.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,14 @@ describe("SwitchComponent", () => {
4343
});
4444

4545
it("should display small version of switch when size equals sm", () => {
46+
expect(buttonElement.className.includes("bx--toggle--small")).toEqual(false);
4647
component.size = "sm";
47-
component.ngOnInit();
4848
fixture.detectChanges();
4949
expect(buttonElement.className.includes("bx--toggle--small")).toEqual(true);
5050
});
5151

5252
it("should display SVG in small version of switch", () => {
5353
component.size = "sm";
54-
component.ngOnInit();
5554
fixture.detectChanges();
5655
labelElement = fixture.debugElement.query(By.css("label")).nativeElement;
5756
expect(fixture.debugElement.query(By.css("svg")).nativeElement).not.toBeNull();

src/switch/switch.component.ts

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
import { CheckboxComponent } from "../checkbox/checkbox.component";
22
import {
3-
ChangeDetectionStrategy,
43
ChangeDetectorRef,
54
Component,
6-
ElementRef,
7-
EventEmitter,
8-
forwardRef,
95
Input,
10-
OnInit,
11-
Output,
12-
Renderer2,
13-
ViewChild
6+
OnInit
147
} from "@angular/core";
15-
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from "@angular/forms";
8+
import { NG_VALUE_ACCESSOR } from "@angular/forms";
169

1710

1811
/**
@@ -60,7 +53,10 @@ export class SwitchChange {
6053
selector: "ibm-switch",
6154
template: `
6255
<input
63-
class="bx--toggle"
56+
[ngClass]="{
57+
'bx--toggle': true,
58+
'bx--toggle--small': size === 'sm'
59+
}"
6460
[id]="id"
6561
type="checkbox"
6662
(click)="onClick($event)"
@@ -114,11 +110,9 @@ export class SwitchComponent extends CheckboxComponent implements OnInit {
114110
/**
115111
* Creates an instance of SwitchComponent.
116112
* @param {ChangeDetectorRef} changeDetectorRef
117-
* @param {ElementRef} elementRef
118-
* @param {Renderer2} renderer
119113
* @memberof SwitchComponent
120114
*/
121-
constructor(protected changeDetectorRef: ChangeDetectorRef, private elementRef: ElementRef, private renderer: Renderer2) {
115+
constructor(protected changeDetectorRef: ChangeDetectorRef) {
122116
super(changeDetectorRef);
123117
SwitchComponent.switchCount++;
124118
}
@@ -127,23 +121,5 @@ export class SwitchComponent extends CheckboxComponent implements OnInit {
127121
* Builds variant classes and appends them to the switch and label elements.
128122
* @memberof SwitchComponent
129123
*/
130-
ngOnInit() {
131-
/* TODO: remove and extend in neutrino
132-
// Build variant classes
133-
const labelClass = `toggle-label${this.size !== "md" ? `--${this.size}` : ""}`;
134-
const buttonClass = `toggle${this.size !== "md" ? `--${this.size}` : ""}`;
135-
136-
// Get elements
137-
const labelEl = this.elementRef.nativeElement.querySelector("label");
138-
const buttonEl = this.elementRef.nativeElement.querySelector("button");
139-
140-
// Add classes to elements
141-
this.renderer.addClass(labelEl, labelClass);
142-
this.renderer.addClass(buttonEl, buttonClass);
143-
*/
144-
if (this.size === "sm") {
145-
const inputEl = this.elementRef.nativeElement.querySelector("input");
146-
this.renderer.addClass(inputEl, "bx--toggle--small");
147-
}
148-
}
124+
ngOnInit() { }
149125
}

0 commit comments

Comments
 (0)