|
| 1 | +import { Component, computed, EventEmitter, input, Input, model, Output, signal } from '@angular/core'; |
| 2 | + |
| 3 | +@Component({ |
| 4 | + selector: 'app-feature', |
| 5 | + templateUrl: './feature.component.html', |
| 6 | + styleUrls: ['./feature.component.scss'], |
| 7 | + standalone: true, |
| 8 | +}) |
| 9 | +export class FeatureComponent { |
| 10 | + /** |
| 11 | + * Regular class field representing the component's title |
| 12 | + */ |
| 13 | + public title: string = 'Feature Component'; |
| 14 | + |
| 15 | + /** |
| 16 | + * Private counter value used for internal state management |
| 17 | + */ |
| 18 | + private _counter = signal(0); |
| 19 | + |
| 20 | + /** |
| 21 | + * Public signal exposing the current counter value |
| 22 | + * @returns The current counter value |
| 23 | + */ |
| 24 | + public counter = this._counter; |
| 25 | + |
| 26 | + /** |
| 27 | + * Computed signal that returns the doubled counter value |
| 28 | + * @returns The counter value multiplied by 2 |
| 29 | + */ |
| 30 | + public doubledCounter = computed(() => this._counter() * 2); |
| 31 | + |
| 32 | + /** |
| 33 | + * Traditional Input decorator for receiving a theme value |
| 34 | + */ |
| 35 | + @Input() theme: 'light' | 'dark' = 'light'; |
| 36 | + |
| 37 | + /** |
| 38 | + * Input setter that logs when the value changes |
| 39 | + * @param value - The new value to set |
| 40 | + */ |
| 41 | + @Input() set logValue(value: string) { |
| 42 | + console.log('logValue changed:', value); |
| 43 | + this._logValue = value; |
| 44 | + } |
| 45 | + get logValue(): string { |
| 46 | + return this._logValue; |
| 47 | + } |
| 48 | + private _logValue: string = ''; |
| 49 | + |
| 50 | + /** |
| 51 | + * Signal input for the component name with default value |
| 52 | + */ |
| 53 | + name = input<string>('Feature'); |
| 54 | + |
| 55 | + /** |
| 56 | + * Required signal input for the user ID |
| 57 | + */ |
| 58 | + userId = input.required<string>(); |
| 59 | + |
| 60 | + /** |
| 61 | + * Signal input for feature flags with default value |
| 62 | + */ |
| 63 | + isEnabled = input(true); |
| 64 | + |
| 65 | + /** |
| 66 | + * Signal input for configuration object |
| 67 | + */ |
| 68 | + config = input({ |
| 69 | + showHeader: true, |
| 70 | + maxItems: 10, |
| 71 | + }); |
| 72 | + |
| 73 | + /** |
| 74 | + * Two-way bindable model signal |
| 75 | + */ |
| 76 | + checked = model(false); |
| 77 | + |
| 78 | + /** |
| 79 | + * Traditional Output decorator for emitting when the counter changes |
| 80 | + */ |
| 81 | + @Output() counterChanged = new EventEmitter<number>(); |
| 82 | + |
| 83 | + /** |
| 84 | + * Traditional Output decorator for emitting when an action is performed |
| 85 | + */ |
| 86 | + @Output() actionPerformed = new EventEmitter<string>(); |
| 87 | + |
| 88 | + /** |
| 89 | + * Increments the counter value and emits the new value |
| 90 | + */ |
| 91 | + public increment(): void { |
| 92 | + this._counter.update((value) => value + 1); |
| 93 | + this.counterChanged.emit(this._counter()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Decrements the counter value and emits the new value |
| 98 | + */ |
| 99 | + public decrement(): void { |
| 100 | + this._counter.update((value) => value - 1); |
| 101 | + this.counterChanged.emit(this._counter()); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Resets the counter to zero and emits the new value |
| 106 | + */ |
| 107 | + public reset(): void { |
| 108 | + this._counter.set(0); |
| 109 | + this.counterChanged.emit(0); |
| 110 | + this.actionPerformed.emit('reset'); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Performs a custom action and emits the action name |
| 115 | + * @param actionName - The name of the action being performed |
| 116 | + */ |
| 117 | + public performAction(actionName: string): void { |
| 118 | + console.log(`Performing action: ${actionName}`); |
| 119 | + this.actionPerformed.emit(actionName); |
| 120 | + } |
| 121 | +} |
0 commit comments