Skip to content

Commit be171de

Browse files
authored
Merge pull request #197 from esuau/feat/switch
feat(switch): Add small version of swich component
2 parents 36427b8 + 9d74b69 commit be171de

File tree

4 files changed

+48
-41
lines changed

4 files changed

+48
-41
lines changed

src/dialog/tooltip/tooltip.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ storiesOf("Tooltip", module)
99
.addDecorator(
1010
moduleMetadata({
1111
imports: [
12-
DialogModule,
12+
DialogModule
1313
]
1414
})
1515
)

src/switch/switch.component.spec.ts

Lines changed: 28 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,15 +23,22 @@ 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

3031
it("should work", () => {
3132
expect(component instanceof SwitchComponent).toBe(true);
3233
});
3334

35+
it("should have input with class 'bx--toggle'", () => {
36+
expect(buttonElement.className.includes("bx--toggle")).toEqual(true);
37+
component.size = "sm";
38+
fixture.detectChanges();
39+
expect(buttonElement.className.includes("bx--toggle")).toEqual(true);
40+
});
41+
3442
it("should change state", () => {
3543
buttonElement.click();
3644
fixture.detectChanges();
@@ -40,4 +48,20 @@ describe("SwitchComponent", () => {
4048
fixture.detectChanges();
4149
expect(component.checked).toBe(false, "setting to off");
4250
});
51+
52+
it("should display small version of switch when size equals sm", () => {
53+
expect(buttonElement.className.includes("bx--toggle--small")).toEqual(false);
54+
component.size = "sm";
55+
fixture.detectChanges();
56+
expect(buttonElement.className.includes("bx--toggle--small")).toEqual(true);
57+
});
58+
59+
it("should display SVG in small version of switch", () => {
60+
component.size = "sm";
61+
fixture.detectChanges();
62+
labelElement = fixture.debugElement.query(By.css("label")).nativeElement;
63+
expect(fixture.debugElement.query(By.css("svg")).nativeElement).not.toBeNull();
64+
expect(labelElement.innerHTML).toContain("bx--toggle__check");
65+
});
66+
4367
});

src/switch/switch.component.ts

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

179

1810
/**
@@ -61,16 +53,26 @@ export class SwitchChange {
6153
template: `
6254
<input
6355
class="bx--toggle"
56+
[ngClass]="{
57+
'bx--toggle--small': size === 'sm'
58+
}"
6459
[id]="id"
6560
type="checkbox"
6661
(click)="onClick($event)"
6762
[disabled]="disabled"
6863
[attr.aria-checked]="checked">
69-
<label class="bx--toggle__label" [for]="id">
64+
<label *ngIf="size === 'md'" class="bx--toggle__label" [for]="id">
7065
<span class="bx--toggle__text--left">Off</span>
7166
<span class="bx--toggle__appearance"></span>
7267
<span class="bx--toggle__text--right">On</span>
7368
</label>
69+
<label *ngIf="size === 'sm'" class="bx--toggle__label" [for]="id">
70+
<span class="bx--toggle__appearance">
71+
<svg class="bx--toggle__check" width="6px" height="5px" viewBox="0 0 6 5">
72+
<path d="M2.2 2.7L5 0 6 1 2.2 5 0 2.7 1 1.5z"/>
73+
</svg>
74+
</span>
75+
</label>
7476
`,
7577
providers: [
7678
{
@@ -80,7 +82,7 @@ export class SwitchChange {
8082
}
8183
]
8284
})
83-
export class SwitchComponent extends CheckboxComponent implements OnInit {
85+
export class SwitchComponent extends CheckboxComponent {
8486
/**
8587
* Variable used for creating unique ids for switch components.
8688
* @type {number}
@@ -107,32 +109,10 @@ export class SwitchComponent extends CheckboxComponent implements OnInit {
107109
/**
108110
* Creates an instance of SwitchComponent.
109111
* @param {ChangeDetectorRef} changeDetectorRef
110-
* @param {ElementRef} elementRef
111-
* @param {Renderer2} renderer
112112
* @memberof SwitchComponent
113113
*/
114-
constructor(protected changeDetectorRef: ChangeDetectorRef, private elementRef: ElementRef, private renderer: Renderer2) {
114+
constructor(protected changeDetectorRef: ChangeDetectorRef) {
115115
super(changeDetectorRef);
116116
SwitchComponent.switchCount++;
117117
}
118-
119-
/**
120-
* Builds variant classes and appends them to the switch and label elements.
121-
* @memberof SwitchComponent
122-
*/
123-
ngOnInit() {
124-
/* TODO: remove and extend in neutrino
125-
// Build variant classes
126-
const labelClass = `toggle-label${this.size !== "md" ? `--${this.size}` : ""}`;
127-
const buttonClass = `toggle${this.size !== "md" ? `--${this.size}` : ""}`;
128-
129-
// Get elements
130-
const labelEl = this.elementRef.nativeElement.querySelector("label");
131-
const buttonEl = this.elementRef.nativeElement.querySelector("button");
132-
133-
// Add classes to elements
134-
this.renderer.addClass(labelEl, labelClass);
135-
this.renderer.addClass(buttonEl, buttonClass);
136-
*/
137-
}
138118
}

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 [disabled]="disabled" size="sm"></ibm-switch>
16+
`,
1417
props: {
1518
disabled: boolean("disabled", false)
1619
}

0 commit comments

Comments
 (0)