projects/rebirth-ng/src/lib/checkbox-group/checkbox-group.component.ts
changeDetection | ChangeDetectionStrategy.OnPush |
exportAs | checkboxGroup |
host | { |
providers |
{
: , : (() => ), : true
}
|
selector | re-checkbox-group |
styleUrls | checkbox-group.component.scss |
templateUrl | ./checkbox-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
|
invertSelect |
invertSelect()
|
Returns :
void
|
isChecked | ||||||
isChecked(item: any)
|
||||||
Parameters :
Returns :
boolean
|
isDisabled | ||||
isDisabled(item: )
|
||||
Parameters :
Returns :
any
|
onCheckBoxChange | |||||||||
onCheckBoxChange(item: any, checkbox: )
|
|||||||||
Parameters :
Returns :
void
|
registerOnChange | ||||||
registerOnChange(fn: any)
|
||||||
Parameters :
Returns :
void
|
registerOnTouched | ||||||
registerOnTouched(fn: any)
|
||||||
Parameters :
Returns :
void
|
selectAll |
selectAll()
|
Returns :
void
|
setDisabledState | ||||||
setDisabledState(isDisabled: boolean)
|
||||||
Parameters :
Returns :
void
|
unselectAll |
unselectAll()
|
Returns :
void
|
writeValue | ||||||
writeValue(obj: any)
|
||||||
Parameters :
Returns :
void
|
Private onChange |
onChange:
|
Default value : (_: any) => null
|
Private onTouched |
onTouched:
|
Default value : () => null
|
value |
value:
|
Type : any[]
|
Default value : []
|
import {
Component, ChangeDetectionStrategy, Input, ChangeDetectorRef, forwardRef, Output,
EventEmitter
} from '@angular/core';
import { RebirthNGConfig } from '../rebirth-ng.config';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
@Component({
selector: 're-checkbox-group',
templateUrl: './checkbox-group.component.html',
styleUrls: ['./checkbox-group.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
exportAs: 'checkboxGroup',
host: { '[class]': 'cssClass' },
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CheckboxGroupComponent),
multi: true
}]
})
export class CheckboxGroupComponent implements ControlValueAccessor {
@Input() disabled = false;
@Input() options: any[];
@Input() inline: boolean;
@Input() cssClass: string;
@Input() disabledItems: any[];
@Input() formatter: (item: any) => string;
@Input() valueParser: (item: any) => any;
@Output() valueChange = new EventEmitter<any>();
value: any[] = [];
private onChange = (_: any) => null;
private onTouched = () => null;
constructor(rebirthNGConfig: RebirthNGConfig, private changeDetectorRef: ChangeDetectorRef) {
this.formatter = rebirthNGConfig.checkboxGroup.formatter;
this.valueParser = rebirthNGConfig.checkboxGroup.valueParser;
}
writeValue(obj: any): void {
this.value = obj instanceof Array ? obj : (obj ? [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;
}
selectAll() {
this.value = this.options.map((item) => this.valueParser(item));
this.onChange(this.value);
this.valueChange.emit(this.value);
}
unselectAll() {
this.value = [];
this.onChange(this.value);
this.valueChange.emit(this.value);
}
invertSelect() {
const current = this.value || [];
this.value = this.options
.filter((item) => current.indexOf(this.valueParser(item)) === -1)
.map((item) => this.valueParser(item));
this.onChange(this.value);
this.valueChange.emit(this.value);
}
isDisabled(item) {
return this.disabled || (this.disabledItems || []).some(it => this.valueParser(item) === it);
}
onCheckBoxChange(item: any, checkbox) {
this.onTouched();
this.value = this.value || [];
const value = this.valueParser(item);
if (checkbox.checked) {
this.value = [...this.value, value];
} else {
this.value = this.value.filter((valueItem) => valueItem !== value);
}
this.onChange(this.value);
this.valueChange.emit(this.value);
}
isChecked(item: any) {
return this.value.indexOf(this.valueParser(item)) !== -1;
}
}
<div [ngClass]="{'checkbox-inline': inline, 'checkbox': !inline}" *ngFor="let item of options">
<label>
<input #checkbox type="checkbox" [checked]="isChecked(item)" [disabled]="isDisabled(item)"
(change)="onCheckBoxChange(item, checkbox)">{{formatter(item)}}
</label>
</div>