Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Commit

Permalink
fix(text-field): Prevent synthetic event on touch devices (#1613)
Browse files Browse the repository at this point in the history
  • Loading branch information
trimox authored Dec 17, 2018
1 parent c706980 commit b158a7c
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/textfield/text-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ export const _MdcTextFieldMixinBase: CanUpdateErrorStateCtor & typeof MdcTextFie

let nextUniqueId = 0;

/**
* Time in milliseconds for which to ignore mouse events, after
* receiving a touch event. Used to avoid doing double work for
* touch devices where the browser fires fake mouse events, in
* addition to touch events.
*/
const MOUSE_EVENT_IGNORE_TIME = 800;

@Component({
moduleId: module.id,
selector: 'mdc-text-field',
Expand Down Expand Up @@ -108,6 +116,9 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
private _uid = `mdc-input-${nextUniqueId++}`;
private _initialized: boolean = false;

/** Time in milliseconds when the last touchstart event happened. */
private _lastTouchStartEvent: number = 0;

controlType: string = 'mdc-text-field';

@Input() label: string | null = null;
Expand Down Expand Up @@ -406,6 +417,17 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
}

onInputInteraction(evt: MouseEvent | TouchEvent): void {
if (evt instanceof MouseEvent) {
const isSyntheticEvent = this._lastTouchStartEvent &&
Date.now() < this._lastTouchStartEvent + MOUSE_EVENT_IGNORE_TIME;

if (isSyntheticEvent) {
return;
}
} else {
this._lastTouchStartEvent = Date.now();
}

this._foundation.setTransformOrigin(evt);
}

Expand Down

0 comments on commit b158a7c

Please sign in to comment.