Skip to content
Merged
Show file tree
Hide file tree
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
23 changes: 15 additions & 8 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,7 @@ var Select = React.createClass({
focusedOption = values[0];
valueForState = values[0].value;
} else {
for (var optionIndex = 0; optionIndex < filteredOptions.length; ++optionIndex) {
if (!filteredOptions[optionIndex].disabled) {
focusedOption = filteredOptions[optionIndex];
break;
}
}
focusedOption = this.getFirstFocusableOption(filteredOptions);
valueForState = values.map(function(v) { return v.value; }).join(this.props.delimiter);
}

Expand All @@ -256,6 +251,15 @@ var Select = React.createClass({
};
},

getFirstFocusableOption: function (options) {

for (var optionIndex = 0; optionIndex < options.length; ++optionIndex) {
if (!options[optionIndex].disabled) {
return options[optionIndex];
}
}
},

initValuesArray: function(values, options) {
if (!Array.isArray(values)) {
if (typeof values === 'string') {
Expand Down Expand Up @@ -472,7 +476,7 @@ var Select = React.createClass({
return filteredOptions[key];
}
}
return filteredOptions[0];
return this.getFirstFocusableOption(filteredOptions);
},

handleInputChange: function(event) {
Expand Down Expand Up @@ -589,7 +593,10 @@ var Select = React.createClass({
if (this.props.allowCreate && !this.state.focusedOption) {
return this.selectValue(this.state.inputValue);
}
return this.selectValue(this.state.focusedOption);

if (this.state.focusedOption) {
return this.selectValue(this.state.focusedOption);
}
},

focusOption: function(op) {
Expand Down
44 changes: 42 additions & 2 deletions test/Select-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
/* global describe, it, beforeEach */

var helper = require('../testHelpers/jsdomHelper');
helper();
var jsdomHelper = require('../testHelpers/jsdomHelper');

var sinon = require('sinon');
var unexpected = require('unexpected');
Expand All @@ -13,6 +12,8 @@ var expect = unexpected
.installPlugin(unexpectedDom)
.installPlugin(unexpectedSinon);

jsdomHelper();

var React = require('react/addons');
var TestUtils = React.addons.TestUtils;

Expand Down Expand Up @@ -895,6 +896,45 @@ describe('Select', function() {
'to have text', 'Select...');
});

it("doesn't select anything when a disabled option is the only item in the list after a search", function () {

typeSearchText('tw'); // Only 'two' in the list
pressEnterToAccept();
expect(onChange, 'was not called');
// And the menu is still open
expect(React.findDOMNode(instance), 'to contain no elements matching', DISPLAYED_SELECTION_SELECTOR)
expect(React.findDOMNode(instance), 'queried for' , '.Select-option',
'to satisfy', [
expect.it('to have text', 'Two')
]);
});

it("doesn't select anything when a disabled option value matches the entered text", function () {

typeSearchText('two'); // Matches value
pressEnterToAccept();
expect(onChange, 'was not called');
// And the menu is still open
expect(React.findDOMNode(instance), 'to contain no elements matching', DISPLAYED_SELECTION_SELECTOR)
expect(React.findDOMNode(instance), 'queried for' , '.Select-option',
'to satisfy', [
expect.it('to have text', 'Two')
]);
});

it("doesn't select anything when a disabled option label matches the entered text", function () {

typeSearchText('Two'); // Matches label
pressEnterToAccept();
expect(onChange, 'was not called');
// And the menu is still open
expect(React.findDOMNode(instance), 'to contain no elements matching', DISPLAYED_SELECTION_SELECTOR)
expect(React.findDOMNode(instance), 'queried for' , '.Select-option',
'to satisfy', [
expect.it('to have text', 'Two')
]);
});

it('shows disabled results in a search', function () {

typeSearchText('t');
Expand Down