Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit 0baec39

Browse files
author
Jake Teton-Landis
committed
fix mouse focus issue with optionIndexes
1 parent 466b985 commit 0baec39

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

lib/combobox.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ module.exports = React.createClass({
115115
props.onClick = this.selectOption.bind(this, child);
116116
props.onFocus = this.handleOptionFocus;
117117
props.onKeyDown = this.handleOptionKeyDown.bind(this, child);
118-
props.onMouseEnter = this.handleOptionMouseEnter.bind(this, index);
118+
props.onMouseEnter = this.handleOptionMouseEnter.bind(
119+
this, optionIndexes.length - 1);
119120
}.bind(this));
120121
return {
121122
optionIndexes: optionIndexes,
@@ -311,20 +312,23 @@ module.exports = React.createClass({
311312

312313
focusPrevious: function() {
313314
if (this.state.menu.isEmpty) return;
314-
var last = this.props.children.length - 1;
315+
var last = this.state.menu.optionIndexes.length - 1;
315316
var index = this.state.focusedIndex == null ?
316317
last : this.state.focusedIndex - 1;
317318
this.focusOptionAtIndex(index);
318319
},
319320

320321
focusSelectedOption: function() {
322+
// we have to do two hops to go from the DOM index to option index
321323
var selectedDomIndex;
324+
325+
// hop 1: dom index
322326
React.Children.forEach(this.props.children, function(child, index) {
323327
if (child.props.value === this.state.value)
324328
selectedDomIndex = index;
325329
}.bind(this));
326330

327-
// find the option index
331+
// hop 2: option index
328332
var selectedOptionIndex;
329333
this.state.menu.optionIndexes.forEach(function(domIndex, optIndex) {
330334
if (domIndex === selectedDomIndex) selectedOptionIndex = optIndex;
@@ -347,6 +351,8 @@ module.exports = React.createClass({
347351
return inputValue || value;
348352
},
349353

354+
// index is the index of the option among the list of only options,
355+
// NOT among all elements in the DOM
350356
focusOptionAtIndex: function(index) {
351357
if (!this.state.isOpen && this.state.value)
352358
return this.focusSelectedOption();

0 commit comments

Comments
 (0)