Skip to content

Commit 5f8358c

Browse files
committed
Add flow to SyntheticEvent
1 parent dab0854 commit 5f8358c

3 files changed

Lines changed: 26 additions & 18 deletions

File tree

packages/react-dom/src/events/DOMPluginEventSystem.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ function accumulateEnterLeaveListenersForEvent(
923923
inCapturePhase: boolean,
924924
): void {
925925
const registrationName = event._reactName;
926-
if (registrationName === undefined) {
926+
if (registrationName === null) {
927927
return;
928928
}
929929
const listeners: Array<DispatchListener> = [];

packages/react-dom/src/events/ReactSyntheticEventType.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type ReactSyntheticEvent = {|
2727
isPropagationStopped: () => boolean,
2828
_dispatchInstances?: null | Array<Fiber | null> | Fiber,
2929
_dispatchListeners?: null | Array<Function> | Function,
30-
_reactName: string,
30+
_reactName: string | null,
3131
_targetInst: Fiber,
3232
nativeEvent: Event,
3333
type: string,

packages/react-dom/src/events/SyntheticEvent.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
68
*/
79

810
/* eslint valid-typeof: 0 */
911

1012
import getEventCharCode from './getEventCharCode';
1113

14+
type EventInterfaceType = {
15+
[propName: string]: 0 | ((event: {[propName: string]: mixed}) => mixed),
16+
};
17+
1218
/**
1319
* @interface Event
1420
* @see http://www.w3.org/TR/DOM-Level-3-Events/
1521
*/
16-
const EventInterface = {
22+
const EventInterface: EventInterfaceType = {
1723
eventPhase: 0,
1824
bubbles: 0,
1925
cancelable: 0,
@@ -46,12 +52,12 @@ function functionThatReturnsFalse() {
4652
* DOM interface; custom application-specific events can also subclass this.
4753
*/
4854
export function SyntheticEvent(
49-
reactName,
50-
reactEventType,
51-
targetInst,
52-
nativeEvent,
53-
nativeEventTarget,
54-
Interface = EventInterface,
55+
reactName: string | null,
56+
reactEventType: string,
57+
targetInst: Fiber,
58+
nativeEvent: {[propName: string]: mixed},
59+
nativeEventTarget: null | EventTarget,
60+
Interface: EventInterfaceType = EventInterface,
5561
) {
5662
this._reactName = reactName;
5763
this._targetInst = targetInst;
@@ -95,6 +101,7 @@ Object.assign(SyntheticEvent.prototype, {
95101

96102
if (event.preventDefault) {
97103
event.preventDefault();
104+
// $FlowFixMe - flow is not aware of `unknown` in IE
98105
} else if (typeof event.returnValue !== 'unknown') {
99106
event.returnValue = false;
100107
}
@@ -109,6 +116,7 @@ Object.assign(SyntheticEvent.prototype, {
109116

110117
if (event.stopPropagation) {
111118
event.stopPropagation();
119+
// $FlowFixMe - flow is not aware of `unknown` in IE
112120
} else if (typeof event.cancelBubble !== 'unknown') {
113121
// The ChangeEventPlugin registers a "propertychange" event for
114122
// IE. This event does not support bubbling or cancelling, and
@@ -138,7 +146,7 @@ Object.assign(SyntheticEvent.prototype, {
138146
isPersistent: functionThatReturnsTrue,
139147
});
140148

141-
export const UIEventInterface = {
149+
export const UIEventInterface: EventInterfaceType = {
142150
...EventInterface,
143151
view: 0,
144152
detail: 0,
@@ -154,7 +162,7 @@ let isMovementYSet = false;
154162
* @interface MouseEvent
155163
* @see http://www.w3.org/TR/DOM-Level-3-Events/
156164
*/
157-
export const MouseEventInterface = {
165+
export const MouseEventInterface: EventInterfaceType = {
158166
...UIEventInterface,
159167
screenX: 0,
160168
screenY: 0,
@@ -213,7 +221,7 @@ export const MouseEventInterface = {
213221
* @interface DragEvent
214222
* @see http://www.w3.org/TR/DOM-Level-3-Events/
215223
*/
216-
export const DragEventInterface = {
224+
export const DragEventInterface: EventInterfaceType = {
217225
...MouseEventInterface,
218226
dataTransfer: 0,
219227
};
@@ -222,7 +230,7 @@ export const DragEventInterface = {
222230
* @interface FocusEvent
223231
* @see http://www.w3.org/TR/DOM-Level-3-Events/
224232
*/
225-
export const FocusEventInterface = {
233+
export const FocusEventInterface: EventInterfaceType = {
226234
...UIEventInterface,
227235
relatedTarget: 0,
228236
};
@@ -232,7 +240,7 @@ export const FocusEventInterface = {
232240
* @see http://www.w3.org/TR/css3-animations/#AnimationEvent-interface
233241
* @see https://developer.mozilla.org/en-US/docs/Web/API/AnimationEvent
234242
*/
235-
export const AnimationEventInterface = {
243+
export const AnimationEventInterface: EventInterfaceType = {
236244
...EventInterface,
237245
animationName: 0,
238246
elapsedTime: 0,
@@ -243,9 +251,9 @@ export const AnimationEventInterface = {
243251
* @interface Event
244252
* @see http://www.w3.org/TR/clipboard-apis/
245253
*/
246-
export const ClipboardEventInterface = {
254+
export const ClipboardEventInterface: EventInterfaceType = {
247255
...EventInterface,
248-
clipboardData: function(event) {
256+
clipboardData: function(event: {[propName: string]: mixed}) {
249257
return 'clipboardData' in event
250258
? event.clipboardData
251259
: window.clipboardData;
@@ -256,7 +264,7 @@ export const ClipboardEventInterface = {
256264
* @interface Event
257265
* @see http://www.w3.org/TR/DOM-Level-3-Events/#events-compositionevents
258266
*/
259-
export const CompositionEventInterface = {
267+
export const CompositionEventInterface: EventInterfaceType = {
260268
...EventInterface,
261269
data: 0,
262270
};
@@ -267,7 +275,7 @@ export const CompositionEventInterface = {
267275
* /#events-inputevents
268276
*/
269277
// Happens to share the same list for now.
270-
export const InputEventInterface = CompositionEventInterface;
278+
export const InputEventInterface: EventInterfaceType = CompositionEventInterface;
271279

272280
/**
273281
* Normalization of deprecated HTML5 `key` values

0 commit comments

Comments
 (0)