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

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

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

Loading…
Cancel
Save