docs(forms): update API reference for value accessors (#26946)
PR Close #26946
This commit is contained in:

committed by
Andrew Kushnir

parent
e5c9f7a507
commit
99c5db1fb1
@ -17,18 +17,27 @@ export const RANGE_VALUE_ACCESSOR: StaticProvider = {
|
||||
};
|
||||
|
||||
/**
|
||||
* The accessor for writing a range value and listening to changes that is used by the
|
||||
* `NgModel`, `FormControlDirective`, and `FormControlName` directives.
|
||||
* @description
|
||||
* The `ControlValueAccessor` for writing a range value and listening to range input changes.
|
||||
* The value accessor is used by the `FormControlDirective`, `FormControlName`, and `NgModel`
|
||||
* directives.
|
||||
*
|
||||
* @usageNotes
|
||||
* ### Example
|
||||
*
|
||||
* ```
|
||||
* <input type="range" [(ngModel)]="age" >
|
||||
* ### Using a range input with a reactive form
|
||||
*
|
||||
* The following example shows how to use a range input with a reactive form.
|
||||
*
|
||||
* ```ts
|
||||
* const ageControl = new FormControl();
|
||||
* ```
|
||||
*
|
||||
* ```
|
||||
* <input type="range" [formControl]="ageControl">
|
||||
* ```
|
||||
*
|
||||
* @ngModule FormsModule
|
||||
* @ngModule ReactiveFormsModule
|
||||
* @ngModule FormsModule
|
||||
*/
|
||||
@Directive({
|
||||
selector:
|
||||
@ -41,21 +50,53 @@ export const RANGE_VALUE_ACCESSOR: StaticProvider = {
|
||||
providers: [RANGE_VALUE_ACCESSOR]
|
||||
})
|
||||
export class RangeValueAccessor implements ControlValueAccessor {
|
||||
/**
|
||||
* @description
|
||||
* The registered callback function called when a change or input event occurs on the input
|
||||
* element.
|
||||
*/
|
||||
onChange = (_: any) => {};
|
||||
|
||||
/**
|
||||
* @description
|
||||
* The registered callback function called when a blur event occurs on the input element.
|
||||
*/
|
||||
onTouched = () => {};
|
||||
|
||||
constructor(private _renderer: Renderer2, private _elementRef: ElementRef) {}
|
||||
|
||||
/**
|
||||
* Sets the "value" property on the input element.
|
||||
*
|
||||
* @param value The checked value
|
||||
*/
|
||||
writeValue(value: any): void {
|
||||
this._renderer.setProperty(this._elementRef.nativeElement, 'value', parseFloat(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description
|
||||
* Registers a function called when the control value changes.
|
||||
*
|
||||
* @param fn The callback function
|
||||
*/
|
||||
registerOnChange(fn: (_: number|null) => void): void {
|
||||
this.onChange = (value) => { fn(value == '' ? null : parseFloat(value)); };
|
||||
}
|
||||
|
||||
/**
|
||||
* @description
|
||||
* Registers a function called when the control is touched.
|
||||
*
|
||||
* @param fn The callback function
|
||||
*/
|
||||
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
|
||||
|
||||
/**
|
||||
* Sets the "disabled" property on the range input element.
|
||||
*
|
||||
* @param isDisabled The disabled value
|
||||
*/
|
||||
setDisabledState(isDisabled: boolean): void {
|
||||
this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', isDisabled);
|
||||
}
|
||||
|
Reference in New Issue
Block a user