Skip to content

Commit 3ee6fe8

Browse files
committed
docs(component-guide): add info on updating angular value accessors
1 parent 0c457b7 commit 3ee6fe8

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

docs/component-guide.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ For example, if your component renders a text input, it should be included in th
779779
780780
```diff
781781
@Directive({
782-
- selector: 'ion-input:not([type=number]),ion-input-otp[type=text],ion-textarea,ion-searchbar,
782+
- selector: 'ion-input:not([type=number]),ion-input-otp[type=text],ion-textarea,ion-searchbar',
783783
+ selector: 'ion-input:not([type=number]),ion-input-otp[type=text],ion-textarea,ion-searchbar,ion-new-component',
784784
providers: [
785785
{
@@ -791,6 +791,33 @@ For example, if your component renders a text input, it should be included in th
791791
})
792792
```
793793
794+
You'll also need to add your component's type to the value accessor's type definitions. For example, in the `TextValueAccessorDirective`, you would add your component's type to the `_handleInputEvent` method:
795+
796+
```diff
797+
@HostListener('ionInput', ['$event.target'])
798+
_handleInputEvent(
799+
- el: HTMLIonInputElement | HTMLIonInputOtpElement | HTMLIonTextareaElement | HTMLIonSearchbarElement
800+
+ el: HTMLIonInputElement | HTMLIonInputOtpElement | HTMLIonTextareaElement | HTMLIonSearchbarElement | HTMLIonNewComponentElement
801+
): void {
802+
this.handleValueChange(el, el.value);
803+
}
804+
```
805+
806+
If your component needs special handling for value changes, you'll also need to update the `registerOnChange` method. For example, if your component needs to handle numeric values:
807+
808+
```diff
809+
registerOnChange(fn: (_: number | null) => void): void {
810+
- if (this.el.nativeElement.tagName === 'ION-INPUT' || this.el.nativeElement.tagName === 'ION-INPUT-OTP') {
811+
+ if (this.el.nativeElement.tagName === 'ION-INPUT' || this.el.nativeElement.tagName === 'ION-INPUT-OTP' || this.el.nativeElement.tagName === 'ION-NEW-COMPONENT') {
812+
super.registerOnChange((value: string) => {
813+
fn(value === '' ? null : parseFloat(value));
814+
});
815+
} else {
816+
super.registerOnChange(fn);
817+
}
818+
}
819+
```
820+
794821
#### Standalone Directive
795822

796823
For standalone components, create a directive in the [standalone package](/packages/angular/standalone/src/directives). Look at the implementation of the most similar existing component as a reference:

0 commit comments

Comments
 (0)