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

Commit b158a7c

Browse files
authored
fix(text-field): Prevent synthetic event on touch devices (#1613)
1 parent c706980 commit b158a7c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

packages/textfield/text-field.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ export const _MdcTextFieldMixinBase: CanUpdateErrorStateCtor & typeof MdcTextFie
5151

5252
let nextUniqueId = 0;
5353

54+
/**
55+
* Time in milliseconds for which to ignore mouse events, after
56+
* receiving a touch event. Used to avoid doing double work for
57+
* touch devices where the browser fires fake mouse events, in
58+
* addition to touch events.
59+
*/
60+
const MOUSE_EVENT_IGNORE_TIME = 800;
61+
5462
@Component({
5563
moduleId: module.id,
5664
selector: 'mdc-text-field',
@@ -108,6 +116,9 @@ export class MdcTextField extends _MdcTextFieldMixinBase implements AfterViewIni
108116
private _uid = `mdc-input-${nextUniqueId++}`;
109117
private _initialized: boolean = false;
110118

119+
/** Time in milliseconds when the last touchstart event happened. */
120+
private _lastTouchStartEvent: number = 0;
121+
111122
controlType: string = 'mdc-text-field';
112123

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

408419
onInputInteraction(evt: MouseEvent | TouchEvent): void {
420+
if (evt instanceof MouseEvent) {
421+
const isSyntheticEvent = this._lastTouchStartEvent &&
422+
Date.now() < this._lastTouchStartEvent + MOUSE_EVENT_IGNORE_TIME;
423+
424+
if (isSyntheticEvent) {
425+
return;
426+
}
427+
} else {
428+
this._lastTouchStartEvent = Date.now();
429+
}
430+
409431
this._foundation.setTransformOrigin(evt);
410432
}
411433

0 commit comments

Comments
 (0)