Skip to content

Commit

Permalink
Always pass React event type and fix beforeinput
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Aug 10, 2020
1 parent 1f4866d commit f98cb9b
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1440,8 +1440,7 @@ describe('ReactDOMEventListener', () => {
log.push('- outer parent');
},
onBeforeInputCapture: e => {
// FIXME: this should be beforeinput for consistency.
expect(e.type).toBe('textInput');
expect(e.type).toBe('beforeinput');
log.push('- outer parent capture');
},
}}
Expand Down
3 changes: 2 additions & 1 deletion packages/react-dom/src/events/SyntheticEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import getEventCharCode from './getEventCharCode';
* @see http://www.w3.org/TR/DOM-Level-3-Events/
*/
const EventInterface = {
type: 0,
eventPhase: 0,
bubbles: 0,
cancelable: 0,
Expand Down Expand Up @@ -48,13 +47,15 @@ function functionThatReturnsFalse() {
*/
export function SyntheticEvent(
reactName,
reactEventType,
targetInst,
nativeEvent,
nativeEventTarget,
Interface = EventInterface,
) {
this._reactName = reactName;
this._targetInst = targetInst;
this.type = reactEventType;
this.nativeEvent = nativeEvent;
this.target = nativeEventTarget;
this.currentTarget = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ function extractCompositionEvent(

const event = new SyntheticEvent(
eventType,
domEventName,
null,
nativeEvent,
nativeEventTarget,
Expand Down Expand Up @@ -397,6 +398,7 @@ function extractBeforeInputEvent(

const event = new SyntheticEvent(
'onBeforeInput',
'beforeinput',
null,
nativeEvent,
nativeEventTarget,
Expand Down
9 changes: 7 additions & 2 deletions packages/react-dom/src/events/plugins/ChangeEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ function createAndAccumulateChangeEvent(
nativeEvent,
target,
) {
const event = new SyntheticEvent('onChange', null, nativeEvent, target);
event.type = 'change';
const event = new SyntheticEvent(
'onChange',
'change',
null,
nativeEvent,
target,
);
// Flag this event loop as needing state restore.
enqueueStateRestore(((target: any): Node));
accumulateTwoPhaseListeners(inst, dispatchQueue, event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,23 @@ function extractEvents(

const leave = new SyntheticEvent(
leaveEventType,
eventTypePrefix + 'leave',
from,
nativeEvent,
nativeEventTarget,
eventInterface,
);
leave.type = eventTypePrefix + 'leave';
leave.target = fromNode;
leave.relatedTarget = toNode;

let enter = new SyntheticEvent(
enterEventType,
eventTypePrefix + 'enter',
to,
nativeEvent,
nativeEventTarget,
eventInterface,
);
enter.type = eventTypePrefix + 'enter';
enter.target = toNode;
enter.relatedTarget = fromNode;

Expand Down
3 changes: 1 addition & 2 deletions packages/react-dom/src/events/plugins/SelectEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,11 @@ function constructSelectEvent(dispatchQueue, nativeEvent, nativeEventTarget) {

const syntheticEvent = new SyntheticEvent(
'onSelect',
'select',
null,
nativeEvent,
nativeEventTarget,
);

syntheticEvent.type = 'select';
syntheticEvent.target = activeElement;

accumulateTwoPhaseListeners(
Expand Down
10 changes: 4 additions & 6 deletions packages/react-dom/src/events/plugins/SimpleEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function extractEvents(
return;
}
let EventInterface;
let syntheticType;
let reactEventType = domEventName;
switch (domEventName) {
case 'keypress':
// Firefox creates a keypress event for function keys too. This removes
Expand All @@ -78,11 +78,11 @@ function extractEvents(
EventInterface = KeyboardEventInterface;
break;
case 'focusin':
syntheticType = 'focus';
reactEventType = 'focus';
EventInterface = FocusEventInterface;
break;
case 'focusout':
syntheticType = 'blur';
reactEventType = 'blur';
EventInterface = FocusEventInterface;
break;
case 'beforeblur':
Expand Down Expand Up @@ -159,14 +159,12 @@ function extractEvents(
}
const event = new SyntheticEvent(
reactName,
reactEventType,
null,
nativeEvent,
nativeEventTarget,
EventInterface,
);
if (syntheticType) {
event.type = syntheticType;
}

const inCapturePhase = (eventSystemFlags & IS_CAPTURE_PHASE) !== 0;
if (
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/src/test-utils/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ function makeSimulator(eventType) {
const targetInst = getInstanceFromNode(domNode);
const event = new SyntheticEvent(
reactName,
fakeNativeEvent.type,
targetInst,
fakeNativeEvent,
domNode,
Expand Down

0 comments on commit f98cb9b

Please sign in to comment.