Skip to content

Commit 6575d9c

Browse files
crisbetojelbourn
authored andcommitted
refactor(input): run ios keyup event outside the ngzone (#10583)
Since the event doesn't do anything related to Angular, it should run outside the NgZone.
1 parent 39887da commit 6575d9c

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/lib/input/input.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
OnInit,
2020
Optional,
2121
Self,
22+
NgZone,
2223
} from '@angular/core';
2324
import {FormGroupDirective, NgControl, NgForm} from '@angular/forms';
2425
import {CanUpdateErrorState, ErrorStateMatcher, mixinErrorState} from '@angular/material/core';
@@ -218,7 +219,8 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
218219
@Optional() _parentFormGroup: FormGroupDirective,
219220
_defaultErrorStateMatcher: ErrorStateMatcher,
220221
@Optional() @Self() @Inject(MAT_INPUT_VALUE_ACCESSOR) inputValueAccessor: any,
221-
private _autofillMonitor: AutofillMonitor) {
222+
private _autofillMonitor: AutofillMonitor,
223+
ngZone: NgZone) {
222224
super(_defaultErrorStateMatcher, _parentForm, _parentFormGroup, ngControl);
223225
// If no input value accessor was explicitly specified, use the element as the input value
224226
// accessor.
@@ -233,15 +235,18 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
233235
// key. In order to get around this we need to "jiggle" the caret loose. Since this bug only
234236
// exists on iOS, we only bother to install the listener on iOS.
235237
if (_platform.IOS) {
236-
_elementRef.nativeElement.addEventListener('keyup', (event: Event) => {
237-
let el = event.target as HTMLInputElement;
238-
if (!el.value && !el.selectionStart && !el.selectionEnd) {
239-
// Note: Just setting `0, 0` doesn't fix the issue. Setting `1, 1` fixes it for the first
240-
// time that you type text and then hold delete. Toggling to `1, 1` and then back to
241-
// `0, 0` seems to completely fix it.
242-
el.setSelectionRange(1, 1);
243-
el.setSelectionRange(0, 0);
244-
}
238+
ngZone.runOutsideAngular(() => {
239+
_elementRef.nativeElement.addEventListener('keyup', (event: Event) => {
240+
let el = event.target as HTMLInputElement;
241+
if (!el.value && !el.selectionStart && !el.selectionEnd) {
242+
// Note: Just setting `0, 0` doesn't fix the issue. Setting
243+
// `1, 1` fixes it for the first time that you type text and
244+
// then hold delete. Toggling to `1, 1` and then back to
245+
// `0, 0` seems to completely fix it.
246+
el.setSelectionRange(1, 1);
247+
el.setSelectionRange(0, 0);
248+
}
249+
});
245250
});
246251
}
247252

0 commit comments

Comments
 (0)