Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(input): prevent input caret from sticking on iOS #6128

Merged
merged 1 commit into from
Jul 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/lib/input/input-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@
*/

import {
AfterContentInit,
AfterContentChecked,
AfterContentInit,
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChild,
ContentChildren,
Directive,
DoCheck,
ElementRef,
Inject,
Input,
OnChanges,
OnDestroy,
Optional,
QueryList,
Renderer2,
Self,
ViewChild,
ViewEncapsulation,
Inject,
ChangeDetectionStrategy,
OnChanges,
OnDestroy,
DoCheck,
} from '@angular/core';
import {animate, state, style, transition, trigger} from '@angular/animations';
import {coerceBooleanProperty, Platform} from '../core';
import {FormGroupDirective, NgControl, NgForm, FormControl} from '@angular/forms';
import {FormControl, FormGroupDirective, NgControl, NgForm} from '@angular/forms';
import {getSupportedInputTypes} from '../core/platform/features';
import {
getMdInputContainerDuplicatedHintError,
Expand All @@ -41,13 +41,13 @@ import {
} from './input-container-errors';
import {
FloatPlaceholderType,
PlaceholderOptions,
MD_PLACEHOLDER_GLOBAL_OPTIONS
MD_PLACEHOLDER_GLOBAL_OPTIONS,
PlaceholderOptions
} from '../core/placeholder/placeholder-options';
import {
defaultErrorStateMatcher,
ErrorStateMatcher,
ErrorOptions,
ErrorStateMatcher,
MD_ERROR_GLOBAL_OPTIONS
} from '../core/error/error-options';
import {Subject} from 'rxjs/Subject';
Expand Down Expand Up @@ -138,7 +138,6 @@ export class MdSuffix {}
}
})
export class MdInputDirective implements OnChanges, OnDestroy, DoCheck {

/** Variables used as cache for getters and setters. */
private _type = 'text';
private _placeholder: string = '';
Expand Down Expand Up @@ -232,7 +231,6 @@ export class MdInputDirective implements OnChanges, OnDestroy, DoCheck {
constructor(private _elementRef: ElementRef,
private _renderer: Renderer2,
private _platform: Platform,
private _changeDetectorRef: ChangeDetectorRef,
@Optional() @Self() public _ngControl: NgControl,
@Optional() private _parentForm: NgForm,
@Optional() private _parentFormGroup: FormGroupDirective,
Expand All @@ -242,6 +240,22 @@ export class MdInputDirective implements OnChanges, OnDestroy, DoCheck {
this.id = this.id;
this._errorOptions = errorOptions ? errorOptions : {};
this.errorStateMatcher = this._errorOptions.errorStateMatcher || defaultErrorStateMatcher;

// On some versions of iOS the caret gets stuck in the wrong place when holding down the delete
// key. In order to get around this we need to "jiggle" the caret loose. Since this bug only
// exists on iOS, we only bother to install the listener on iOS.
if (_platform.IOS) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does mdInput do that causes this behavior in the first place? (vs. any other native input on ios)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's the fact that it causes relayout in response to input events sometimes (e.g. when showing errors) I created a minimal repro using native inputs which I posted on the associated bug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack. Can file figure out if this is Safari specifically or Webkit in general? I suspect it's probably webkit, for which we can file a bug.

_renderer.listen(_elementRef.nativeElement, 'keyup', (event: Event) => {
let el = event.target as HTMLInputElement;
if (!el.value && !el.selectionStart && !el.selectionEnd) {
// Note: Just setting `0, 0` doesn't fix the issue. Setting `1, 1` fixes it for the first
// time that you type text and then hold delete. Toggling to `1, 1` and then back to
// `0, 0` seems to completely fix it.
el.setSelectionRange(1, 1);
el.setSelectionRange(0, 0);
}
});
}
}

ngOnChanges() {
Expand Down