import * as i0 from '@angular/core';
import { forwardRef, Injectable, EventEmitter, Component, ChangeDetectionStrategy, Input, Output, ViewChild, NgModule } from '@angular/core';
import * as i1 from '@angular/common';
import { CommonModule } from '@angular/common';
import { NG_VALUE_ACCESSOR, NgControl } from '@angular/forms';
const RADIO_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => RadioButton),
multi: true
};
class RadioControlRegistry {
constructor() {
this.accessors = [];
}
add(control, accessor) {
this.accessors.push([control, accessor]);
}
remove(accessor) {
this.accessors = this.accessors.filter((c) => {
return c[1] !== accessor;
});
}
select(accessor) {
this.accessors.forEach((c) => {
if (this.isSameGroup(c, accessor) && c[1] !== accessor) {
c[1].writeValue(accessor.value);
}
});
}
isSameGroup(controlPair, accessor) {
if (!controlPair[0].control) {
return false;
}
return controlPair[0].control.root === accessor.control.control.root && controlPair[1].name === accessor.name;
}
}
RadioControlRegistry.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: RadioControlRegistry, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
RadioControlRegistry.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: RadioControlRegistry, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.0", ngImport: i0, type: RadioControlRegistry, decorators: [{
type: Injectable,
args: [{
providedIn: 'root',
}]
}] });
class RadioButton {
constructor(cd, injector, registry) {
this.cd = cd;
this.injector = injector;
this.registry = registry;
this.onClick = new EventEmitter();
this.onFocus = new EventEmitter();
this.onBlur = new EventEmitter();
this.onModelChange = () => { };
this.onModelTouched = () => { };
}
ngOnInit() {
this.control = this.injector.get(NgControl);
this.checkName();
this.registry.add(this.control, this);
}
handleClick(event, radioButton, focus) {
event.preventDefault();
if (this.disabled) {
return;
}
this.select(event);
if (focus) {
radioButton.focus();
}
}
select(event) {
if (!this.disabled) {
this.inputViewChild.nativeElement.checked = true;
this.checked = true;
this.onModelChange(this.value);
this.registry.select(this);
this.onClick.emit(event);
}
}
writeValue(value) {
this.checked = (value == this.value);
if (this.inputViewChild && this.inputViewChild.nativeElement) {
this.inputViewChild.nativeElement.checked = this.checked;
}
this.cd.markForCheck();
}
registerOnChange(fn) {
this.onModelChange = fn;
}
registerOnTouched(fn) {
this.onModelTouched = fn;
}
setDisabledState(val) {
this.disabled = val;
this.cd.markForCheck();
}
onInputFocus(event) {
this.focused = true;
this.onFocus.emit(event);
}
onInputBlur(event) {
this.focused = false;
this.onModelTouched();
this.onBlur.emit(event);
}
onChange(event) {
this.select(event);
}
focus() {
this.inputViewChild.nativeElement.focus();
}
ngOnDestroy() {
this.registry.remove(this);
}
checkName() {
if (this.name && this.formControlName && this.name !== this.formControlName) {
this.throwNameError();
}
if (!this.name && this.formControlName) {
this.name = this.formControlName;
}
}
throwNameError() {
throw new Error(`
If you define both a name and a formControlName attribute on your radio button, their values
must match. Ex: