|
| 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