MCPcopy Create free account
hub / github.com/krasimir/react-in-patterns / getNativeBeforeInputChars

Function getNativeBeforeInputChars

code/event-handlers/public/app.js:2664–2708  ·  view source on GitHub ↗

* @param {TopLevelTypes} topLevelType Record from `BrowserEventConstants`. * @param {object} nativeEvent Native browser event. * @return {?string} The string corresponding to this `beforeInput` event.

(topLevelType, nativeEvent)

Source from the content-addressed store, hash-verified

2662 * @return {?string} The string corresponding to this `beforeInput` event.
2663 */
2664function getNativeBeforeInputChars(topLevelType, nativeEvent) {
2665 switch (topLevelType) {
2666 case 'topCompositionEnd':
2667 return getDataFromCustomEvent(nativeEvent);
2668 case 'topKeyPress':
2669 /**
2670 * If native `textInput` events are available, our goal is to make
2671 * use of them. However, there is a special case: the spacebar key.
2672 * In Webkit, preventing default on a spacebar `textInput` event
2673 * cancels character insertion, but it *also* causes the browser
2674 * to fall back to its default spacebar behavior of scrolling the
2675 * page.
2676 *
2677 * Tracking at:
2678 * https://code.google.com/p/chromium/issues/detail?id=355103
2679 *
2680 * To avoid this issue, use the keypress event as if no `textInput`
2681 * event is available.
2682 */
2683 var which = nativeEvent.which;
2684 if (which !== SPACEBAR_CODE) {
2685 return null;
2686 }
2687
2688 hasSpaceKeypress = true;
2689 return SPACEBAR_CHAR;
2690
2691 case 'topTextInput':
2692 // Record the characters to be added to the DOM.
2693 var chars = nativeEvent.data;
2694
2695 // If it's a spacebar character, assume that we have already handled
2696 // it at the keypress level and bail immediately. Android Chrome
2697 // doesn't give us keycodes, so we need to blacklist it.
2698 if (chars === SPACEBAR_CHAR && hasSpaceKeypress) {
2699 return null;
2700 }
2701
2702 return chars;
2703
2704 default:
2705 // For other native event types, do nothing.
2706 return null;
2707 }
2708}
2709
2710/**
2711 * For browsers that do not provide the `textInput` event, extract the

Callers 1

extractBeforeInputEventFunction · 0.70

Calls 1

getDataFromCustomEventFunction · 0.70

Tested by

no test coverage detected