Skip to content

Commit 372253f

Browse files
author
esuau
committed
feat(switch): #68 Add small version of switch component
1 parent f1d4674 commit 372253f

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/switch/switch.component.spec.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import { DebugElement } from "@angular/core";
55
import { StaticIconModule } from "../icon/static-icon.module";
66

77
import { SwitchComponent } from "./switch.component";
8+
import { CheckboxComponent } from "../checkbox/checkbox.module";
89

910
describe("SwitchComponent", () => {
1011
let component: SwitchComponent;
1112
let fixture: ComponentFixture<SwitchComponent>;
12-
let de: DebugElement;
13-
let el: HTMLElement;
13+
let labelElement: HTMLElement;
1414
let buttonElement: HTMLElement;
15+
let svgElement: HTMLElement;
1516

1617
beforeEach(() => {
1718
TestBed.configureTestingModule({
@@ -22,8 +23,8 @@ describe("SwitchComponent", () => {
2223

2324
fixture = TestBed.createComponent(SwitchComponent);
2425
component = fixture.componentInstance;
25-
de = fixture.debugElement.query(By.css("label"));
26-
el = de.nativeElement;
26+
fixture.detectChanges();
27+
labelElement = fixture.debugElement.query(By.css("label")).nativeElement;
2728
buttonElement = fixture.debugElement.query(By.css("input")).nativeElement;
2829
});
2930

@@ -40,4 +41,21 @@ describe("SwitchComponent", () => {
4041
fixture.detectChanges();
4142
expect(component.checked).toBe(false, "setting to off");
4243
});
44+
45+
it("should display small version of switch when size equals sm", () => {
46+
component.size = "sm";
47+
component.ngOnInit();
48+
fixture.detectChanges();
49+
expect(buttonElement.className.includes("bx--toggle--small")).toEqual(true);
50+
});
51+
52+
it("should display SVG in small version of switch", () => {
53+
component.size = "sm";
54+
component.ngOnInit();
55+
fixture.detectChanges();
56+
labelElement = fixture.debugElement.query(By.css("label")).nativeElement;
57+
expect(fixture.debugElement.query(By.css("svg")).nativeElement).not.toBeNull();
58+
expect(labelElement.innerHTML).toContain("bx--toggle__check");
59+
});
60+
4361
});

src/switch/switch.component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,18 @@ export class SwitchChange {
6666
(click)="onClick($event)"
6767
[disabled]="disabled"
6868
[attr.aria-checked]="checked">
69-
<label class="bx--toggle__label" [for]="id">
69+
<label *ngIf="size === 'md'" class="bx--toggle__label" [for]="id">
7070
<span class="bx--toggle__text--left">Off</span>
7171
<span class="bx--toggle__appearance"></span>
7272
<span class="bx--toggle__text--right">On</span>
7373
</label>
74+
<label *ngIf="size === 'sm'" class="bx--toggle__label" [for]="id">
75+
<span class="bx--toggle__appearance">
76+
<svg class="bx--toggle__check" width="6px" height="5px" viewBox="0 0 6 5">
77+
<path d="M2.2 2.7L5 0 6 1 2.2 5 0 2.7 1 1.5z"/>
78+
</svg>
79+
</span>
80+
</label>
7481
`,
7582
providers: [
7683
{
@@ -134,5 +141,9 @@ export class SwitchComponent extends CheckboxComponent implements OnInit {
134141
this.renderer.addClass(labelEl, labelClass);
135142
this.renderer.addClass(buttonEl, buttonClass);
136143
*/
144+
if (this.size === "sm") {
145+
const inputEl = this.elementRef.nativeElement.querySelector("input");
146+
this.renderer.addClass(inputEl, "bx--toggle--small");
147+
}
137148
}
138149
}

src/switch/switch.stories.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ storiesOf("Switch", module).addDecorator(
1010
)
1111
.addDecorator(withKnobs)
1212
.add("Basic", () => ({
13-
template: `<ibm-switch [disabled]="disabled"></ibm-switch>`,
13+
template: `
14+
<ibm-switch [disabled]="disabled"></ibm-switch>
15+
<ibm-switch [size]="'sm'"></ibm-switch>
16+
`,
1417
props: {
1518
disabled: boolean("disabled", false)
1619
}

0 commit comments

Comments
 (0)