Skip to content

Commit 50f8504

Browse files
[DSC-1872] remove function from template, adapt tests
1 parent 358beb3 commit 50f8504

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

src/app/shared/switch/switch.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="btn-group">
2-
<div class="switch-container" [ngClass]="getBackgroundColorClass()">
2+
<div class="switch-container" [ngClass]="backgroundClass">
33
<div *ngFor="let option of options">
44
<div class="switch-opt"
55
(click)="onOptionClick(option.value)"

src/app/shared/switch/switch.component.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('SwitchComponent', () => {
4545

4646
it('should select an option and emit selected value', () => {
4747
component.options = mockOptions;
48-
component.selectedValue = mockOptions[0].value;
48+
component.onOptionClick(mockOptions[0].value);
4949
fixture.detectChanges();
5050

5151
spyOn(component.selectedValueChange, 'emit');
@@ -60,7 +60,7 @@ describe('SwitchComponent', () => {
6060

6161
it('should apply the correct background color class', () => {
6262
component.options = mockOptions;
63-
component.selectedValue = mockOptions[1].value;
63+
component.onOptionClick(mockOptions[1].value);
6464
fixture.detectChanges();
6565

6666
const containerElement = fixture.debugElement.query(By.css('.switch-container'));
@@ -69,7 +69,7 @@ describe('SwitchComponent', () => {
6969

7070
it('should apply the correct icon color class for selected option', () => {
7171
component.options = mockOptions;
72-
component.selectedValue = mockOptions[1].value;
72+
component.onOptionClick(mockOptions[1].value);
7373
fixture.detectChanges();
7474

7575
const iconElement = fixture.debugElement.query(By.css('.switch-opt .icon-2'));
@@ -78,7 +78,7 @@ describe('SwitchComponent', () => {
7878

7979
it('should display the correct label with the selected color', () => {
8080
component.options = mockOptions;
81-
component.selectedValue = mockOptions[1].value;
81+
component.onOptionClick(mockOptions[1].value);
8282
fixture.detectChanges();
8383

8484
const labelElement = fixture.debugElement.query(By.css('.visibility-label'));
@@ -88,7 +88,7 @@ describe('SwitchComponent', () => {
8888

8989
it('should apply bg-white class to selected option', () => {
9090
component.options = mockOptions;
91-
component.selectedValue = mockOptions[1].value;
91+
component.onOptionClick(mockOptions[1].value);
9292
fixture.detectChanges();
9393

9494
const selectedOptionElement = fixture.debugElement.query(By.css('.switch-opt.bg-white'));

src/app/shared/switch/switch.component.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Component, EventEmitter, Input, Output } from '@angular/core';
1+
import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges } from '@angular/core';
2+
import { hasValue } from '../empty.util';
23

34
export enum SwitchColor {
45
Primary = 'primary',
@@ -21,7 +22,7 @@ export interface SwitchOption {
2122
templateUrl: './switch.component.html',
2223
styleUrls: ['./switch.component.scss'],
2324
})
24-
export class SwitchComponent {
25+
export class SwitchComponent implements OnInit, OnChanges {
2526
/**
2627
* The options available for the switch
2728
*/
@@ -37,6 +38,24 @@ export class SwitchComponent {
3738
*/
3839
@Output() selectedValueChange = new EventEmitter<any>();
3940

41+
/**
42+
* BG style of the currently selected option
43+
*/
44+
45+
public backgroundClass: string;
46+
47+
ngOnInit() {
48+
this.backgroundClass = this.getBackgroundColorClass();
49+
}
50+
51+
ngOnChanges(changes: SimpleChanges) {
52+
// Recalculate BG class if options or current value are changed by parent component
53+
if ((hasValue(changes?.selectedValue?.currentValue) && !changes.selectedValue.isFirstChange())
54+
|| (hasValue(changes?.options?.currentValue) && !changes.options.isFirstChange())) {
55+
this.backgroundClass = this.getBackgroundColorClass();
56+
}
57+
}
58+
4059
/**
4160
* Update the selected value and emit the change event
4261
* @param value The new value to select
@@ -46,6 +65,8 @@ export class SwitchComponent {
4665
this.selectedValue = value;
4766
this.selectedValueChange.emit(this.selectedValue);
4867

68+
this.backgroundClass = this.getBackgroundColorClass();
69+
4970
}
5071

5172
/**

0 commit comments

Comments
 (0)