Browse Source

Task/improve type safety of toggle component (#6533)

* Improve type safety
pull/6523/head^2
Kenrick Tandrian 1 week ago
committed by GitHub
parent
commit
573fbb9a40
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 7
      libs/ui/src/lib/toggle/toggle.component.html
  2. 32
      libs/ui/src/lib/toggle/toggle.component.ts

7
libs/ui/src/lib/toggle/toggle.component.html

@ -3,13 +3,14 @@
[formControl]="optionFormControl" [formControl]="optionFormControl"
(change)="onValueChange()" (change)="onValueChange()"
> >
@for (option of options; track option) { @for (option of options(); track option) {
<mat-radio-button <mat-radio-button
class="d-inline-flex" class="d-inline-flex"
[disabled]="isLoading" [disabled]="isLoading()"
[ngClass]="{ [ngClass]="{
'cursor-default': option.value === optionFormControl.value, 'cursor-default': option.value === optionFormControl.value,
'cursor-pointer': !isLoading && option.value !== optionFormControl.value 'cursor-pointer':
!isLoading() && option.value !== optionFormControl.value
}" }"
[value]="option.value" [value]="option.value"
>{{ option.label }}</mat-radio-button >{{ option.label }}</mat-radio-button

32
libs/ui/src/lib/toggle/toggle.component.ts

@ -4,10 +4,9 @@ import { CommonModule } from '@angular/common';
import { import {
ChangeDetectionStrategy, ChangeDetectionStrategy,
Component, Component,
EventEmitter, effect,
Input, input,
OnChanges, output
Output
} from '@angular/core'; } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { MatRadioModule } from '@angular/material/radio'; import { MatRadioModule } from '@angular/material/radio';
@ -19,20 +18,25 @@ import { MatRadioModule } from '@angular/material/radio';
styleUrls: ['./toggle.component.scss'], styleUrls: ['./toggle.component.scss'],
templateUrl: './toggle.component.html' templateUrl: './toggle.component.html'
}) })
export class GfToggleComponent implements OnChanges { export class GfToggleComponent {
@Input() defaultValue: string; public readonly defaultValue = input.required<string>();
@Input() isLoading: boolean; public readonly isLoading = input<boolean>(false);
@Input() options: ToggleOption[] = []; public readonly options = input<ToggleOption[]>([]);
@Output() valueChange = new EventEmitter<Pick<ToggleOption, 'value'>>(); protected readonly optionFormControl = new FormControl<string | null>(null);
protected readonly valueChange = output<Pick<ToggleOption, 'value'>>();
public optionFormControl = new FormControl<string>(undefined); public constructor() {
effect(() => {
public ngOnChanges() { this.optionFormControl.setValue(this.defaultValue());
this.optionFormControl.setValue(this.defaultValue); });
} }
public onValueChange() { public onValueChange() {
this.valueChange.emit({ value: this.optionFormControl.value }); const value = this.optionFormControl.value;
if (value !== null) {
this.valueChange.emit({ value });
}
} }
} }

Loading…
Cancel
Save