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: picker an arrow navigation #3871

Merged
merged 7 commits into from
Jul 9, 2021
Merged
Changes from 4 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
63 changes: 33 additions & 30 deletions src/pages/home/report/EmojiPickerMenu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class EmojiPickerMenu extends Component {
this.filterEmojis = _.debounce(this.filterEmojis.bind(this), 300);
this.highlightAdjacentEmoji = this.highlightAdjacentEmoji.bind(this);
this.scrollToHighlightedIndex = this.scrollToHighlightedIndex.bind(this);
this.toggleArrowKeysOnSearchInput = this.toggleArrowKeysOnSearchInput.bind(this);
this.setupEventHandlers = this.setupEventHandlers.bind(this);
this.cleanupEventHandlers = this.cleanupEventHandlers.bind(this);
this.renderItem = this.renderItem.bind(this);
Expand Down Expand Up @@ -97,10 +96,6 @@ class EmojiPickerMenu extends Component {
if (document) {
this.keyDownHandler = (keyBoardEvent) => {
if (keyBoardEvent.key.startsWith('Arrow')) {
// Depending on the position of the highlighted emoji after moving and rendering,
// toggle which arrow keys can affect the cursor position in the search input.
this.toggleArrowKeysOnSearchInput(keyBoardEvent);

// Move the highlight when arrow keys are pressed
this.highlightAdjacentEmoji(keyBoardEvent.key);
}
Expand All @@ -110,7 +105,9 @@ class EmojiPickerMenu extends Component {
this.props.onEmojiSelected(this.state.filteredEmojis[this.state.highlightedIndex].code);
}
};
document.addEventListener('keydown', this.keyDownHandler);

// Keyboard events are not bubbling in RN-Web
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment isn't super helpful in it's current state – if you include it I think it's better to explain why bubbling in this case is necessary for this feature to work.

document.addEventListener('keydown', this.keyDownHandler, true);

// Re-enable pointer events and hovering over EmojiPickerItems when the mouse moves
this.mouseMoveHandler = () => {
Expand All @@ -127,7 +124,7 @@ class EmojiPickerMenu extends Component {
*/
cleanupEventHandlers() {
if (document) {
document.removeEventListener('keydown', this.keyDownHandler);
document.removeEventListener('keydown', this.keyDownHandler, true);
document.removeEventListener('mousemove', this.mouseMoveHandler);
}
}
Expand All @@ -139,6 +136,20 @@ class EmojiPickerMenu extends Component {
highlightAdjacentEmoji(arrowKey) {
const firstNonHeaderIndex = this.state.filteredEmojis.length === this.emojis.length ? this.numColumns : 0;

// Arrow Down enable arrow navigation when search is focused
if (this.searchInput && this.searchInput.isFocused() && this.state.filteredEmojis.length) {
if (arrowKey !== 'ArrowDown') {
return;
}
this.searchInput.blur();

// We only want to hightlight the Emoji if none was highlighted already
// If we already have a highlighted Emoji, lets just skip the first navigation
if (this.state.highlightedIndex !== -1) {
return;
}
}

// If nothing is highlighted and an arrow key is pressed
// select the first emoji
if (this.state.highlightedIndex === -1) {
Expand All @@ -148,8 +159,9 @@ class EmojiPickerMenu extends Component {
}

let newIndex = this.state.highlightedIndex;
const move = (steps, boundsCheck) => {
const move = (steps, boundsCheck, onBoundReached = () => {}) => {
if (boundsCheck()) {
onBoundReached();
return;
}

Expand All @@ -174,7 +186,19 @@ class EmojiPickerMenu extends Component {
move(1, () => this.state.highlightedIndex + 1 > this.state.filteredEmojis.length - 1);
break;
case 'ArrowUp':
move(-this.numColumns, () => this.state.highlightedIndex - this.numColumns < firstNonHeaderIndex);
move(
-this.numColumns,
() => this.state.highlightedIndex - this.numColumns < firstNonHeaderIndex,
() => {
if (!this.searchInput) {
return;
}

// Reaching start of the list, arrow up set the focus to searchInput.
this.searchInput.focus();
newIndex = -1;
},
);
break;
default:
break;
Expand Down Expand Up @@ -252,27 +276,6 @@ class EmojiPickerMenu extends Component {
this.setState({filteredEmojis: newFilteredEmojiList, headerIndices: [], highlightedIndex: 0});
}

/**
* Toggles which arrow keys can affect the cursor in the search input,
* depending on whether the arrow keys will affect the index of the highlighted emoji.
*
* @param {KeyboardEvent} arrowKeyBoardEvent
*/
toggleArrowKeysOnSearchInput(arrowKeyBoardEvent) {
let keysToIgnore = ['ArrowDown', 'ArrowRight', 'ArrowLeft', 'ArrowUp'];
if (this.state.highlightedIndex === 0 && this.state.filteredEmojis.length) {
keysToIgnore = ['ArrowDown', 'ArrowRight'];
} else if (this.state.highlightedIndex === this.state.filteredEmojis.length - 1) {
keysToIgnore = ['ArrowLeft', 'ArrowUp'];
}

// Moving the cursor is the default behavior for arrow key presses while an input is focused,
// so prevent it
if (keysToIgnore.includes(arrowKeyBoardEvent.key)) {
arrowKeyBoardEvent.preventDefault();
}
}

/**
* Given an emoji item object, render a component based on its type.
* Items with the code "SPACER" return nothing and are used to fill rows up to 8
Expand Down