These are no longer needed as stable docs are computed as those that do not have `@experimental` or `@deprecated` tags. PR Close #23210
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
|
|
import {Directive, ElementRef, Renderer2, forwardRef} from '@angular/core';
|
|
|
|
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
|
|
|
|
export const CHECKBOX_VALUE_ACCESSOR: any = {
|
|
provide: NG_VALUE_ACCESSOR,
|
|
useExisting: forwardRef(() => CheckboxControlValueAccessor),
|
|
multi: true,
|
|
};
|
|
|
|
/**
|
|
* The accessor for writing a value and listening to changes on a checkbox input element.
|
|
*
|
|
* ### Example
|
|
* ```
|
|
* <input type="checkbox" name="rememberLogin" ngModel>
|
|
* ```
|
|
*
|
|
*
|
|
*/
|
|
@Directive({
|
|
selector:
|
|
'input[type=checkbox][formControlName],input[type=checkbox][formControl],input[type=checkbox][ngModel]',
|
|
host: {'(change)': 'onChange($event.target.checked)', '(blur)': 'onTouched()'},
|
|
providers: [CHECKBOX_VALUE_ACCESSOR]
|
|
})
|
|
export class CheckboxControlValueAccessor implements ControlValueAccessor {
|
|
onChange = (_: any) => {};
|
|
onTouched = () => {};
|
|
|
|
constructor(private _renderer: Renderer2, private _elementRef: ElementRef) {}
|
|
|
|
writeValue(value: any): void {
|
|
this._renderer.setProperty(this._elementRef.nativeElement, 'checked', value);
|
|
}
|
|
registerOnChange(fn: (_: any) => {}): void { this.onChange = fn; }
|
|
registerOnTouched(fn: () => {}): void { this.onTouched = fn; }
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', isDisabled);
|
|
}
|
|
}
|