Skip to content

Commit 4f6cab5

Browse files
author
Nicolas Gallagher
authored
[Flare] Ignore keyboard interactions on text input children (#15810)
1 parent fa1e8df commit 4f6cab5

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

packages/react-events/src/Press.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,17 @@ function dispatchCancel(
414414
}
415415
}
416416

417-
function isValidKeyPress(key: string): boolean {
417+
function isValidKeyboardEvent(nativeEvent: Object): boolean {
418+
const {key, target} = nativeEvent;
419+
const {tagName, isContentEditable} = target;
418420
// Accessibility for keyboards. Space and Enter only.
419421
// "Spacebar" is for IE 11
420-
return key === 'Enter' || key === ' ' || key === 'Spacebar';
422+
return (
423+
(key === 'Enter' || key === ' ' || key === 'Spacebar') &&
424+
(tagName !== 'INPUT' &&
425+
tagName !== 'TEXTAREA' &&
426+
isContentEditable !== true)
427+
);
421428
}
422429

423430
function calculateDelayMS(delay: ?number, min = 0, fallback = 0) {
@@ -671,7 +678,7 @@ const PressResponder = {
671678

672679
// Ignore unrelated key events
673680
if (pointerType === 'keyboard') {
674-
if (!isValidKeyPress(nativeEvent.key)) {
681+
if (!isValidKeyboardEvent(nativeEvent)) {
675682
return;
676683
}
677684
}
@@ -717,7 +724,7 @@ const PressResponder = {
717724
addRootEventTypes(context, state);
718725
} else {
719726
// Prevent spacebar press from scrolling the window
720-
if (isValidKeyPress(nativeEvent.key) && nativeEvent.key === ' ') {
727+
if (isValidKeyboardEvent(nativeEvent) && nativeEvent.key === ' ') {
721728
nativeEvent.preventDefault();
722729
}
723730
}
@@ -859,7 +866,7 @@ const PressResponder = {
859866
// Ignore unrelated keyboard events and verify press is within
860867
// responder region for non-keyboard events.
861868
if (pointerType === 'keyboard') {
862-
if (!isValidKeyPress(nativeEvent.key)) {
869+
if (!isValidKeyboardEvent(nativeEvent)) {
863870
return;
864871
}
865872
// If the event target isn't within the press target, check if we're still

packages/react-events/src/__tests__/Press-test.internal.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,27 @@ describe('Event responder: Press', () => {
561561
);
562562
});
563563

564+
it('is not called after invalid "keyup" event', () => {
565+
const inputRef = React.createRef();
566+
const element = (
567+
<Press onPress={onPress}>
568+
<input ref={inputRef} />
569+
</Press>
570+
);
571+
ReactDOM.render(element, container);
572+
inputRef.current.dispatchEvent(
573+
createKeyboardEvent('keydown', {key: 'Enter'}),
574+
);
575+
inputRef.current.dispatchEvent(
576+
createKeyboardEvent('keyup', {key: 'Enter'}),
577+
);
578+
inputRef.current.dispatchEvent(
579+
createKeyboardEvent('keydown', {key: ' '}),
580+
);
581+
inputRef.current.dispatchEvent(createKeyboardEvent('keyup', {key: ' '}));
582+
expect(onPress).not.toBeCalled();
583+
});
584+
564585
it('is always called immediately after press is released', () => {
565586
const element = (
566587
<Press delayPressEnd={500} onPress={onPress}>

0 commit comments

Comments
 (0)