Skip to content

Commit

Permalink
Merge branch 'master' into 1.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoo committed Mar 25, 2016
2 parents 9bb4181 + efb9103 commit 321db82
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ This component receives the following props :
- `props.selectionIndex`
- The index of the highlighted option for rendering

#### props.showOptionsWhenEmpty

Type: `boolean`
Default: false

If true, options will still be rendered when there is no value.

### Typeahead ([Exposed Component Functions][reactecf])

Expand Down
17 changes: 12 additions & 5 deletions src/typeahead/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var fuzzy = require('fuzzy');
var classNames = require('classnames');

var IDENTITY_FN = function(input) { return input; };
var SHOULD_SEARCH_VALUE = function(input) { return input && input.trim().length > 0; };
var _generateAccessor = function(field) {
return function(object) { return object[field]; };
};
Expand Down Expand Up @@ -56,7 +55,8 @@ var Typeahead = React.createClass({
customListComponent: React.PropTypes.oneOfType([
React.PropTypes.element,
React.PropTypes.func
])
]),
showOptionsWhenEmpty: React.PropTypes.bool
},

getDefaultProps: function() {
Expand All @@ -79,7 +79,8 @@ var Typeahead = React.createClass({
onBlur: function(event) {},
filterOption: null,
defaultClassNames: true,
customListComponent: TypeaheadSelector
customListComponent: TypeaheadSelector,
showOptionsWhenEmpty: false
};
},

Expand All @@ -99,8 +100,14 @@ var Typeahead = React.createClass({
};
},

_shouldSkipSearch: function(input) {
var emptyValue = !input || input.trim().length == 0;
return !this.props.showOptionsWhenEmpty && emptyValue;
},

getOptionsForValue: function(value, options) {
if (!SHOULD_SEARCH_VALUE(value)) { return []; }
if (this._shouldSkipSearch(value)) { return []; }

var filterOptions = this._generateFilterFunction();
var result = filterOptions(value, options);
if (this.props.maxVisible) {
Expand Down Expand Up @@ -136,7 +143,7 @@ var Typeahead = React.createClass({

_renderIncrementalSearchResults: function() {
// Nothing has been entered into the textbox
if (!this.state.entryValue) {
if (this._shouldSkipSearch(this.state.entryValue)) {
return "";
}

Expand Down
27 changes: 26 additions & 1 deletion test/typeahead-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,31 @@ describe('Typeahead Component', function() {
var input = component.refs.entry;
assert.equal(input.tagName.toLowerCase(), 'input');
});
})
});

context('showOptionsWhenEmpty', function() {
it('do not render options when value is empty by default', function() {
var component = TestUtils.renderIntoDocument(
<Typeahead
options={ BEATLES }
/>
);

var results = TestUtils.scryRenderedComponentsWithType(component, TypeaheadOption);
assert.equal(0, results.length);
});

it('render options when value is empty when set to true', function() {
var component = TestUtils.renderIntoDocument(
<Typeahead
options={ BEATLES }
showOptionsWhenEmpty={ true }
/>
);

var results = TestUtils.scryRenderedComponentsWithType(component, TypeaheadOption);
assert.equal(4, results.length);
});
});
});
});

0 comments on commit 321db82

Please sign in to comment.