import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { forwardRef, Injectable, EventEmitter, booleanAttribute, numberAttribute, Component, ChangeDetectionStrategy, Input, Output, ViewChild, NgModule } from '@angular/core';
import { NG_VALUE_ACCESSOR, NgControl } from '@angular/forms';
import * as i3 from 'primeng/autofocus';
import { AutoFocusModule } from 'primeng/autofocus';
import * as i1 from 'primeng/api';
const RADIO_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => RadioButton),
multi: true
};
class RadioControlRegistry {
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;
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: RadioControlRegistry, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: RadioControlRegistry, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: RadioControlRegistry, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}] });
/**
* RadioButton is an extension to standard radio button element with theming.
* @group Components
*/
class RadioButton {
cd;
injector;
registry;
config;
/**
* Value of the radiobutton.
* @group Props
*/
value;
/**
* The name of the form control.
* @group Props
*/
formControlName;
/**
* Name of the radiobutton group.
* @group Props
*/
name;
/**
* When present, it specifies that the element should be disabled.
* @group Props
*/
disabled;
/**
* Label of the radiobutton.
* @group Props
*/
label;
/**
* Specifies the input variant of the component.
* @group Props
*/
variant = 'outlined';
/**
* Index of the element in tabbing order.
* @group Props
*/
tabindex;
/**
* Identifier of the focus input to match a label defined for the component.
* @group Props
*/
inputId;
/**
* Establishes relationships between the component and label(s) where its value should be one or more element IDs.
* @group Props
*/
ariaLabelledBy;
/**
* Used to define a string that labels the input element.
* @group Props
*/
ariaLabel;
/**
* Inline style of the component.
* @group Props
*/
style;
/**
* Style class of the component.
* @group Props
*/
styleClass;
/**
* Style class of the label.
* @group Props
*/
labelStyleClass;
/**
* When present, it specifies that the component should automatically get focus on load.
* @group Props
*/
autofocus;
/**
* Callback to invoke on radio button click.
* @param {RadioButtonClickEvent} event - Custom click event.
* @group Emits
*/
onClick = new EventEmitter();
/**
* Callback to invoke when the receives focus.
* @param {Event} event - Browser event.
* @group Emits
*/
onFocus = new EventEmitter();
/**
* Callback to invoke when the loses focus.
* @param {Event} event - Browser event.
* @group Emits
*/
onBlur = new EventEmitter();
inputViewChild;
onModelChange = () => { };
onModelTouched = () => { };
checked;
focused;
control;
constructor(cd, injector, registry, config) {
this.cd = cd;
this.injector = injector;
this.registry = registry;
this.config = config;
}
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({ originalEvent: event, value: this.value });
}
}
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);
}
/**
* Applies focus to input field.
* @group Method
*/
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: