Skip to content

refactor: making radio controls compatible with forms #1314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions projects/components/src/radio/radio-group.component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output, TemplateRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { MatRadioChange } from '@angular/material/radio';
import { LoggerService } from '@hypertrace/common';
import { RadioOption } from './radio-option';

@Component({
selector: 'ht-radio-group',
styleUrls: ['./radio-group.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: RadioGroupComponent,
multi: true
}
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<ht-label *ngIf="this.title" class="title" [label]="this.title"></ht-label>
Expand Down Expand Up @@ -34,7 +42,7 @@ import { RadioOption } from './radio-option';
<ng-template #defaultLabel let-label><ht-label class="radio-button-label" [label]="label"></ht-label></ng-template>
`
})
export class RadioGroupComponent implements OnInit {
export class RadioGroupComponent implements ControlValueAccessor, OnInit {
@Input()
public title!: string;

Expand All @@ -53,6 +61,9 @@ export class RadioGroupComponent implements OnInit {
@Output()
public readonly selectedChange: EventEmitter<string> = new EventEmitter();

private propagateControlValueChange?: (value: string | undefined) => void;
private propagateControlValueChangeOnTouch?: (value: string | undefined) => void;

public constructor(private readonly loggerService: LoggerService) {}

public ngOnInit(): void {
Expand All @@ -69,14 +80,32 @@ export class RadioGroupComponent implements OnInit {
}
}

public writeValue(value?: string): void {
this.setSelection(value);
}

public registerOnChange(onChange: (value?: string) => void): void {
this.propagateControlValueChange = onChange;
}

public registerOnTouched(onTouch: (value?: string) => void): void {
this.propagateControlValueChangeOnTouch = onTouch;
}

public onRadioChange(event: MatRadioChange): void {
this.selected = this.options.find(option => option.value === event.value);
this.selectedChange.emit(event.value);
this.setSelection(event.value);
}

public isLabelAString(label: string | TemplateRef<unknown>): boolean {
return typeof label === 'string';
}

private setSelection(value: string | undefined): void {
this.selected = this.options.find(option => option.value === value);
this.selectedChange.emit(value);
this.propagateControlValueChange?.(value);
this.propagateControlValueChangeOnTouch?.(value);
}
}

export const enum OptionsDirection {
Expand Down