Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pressing enter doesn't select the emoji category #50971

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/components/EmojiPicker/EmojiPickerMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ function EmojiPickerMenu({onEmojiSelected, activeEmoji}: EmojiPickerMenuProps, r
setFilteredEmojis(allEmojis);
setHeaderIndices(headerRowIndices);
setFocusedIndex(-1);
setHighlightFirstEmoji(false);
setHighlightEmoji(false);
return;
}
Expand All @@ -139,14 +140,6 @@ function EmojiPickerMenu({onEmojiSelected, activeEmoji}: EmojiPickerMenuProps, r
return;
}

if (!isEnterWhileComposition(keyBoardEvent) && keyBoardEvent.key === CONST.KEYBOARD_SHORTCUTS.ENTER.shortcutKey) {
// On web, avoid this Enter default input action; otherwise, it will add a new line in the subsequently focused composer.
keyBoardEvent.preventDefault();
// On mWeb, avoid propagating this Enter keystroke to Pressable child component; otherwise, it will trigger the onEmojiSelected callback again.
keyBoardEvent.stopPropagation();
return;
}
Comment on lines -142 to -148
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you remove the whole condition, Shouldn't we keep the return early here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it because it won't affect the code below it.

// Enable keyboard movement if tab or enter is pressed or if shift is pressed while the input
// is not focused, so that the navigation and tab cycling can be done using the keyboard without
// interfering with the input behaviour.
if (keyBoardEvent.key === 'Tab' || keyBoardEvent.key === 'Enter' || (keyBoardEvent.key === 'Shift' && searchInputRef.current && !isTextInputFocused(searchInputRef))) {
setIsUsingKeyboardMovement(true);
}
// We allow typing in the search box if any key is pressed apart from Arrow keys.
if (searchInputRef.current && !isTextInputFocused(searchInputRef) && ReportUtils.shouldAutoFocusOnKeyPress(keyBoardEvent)) {
searchInputRef.current.focus();
}
},

For

// We allow typing in the search box if any key is pressed apart from Arrow keys.
if (searchInputRef.current && !isTextInputFocused(searchInputRef) && ReportUtils.shouldAutoFocusOnKeyPress(keyBoardEvent)) {
searchInputRef.current.focus();
}

it's only executed if shouldAutoFocusOnKeyPress is true which is false for ENTER.

App/src/libs/ReportUtils.ts

Lines 7595 to 7598 in c3d33fc

function shouldAutoFocusOnKeyPress(event: KeyboardEvent): boolean {
if (event.key.length > 1) {
return false;
}

For

// Enable keyboard movement if tab or enter is pressed or if shift is pressed while the input
// is not focused, so that the navigation and tab cycling can be done using the keyboard without
// interfering with the input behaviour.
if (keyBoardEvent.key === 'Tab' || keyBoardEvent.key === 'Enter' || (keyBoardEvent.key === 'Shift' && searchInputRef.current && !isTextInputFocused(searchInputRef))) {
setIsUsingKeyboardMovement(true);
}

the keyboard movement state is used to show the focused state of emoji item.

const isEmojiFocused = index === focusedIndex && isUsingKeyboardMovement;

The emoji will be focused when we use arrow key, tab, or hold down mouse on it. The time we press Enter, the emoji will be selected. From my understanding, the isUsingKeyboardMovement state is used to not show the blue outline on the emoji when focused using mouse.

But it will still show anyway with the mouse because when focused, we update the focused index.

onFocus={() => setFocusedIndex(index)}

which always update isUsingKeyboardMovement to true.

const onFocusedIndexChange = useCallback(
(newIndex: number) => {
if (filteredEmojis.length === 0) {
return;
}
if (highlightFirstEmoji) {
setHighlightFirstEmoji(false);
}
if (!isUsingKeyboardMovement) {
setIsUsingKeyboardMovement(true);
}

We can remove Enter from the if if you want.

if (keyBoardEvent.key === 'Tab' || keyBoardEvent.key === 'Enter' || (keyBoardEvent.key === 'Shift' && searchInputRef.current && !isTextInputFocused(searchInputRef))) {


// Enable keyboard movement if tab or enter is pressed or if shift is pressed while the input
// is not focused, so that the navigation and tab cycling can be done using the keyboard without
// interfering with the input behaviour.
Expand Down Expand Up @@ -182,9 +175,13 @@ function EmojiPickerMenu({onEmojiSelected, activeEmoji}: EmojiPickerMenuProps, r
if ('types' in item || 'name' in item) {
const emoji = typeof preferredSkinTone === 'number' && preferredSkinTone !== -1 && item?.types?.at(preferredSkinTone) ? item.types.at(preferredSkinTone) : item.code;
onEmojiSelected(emoji ?? '', item);
// On web, avoid this Enter default input action; otherwise, it will add a new line in the subsequently focused composer.
keyBoardEvent.preventDefault();
// On mWeb, avoid propagating this Enter keystroke to Pressable child component; otherwise, it will trigger the onEmojiSelected callback again.
keyBoardEvent.stopPropagation();
}
},
{shouldPreventDefault: true, shouldStopPropagation: true},
{shouldPreventDefault: false},
);

/**
Expand Down
Loading