Skip to content

Commit

Permalink
Simulate input event instead of relying on native
Browse files Browse the repository at this point in the history
Test Plan:
With the ballmer-peak example (modified to use input), tested that the
percentage updates when adding or deleting text in the field on Chrome
and IE9. After adding es5-shim and es5-sham to the ballmer-peak page,
IE8 works properly too.
  • Loading branch information
sophiebits committed Jun 1, 2013
1 parent 2467c0e commit 580e8f0
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/core/ReactDefaultInjection.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var ReactDOMForm = require('ReactDOMForm');
var DefaultEventPluginOrder = require('DefaultEventPluginOrder');
var EnterLeaveEventPlugin = require('EnterLeaveEventPlugin');
var EventPluginHub = require('EventPluginHub');
var InputEventPlugin = require('InputEventPlugin');
var ReactInstanceHandles = require('ReactInstanceHandles');
var SimpleEventPlugin = require('SimpleEventPlugin');

Expand All @@ -40,7 +41,8 @@ function inject() {
*/
EventPluginHub.injection.injectEventPluginsByName({
'SimpleEventPlugin': SimpleEventPlugin,
'EnterLeaveEventPlugin': EnterLeaveEventPlugin
'EnterLeaveEventPlugin': EnterLeaveEventPlugin,
'InputEventPlugin': InputEventPlugin
});

/*
Expand Down
4 changes: 3 additions & 1 deletion src/core/ReactEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,10 @@ function listenAtTopLevel(touchNotMouse) {
trapBubbledEvent(topLevelTypes.topKeyUp, 'keyup', mountAt);
trapBubbledEvent(topLevelTypes.topKeyPress, 'keypress', mountAt);
trapBubbledEvent(topLevelTypes.topKeyDown, 'keydown', mountAt);
trapBubbledEvent(topLevelTypes.topInput, 'input', mountAt);
trapBubbledEvent(topLevelTypes.topChange, 'change', mountAt);
trapBubbledEvent(topLevelTypes.topInput, 'input', mountAt);
trapBubbledEvent(topLevelTypes.topCut, 'cut', mountAt);
trapBubbledEvent(topLevelTypes.topPaste, 'paste', mountAt);
trapBubbledEvent(
topLevelTypes.topDOMCharacterDataModified,
'DOMCharacterDataModified',
Expand Down
2 changes: 2 additions & 0 deletions src/event/EventConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var topLevelTypes = keyMirror({
topBlur: null,
topChange: null,
topClick: null,
topCut: null,
topDOMCharacterDataModified: null,
topDoubleClick: null,
topFocus: null,
Expand All @@ -42,6 +43,7 @@ var topLevelTypes = keyMirror({
topMouseOver: null,
topMouseUp: null,
topMouseWheel: null,
topPaste: null,
topScroll: null,
topSubmit: null,
topTouchCancel: null,
Expand Down
1 change: 1 addition & 0 deletions src/eventPlugins/DefaultEventPluginOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var DefaultEventPluginOrder = [
keyOf({SimpleEventPlugin: null}),
keyOf({TapEventPlugin: null}),
keyOf({EnterLeaveEventPlugin: null}),
keyOf({InputEventPlugin: null}),
keyOf({AnalyticsEventPlugin: null})
];

Expand Down
99 changes: 99 additions & 0 deletions src/eventPlugins/InputEventPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @providesModule InputEventPlugin
*/

"use strict";

var AbstractEvent = require('AbstractEvent');
var EventConstants = require('EventConstants');
var EventPluginUtils = require('EventPluginUtils');
var EventPropagators = require('EventPropagators');

var keyOf = require('keyOf');

var topLevelTypes = EventConstants.topLevelTypes;

var abstractEventTypes = {
input: {
phasedRegistrationNames: {
bubbled: keyOf({onInput: null}),
captured: keyOf({onInputCapture: null})
}
}
};

/**
* @see EventPluginHub.extractAbstractEvents
*/
var extractAbstractEvents = function(
topLevelType,
nativeEvent,
renderedTargetID,
renderedTarget) {

var defer, key;
switch (topLevelType) {
case topLevelTypes.topInput:
// When the native input event is triggered, we definitely want to
// forward it along. However, IE9's input event doesn't get triggered
// when deleting text, and IE8 doesn't support input at all, so we
// simulate it on change, cut, paste, and keydown.
case topLevelTypes.topChange:
defer = false;
break;
case topLevelTypes.topCut:
case topLevelTypes.topPaste:
defer = true;
break;
case topLevelTypes.topKeyDown:
key = nativeEvent.keyCode;
// Ignore command, modifiers, and arrow keys, respectively
if (key === 91 || (15 < key && key < 19) || (37 <= key && key <= 40)) {
return;
}
defer = true;
break;
default:
return;
}

var type = abstractEventTypes.input;
var abstractTargetID = renderedTargetID;
var abstractEvent = AbstractEvent.getPooled(
type,
abstractTargetID,
topLevelType,
nativeEvent
);
EventPropagators.accumulateTwoPhaseDispatches(abstractEvent);

if (defer) {
setTimeout(function() {
EventPluginHub.enqueueAbstractEvents(abstractEvent);
EventPluginHub.processAbstractEventQueue();
}, 0);
} else {
return abstractEvent;
}
};

var InputEventPlugin = {
abstractEventTypes: abstractEventTypes,
extractAbstractEvents: extractAbstractEvents
};

module.exports = InputEventPlugin;
7 changes: 0 additions & 7 deletions src/eventPlugins/SimpleEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@ var SimpleEventPlugin = {
captured: keyOf({onKeyDownCapture: true})
}
},
input: {
phasedRegistrationNames: {
bubbled: keyOf({onInput: true}),
captured: keyOf({onInputCapture: true})
}
},
focus: {
phasedRegistrationNames: {
bubbled: keyOf({onFocus: true}),
Expand Down Expand Up @@ -226,7 +220,6 @@ SimpleEventPlugin.topLevelTypesToAbstract = {
topKeyUp: SimpleEventPlugin.abstractEventTypes.keyUp,
topKeyPress: SimpleEventPlugin.abstractEventTypes.keyPress,
topKeyDown: SimpleEventPlugin.abstractEventTypes.keyDown,
topInput: SimpleEventPlugin.abstractEventTypes.input,
topFocus: SimpleEventPlugin.abstractEventTypes.focus,
topBlur: SimpleEventPlugin.abstractEventTypes.blur,
topScroll: SimpleEventPlugin.abstractEventTypes.scroll,
Expand Down

0 comments on commit 580e8f0

Please sign in to comment.