Skip to content

Commit 99c5db1

Browse files
brandonrobertsAndrewKushnir
authored andcommitted
docs(forms): update API reference for value accessors (angular#26946)
PR Close angular#26946
1 parent e5c9f7a commit 99c5db1

File tree

7 files changed

+433
-93
lines changed

7 files changed

+433
-93
lines changed

packages/forms/src/directives/checkbox_value_accessor.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,26 @@ export const CHECKBOX_VALUE_ACCESSOR: any = {
1717
};
1818

1919
/**
20-
* The accessor for writing a value and listening to changes on a checkbox input element.
20+
* @description
21+
* A `ControlValueAccessor` for writing a value and listening to changes on a checkbox input
22+
* element.
2123
*
2224
* @usageNotes
23-
* ### Example
2425
*
26+
* ### Using a checkbox with a reactive form.
27+
*
28+
* The following example shows how to use a checkbox with a reactive form.
29+
*
30+
* ```ts
31+
* const rememberLoginControl = new FormControl();
2532
* ```
26-
* <input type="checkbox" name="rememberLogin" ngModel>
33+
*
34+
* ```
35+
* <input type="checkbox" [formControl]="rememberLoginControl">
2736
* ```
2837
*
29-
* @ngModule FormsModule
3038
* @ngModule ReactiveFormsModule
39+
* @ngModule FormsModule
3140
* @publicApi
3241
*/
3342
@Directive({
@@ -37,17 +46,50 @@ export const CHECKBOX_VALUE_ACCESSOR: any = {
3746
providers: [CHECKBOX_VALUE_ACCESSOR]
3847
})
3948
export class CheckboxControlValueAccessor implements ControlValueAccessor {
49+
/**
50+
* @description
51+
* The registered callback function called when a change event occurs on the input element.
52+
*/
4053
onChange = (_: any) => {};
54+
55+
/**
56+
* @description
57+
* The registered callback function called when a blur event occurs on the input element.
58+
*/
4159
onTouched = () => {};
4260

4361
constructor(private _renderer: Renderer2, private _elementRef: ElementRef) {}
4462

63+
/**
64+
* Sets the "checked" property on the input element.
65+
*
66+
* @param value The checked value
67+
*/
4568
writeValue(value: any): void {
4669
this._renderer.setProperty(this._elementRef.nativeElement, 'checked', value);
4770
}
71+
72+
/**
73+
* @description
74+
* Registers a function called when the control value changes.
75+
*
76+
* @param fn The callback function
77+
*/
4878
registerOnChange(fn: (_: any) => {}): void { this.onChange = fn; }
79+
80+
/**
81+
* @description
82+
* Registers a function called when the control is touched.
83+
*
84+
* @param fn The callback function
85+
*/
4986
registerOnTouched(fn: () => {}): void { this.onTouched = fn; }
5087

88+
/**
89+
* Sets the "disabled" property on the input element.
90+
*
91+
* @param isDisabled The disabled value
92+
*/
5193
setDisabledState(isDisabled: boolean): void {
5294
this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', isDisabled);
5395
}

packages/forms/src/directives/default_value_accessor.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,28 @@ function _isAndroid(): boolean {
3232
export const COMPOSITION_BUFFER_MODE = new InjectionToken<boolean>('CompositionEventMode');
3333

3434
/**
35-
* The default accessor for writing a value and listening to changes that is used by the
36-
* `NgModel`, `FormControlDirective`, and `FormControlName` directives.
35+
* @description
36+
* The default `ControlValueAccessor` for writing a value and listening to changes on input
37+
* elements. The accessor is used by the `FormControlDirective`, `FormControlName`, and
38+
* `NgModel` directives.
3739
*
3840
* @usageNotes
39-
* ### Example
41+
*
42+
* ### Using the default value accessor
43+
*
44+
* The following example shows how to use an input element that activates the default value accessor
45+
* (in this case, a text field).
46+
*
47+
* ```ts
48+
* const firstNameControl = new FormControl();
49+
* ```
4050
*
4151
* ```
42-
* <input type="text" name="searchQuery" ngModel>
52+
* <input type="text" [formControl]="firstNameControl">
4353
* ```
4454
*
45-
* @ngModule FormsModule
4655
* @ngModule ReactiveFormsModule
56+
* @ngModule FormsModule
4757
* @publicApi
4858
*/
4959
@Directive({
@@ -61,7 +71,16 @@ export const COMPOSITION_BUFFER_MODE = new InjectionToken<boolean>('CompositionE
6171
providers: [DEFAULT_VALUE_ACCESSOR]
6272
})
6373
export class DefaultValueAccessor implements ControlValueAccessor {
74+
/**
75+
* @description
76+
* The registered callback function called when an input event occurs on the input element.
77+
*/
6478
onChange = (_: any) => {};
79+
80+
/**
81+
* @description
82+
* The registered callback function called when a blur event occurs on the input element.
83+
*/
6584
onTouched = () => {};
6685

6786
/** Whether the user is creating a composition string (IME events). */
@@ -75,14 +94,37 @@ export class DefaultValueAccessor implements ControlValueAccessor {
7594
}
7695
}
7796

97+
/**
98+
* Sets the "value" property on the input element.
99+
*
100+
* @param value The checked value
101+
*/
78102
writeValue(value: any): void {
79103
const normalizedValue = value == null ? '' : value;
80104
this._renderer.setProperty(this._elementRef.nativeElement, 'value', normalizedValue);
81105
}
82106

107+
/**
108+
* @description
109+
* Registers a function called when the control value changes.
110+
*
111+
* @param fn The callback function
112+
*/
83113
registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
114+
115+
/**
116+
* @description
117+
* Registers a function called when the control is touched.
118+
*
119+
* @param fn The callback function
120+
*/
84121
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
85122

123+
/**
124+
* Sets the "disabled" property on the input element.
125+
*
126+
* @param isDisabled The disabled value
127+
*/
86128
setDisabledState(isDisabled: boolean): void {
87129
this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', isDisabled);
88130
}

packages/forms/src/directives/number_value_accessor.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,27 @@ export const NUMBER_VALUE_ACCESSOR: any = {
1717
};
1818

1919
/**
20-
* The accessor for writing a number value and listening to changes that is used by the
21-
* `NgModel`, `FormControlDirective`, and `FormControlName` directives.
20+
* @description
21+
* The `ControlValueAccessor` for writing a number value and listening to number input changes.
22+
* The value accessor is used by the `FormControlDirective`, `FormControlName`, and `NgModel`
23+
* directives.
2224
*
2325
* @usageNotes
24-
* ### Example
26+
*
27+
* ### Using a number input with a reactive form.
28+
*
29+
* The following example shows how to use a number input with a reactive form.
30+
*
31+
* ```ts
32+
* const totalCountControl = new FormControl();
33+
* ```
2534
*
2635
* ```
27-
* <input type="number" [(ngModel)]="age">
36+
* <input type="number" [formControl]="totalCountControl">
2837
* ```
2938
*
30-
* @ngModule FormsModule
3139
* @ngModule ReactiveFormsModule
40+
* @ngModule FormsModule
3241
*/
3342
@Directive({
3443
selector:
@@ -41,22 +50,55 @@ export const NUMBER_VALUE_ACCESSOR: any = {
4150
providers: [NUMBER_VALUE_ACCESSOR]
4251
})
4352
export class NumberValueAccessor implements ControlValueAccessor {
53+
/**
54+
* @description
55+
* The registered callback function called when a change or input event occurs on the input
56+
* element.
57+
*/
4458
onChange = (_: any) => {};
59+
60+
/**
61+
* @description
62+
* The registered callback function called when a blur event occurs on the input element.
63+
*/
4564
onTouched = () => {};
4665

4766
constructor(private _renderer: Renderer2, private _elementRef: ElementRef) {}
4867

68+
/**
69+
* Sets the "value" property on the input element.
70+
*
71+
* @param value The checked value
72+
*/
4973
writeValue(value: number): void {
5074
// The value needs to be normalized for IE9, otherwise it is set to 'null' when null
5175
const normalizedValue = value == null ? '' : value;
5276
this._renderer.setProperty(this._elementRef.nativeElement, 'value', normalizedValue);
5377
}
5478

79+
/**
80+
* @description
81+
* Registers a function called when the control value changes.
82+
*
83+
* @param fn The callback function
84+
*/
5585
registerOnChange(fn: (_: number|null) => void): void {
5686
this.onChange = (value) => { fn(value == '' ? null : parseFloat(value)); };
5787
}
88+
89+
/**
90+
* @description
91+
* Registers a function called when the control is touched.
92+
*
93+
* @param fn The callback function
94+
*/
5895
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
5996

97+
/**
98+
* Sets the "disabled" property on the input element.
99+
*
100+
* @param isDisabled The disabled value
101+
*/
60102
setDisabledState(isDisabled: boolean): void {
61103
this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', isDisabled);
62104
}

0 commit comments

Comments
 (0)