Skip to content

Commit 49f5a58

Browse files
[DSC-1872] Created toggle component
1 parent 640dc02 commit 49f5a58

5 files changed

Lines changed: 140 additions & 0 deletions

File tree

src/app/shared/shared.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ import { ThemedDefaultBrowseElementsComponent } from './browse-most-elements/def
357357
import { MetadataLinkViewPopoverComponent } from './metadata-link-view/metadata-link-view-popover/metadata-link-view-popover.component';
358358
import { MetadataLinkViewAvatarPopoverComponent } from './metadata-link-view/metadata-link-view-avatar-popover/metadata-link-view-avatar-popover.component';
359359
import { MetadataLinkViewOrcidComponent } from './metadata-link-view/metadata-link-view-orcid/metadata-link-view-orcid.component';
360+
import { SwitchComponent } from './switch/switch.component';
360361

361362
const MODULES = [
362363
CommonModule,
@@ -595,6 +596,7 @@ const ENTRY_COMPONENTS = [
595596
RelationshipsListComponent,
596597
AdditionalMetadataComponent,
597598
ThemedDefaultBrowseElementsComponent,
599+
SwitchComponent,
598600
];
599601

600602
const PROVIDERS = [
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<div class="btn-group">
2+
<div class="switch-container" [ngClass]="getBackgroundColorClass()">
3+
<div *ngFor="let option of options">
4+
<div class="switch-opt"
5+
(click)="onOptionClick(option.value)"
6+
[class.bg-white]="selectedValue === option.value">
7+
<i [class]="option.icon + ' ' + (selectedValue === option.value ? 'text-' + option.iconColor : '')"></i>
8+
</div>
9+
</div>
10+
</div>
11+
<div *ngFor="let option of options">
12+
<span *ngIf="selectedValue === option.value"
13+
[class]="selectedValue === option.value ? 'text-' + option.labelColor : ''"
14+
class="visibility-label ml-2 font-weight-bold">
15+
{{ option.label | translate }}
16+
</span>
17+
</div>
18+
</div>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.switch-container {
2+
height: 35px;
3+
width: fit-content;
4+
border-radius: 100px;
5+
display: flex;
6+
&.bg-default {
7+
background-color: rgb(204, 204, 204);
8+
}
9+
}
10+
11+
.switch-opt {
12+
align-items: center;
13+
display: flex;
14+
justify-content: center;
15+
background-color: transparent;
16+
cursor: pointer;
17+
width: 32px;
18+
height: 32px;
19+
border-radius: 100%;
20+
position: relative;
21+
top: 1px;
22+
margin-left: 2px;
23+
margin-right: 2px;
24+
}
25+
26+
.visibility-label {
27+
line-height: 40px;
28+
}
29+
30+
.disabled {
31+
opacity: 0.5;
32+
cursor: not-allowed;
33+
pointer-events: none;
34+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { SwitchComponent } from './switch.component';
4+
5+
describe('SwitchComponent', () => {
6+
let component: SwitchComponent;
7+
let fixture: ComponentFixture<SwitchComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [ SwitchComponent ]
12+
})
13+
.compileComponents();
14+
15+
fixture = TestBed.createComponent(SwitchComponent);
16+
component = fixture.componentInstance;
17+
fixture.detectChanges();
18+
});
19+
20+
it('should create', () => {
21+
expect(component).toBeTruthy();
22+
});
23+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Component, EventEmitter, Input, Output } from '@angular/core';
2+
3+
export enum SwitchColor {
4+
Primary = 'primary',
5+
Success = 'success',
6+
Warning = 'warning',
7+
Danger = 'danger',
8+
}
9+
10+
export interface SwitchOption {
11+
value: any;
12+
icon?: string;
13+
iconColor?: SwitchColor;
14+
label?: string;
15+
labelColor?: SwitchColor;
16+
backgroundColor?: SwitchColor;
17+
}
18+
19+
@Component({
20+
selector: 'ds-switch',
21+
templateUrl: './switch.component.html',
22+
styleUrls: ['./switch.component.scss'],
23+
})
24+
export class SwitchComponent {
25+
/**
26+
* The options available for the switch
27+
*/
28+
@Input() options: SwitchOption[] = [];
29+
30+
/**
31+
* The currently selected value
32+
*/
33+
@Input() selectedValue: any;
34+
35+
/**
36+
* Event emitted when the selected value changes
37+
*/
38+
@Output() selectedValueChange = new EventEmitter<any>();
39+
40+
/**
41+
* Update the selected value and emit the change event
42+
* @param value The new value to select
43+
*/
44+
onOptionClick(value: any) {
45+
46+
this.selectedValue = value;
47+
this.selectedValueChange.emit(this.selectedValue);
48+
49+
}
50+
51+
/**
52+
* Returns the background color class based on the selected value.
53+
* Defaults to 'bg-default' if no specific color is set.
54+
*/
55+
getBackgroundColorClass(): string {
56+
const selectedOption = this.options.find(option => option.value === this.selectedValue);
57+
if (selectedOption && selectedOption.backgroundColor) {
58+
return `bg-${selectedOption.backgroundColor}`;
59+
}
60+
return 'bg-default';
61+
}
62+
63+
}

0 commit comments

Comments
 (0)