projects/rebirth-ng/src/lib/radio-group/radio-group.component.ts
changeDetection | ChangeDetectionStrategy.OnPush |
exportAs | radioGroup |
host | { |
providers |
{
: , : (() => ), : true
}
|
selector | re-radio-group |
styleUrls | radio-group.component.scss |
templateUrl | ./radio-group.component.html |
Properties |
Methods |
Inputs |
Outputs |
constructor(rebirthNGConfig: RebirthNGConfig, changeDetectorRef: ChangeDetectorRef)
|
|||||||||
Parameters :
|
cssClass
|
Type: |
disabled
|
Default value: |
disabledItems
|
Type: |
formatter
|
Type: |
inline
|
Type: |
options
|
Type: |
valueParser
|
Type: |
valueChange
|
$event type: EventEmitter
|
isChecked | ||||||
isChecked(item: any)
|
||||||
Parameters :
Returns :
boolean
|
isDisabled | ||||
isDisabled(item: )
|
||||
Parameters :
Returns :
any
|
onRadioChange | ||||||
onRadioChange(item: any)
|
||||||
Parameters :
Returns :
void
|
registerOnChange | ||||||
registerOnChange(fn: any)
|
||||||
Parameters :
Returns :
void
|
registerOnTouched | ||||||
registerOnTouched(fn: any)
|
||||||
Parameters :
Returns :
void
|
setDisabledState | ||||||
setDisabledState(isDisabled: boolean)
|
||||||
Parameters :
Returns :
void
|
writeValue | ||||||
writeValue(obj: any)
|
||||||
Parameters :
Returns :
void
|
name |
name:
|
Type : string
|
Static NAME_SEED |
NAME_SEED:
|
Type : number
|
Default value : 0
|
Private onChange |
onChange:
|
Default value : (_: any) => null
|
Private onTouched |
onTouched:
|
Default value : () => null
|
value |
value:
|
Type : any
|
import {
Component, ChangeDetectionStrategy, forwardRef, Input, ChangeDetectorRef, EventEmitter,
Output
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { RebirthNGConfig } from '../rebirth-ng.config';
@Component({
selector: 're-radio-group',
templateUrl: './radio-group.component.html',
styleUrls: ['./radio-group.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
host: { '[class]': 'cssClass' },
exportAs: 'radioGroup',
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => RadioGroupComponent),
multi: true
}]
})
export class RadioGroupComponent implements ControlValueAccessor {
static NAME_SEED = 0;
@Input() disabled = false;
@Input() options: any[];
@Input() inline: boolean;
@Input() disabledItems: any[];
@Input() cssClass: string;
@Input() formatter: (item: any) => string;
@Input() valueParser: (item: any) => any;
@Output() valueChange = new EventEmitter<any>();
name: string;
value: any;
private onChange = (_: any) => null;
private onTouched = () => null;
constructor(rebirthNGConfig: RebirthNGConfig, private changeDetectorRef: ChangeDetectorRef) {
this.formatter = rebirthNGConfig.radioGroup.formatter;
this.valueParser = rebirthNGConfig.radioGroup.valueParser;
this.name = `rebirth-radio-group-${RadioGroupComponent.NAME_SEED++}`;
}
writeValue(obj: any): void {
this.value = obj;
this.changeDetectorRef.markForCheck();
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
}
onRadioChange(item: any) {
this.onTouched();
this.value = this.valueParser(item);
this.onChange(this.value);
this.valueChange.emit(this.value);
}
isChecked(item: any) {
return this.value === this.valueParser(item);
}
isDisabled(item) {
return this.disabled || (this.disabledItems || []).some(it => this.valueParser(item) === it);
}
}
<div [ngClass]="{'radio-inline': inline, 'radio': !inline}" *ngFor="let item of options">
<label>
<input type="radio" [name]="name" [value]="valueParser(item)" [checked]="isChecked(item)" [disabled]="isDisabled(item)"
(change)="onRadioChange(item)">{{formatter(item)}}
</label>
</div>