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 issue with keyboard navigation in emoji picker #9954

Merged
Merged
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
25 changes: 20 additions & 5 deletions src/components/EmojiPicker/EmojiPickerMenu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ class EmojiPickerMenu extends Component {
this.isMobileLandscape = this.isMobileLandscape.bind(this);
this.onSelectionChange = this.onSelectionChange.bind(this);
this.updatePreferredSkinTone = this.updatePreferredSkinTone.bind(this);
this.setFirstNonHeaderIndex = this.setFirstNonHeaderIndex.bind(this);

this.currentScrollOffset = 0;
this.firstNonHeaderIndex = 0;

this.state = {
filteredEmojis: this.emojis,
Expand All @@ -111,6 +113,7 @@ class EmojiPickerMenu extends Component {
this.props.forwardedRef(this.searchInput);
}
this.setupEventHandlers();
this.setFirstNonHeaderIndex(this.emojis);
}

componentWillUnmount() {
Expand All @@ -126,6 +129,14 @@ class EmojiPickerMenu extends Component {
this.setState({selection: event.nativeEvent.selection});
}

/**
* Find and store index of the first emoji item
* @param {Array} filteredEmojis
*/
setFirstNonHeaderIndex(filteredEmojis) {
this.firstNonHeaderIndex = _.findIndex(filteredEmojis, item => !item.spacer && !item.header);
eVoloshchak marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Setup and attach keypress/mouse handlers for highlight navigation.
*/
Expand Down Expand Up @@ -214,10 +225,12 @@ class EmojiPickerMenu extends Component {
* @param {String} arrowKey
*/
highlightAdjacentEmoji(arrowKey) {
const firstNonHeaderIndex = this.state.filteredEmojis.length === this.emojis.length ? this.numColumns : 0;
if (this.state.filteredEmojis.length === 0) {
return;
}

// Arrow Down and Arrow Right enable arrow navigation when search is focused
if (this.searchInput && this.searchInput.isFocused() && this.state.filteredEmojis.length) {
if (this.searchInput && this.searchInput.isFocused()) {
if (arrowKey !== 'ArrowDown' && arrowKey !== 'ArrowRight') {
return;
}
Expand All @@ -242,7 +255,7 @@ class EmojiPickerMenu extends Component {
// If nothing is highlighted and an arrow key is pressed
// select the first emoji
if (this.state.highlightedIndex === -1) {
this.setState({highlightedIndex: firstNonHeaderIndex});
this.setState({highlightedIndex: this.firstNonHeaderIndex});
this.scrollToHighlightedIndex();
return;
}
Expand Down Expand Up @@ -273,7 +286,7 @@ class EmojiPickerMenu extends Component {
break;
case 'ArrowLeft':
move(-1,
() => this.state.highlightedIndex - 1 < firstNonHeaderIndex,
() => this.state.highlightedIndex - 1 < this.firstNonHeaderIndex,
() => {
// Reaching start of the list, arrow left set the focus to searchInput.
this.focusInputWithTextSelect();
Expand All @@ -286,7 +299,7 @@ class EmojiPickerMenu extends Component {
case 'ArrowUp':
move(
-this.numColumns,
() => this.state.highlightedIndex - this.numColumns < firstNonHeaderIndex,
() => this.state.highlightedIndex - this.numColumns < this.firstNonHeaderIndex,
() => {
// Reaching start of the list, arrow up set the focus to searchInput.
this.focusInputWithTextSelect();
Expand Down Expand Up @@ -356,6 +369,7 @@ class EmojiPickerMenu extends Component {
headerIndices: this.unfilteredHeaderIndices,
highlightedIndex: -1,
});
this.setFirstNonHeaderIndex(this.emojis);
return;
}

Expand All @@ -370,6 +384,7 @@ class EmojiPickerMenu extends Component {

// Remove sticky header indices. There are no headers while searching and we don't want to make emojis sticky
this.setState({filteredEmojis: newFilteredEmojiList, headerIndices: [], highlightedIndex: 0});
this.setFirstNonHeaderIndex(newFilteredEmojiList);
eVoloshchak marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down