packages/ui/src/lib/form/toggle/toggle.component.ts
The toggle component is an alternative to the default boolean checkbox with fancier style. It is used in the default input template for the view 'toggle'.
| providers |
{
provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => ToggleComponent), multi: true,
}
|
| selector | ec-toggle |
| templateUrl | ./toggle.component.html |
Properties |
Methods |
Inputs |
| id | |
Default value : 'toggle' + Date.now()
|
|
| placeholder | |
Type : string
|
|
|
The toggles placeholder (currently not in use) |
|
| registerOnChange | ||||
registerOnChange(fn)
|
||||
|
Registers change callback
Parameters :
Returns :
void
|
| registerOnTouched |
registerOnTouched()
|
|
Register Touch
Returns :
void
|
| toggle |
toggle()
|
|
Toggles the value
Returns :
void
|
| writeValue | ||||||
writeValue(value: boolean)
|
||||||
|
writes incoming value
Parameters :
Returns :
void
|
| propagateChange |
Default value : () => {...}
|
| value |
Type : boolean
|
|
The current value |
import { Component, forwardRef, Input } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
/** The toggle component is an alternative to the default boolean checkbox with fancier style.
* It is used in the default input template for the view 'toggle'. */
@Component({
selector: 'ec-toggle',
templateUrl: './toggle.component.html',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => ToggleComponent),
multi: true,
},
],
})
export class ToggleComponent implements ControlValueAccessor {
/** The current value */
value: boolean;
/** The toggles placeholder (currently not in use) */
@Input() placeholder: string;
@Input() id = 'toggle' + Date.now();
/** Toggles the value */
toggle() {
this.value = !this.value;
this.propagateChange(this.value);
}
/** writes incoming value */
writeValue(value: boolean) {
this.value = value;
}
/* Propagates change*/
propagateChange = (_: any) => { };
/** Registers change callback */
registerOnChange(fn) {
this.propagateChange = fn;
}
/** Register Touch */
registerOnTouched() { }
}
<div class="ec-toggle xui-toggle">
<input [id]="id" type="checkbox" [checked]="value" (click)="toggle()" />
<label [for]="id" class="xui-toggle__label">
<div class="xui-toggle__switch" [class.is-active]="value"></div>
{{ placeholder }}
</label>
</div>