Skip to content

Commit 65f07af

Browse files
committed
Merge pull request #164 from dseeto/master
Make skipping blank searches configurable
2 parents ed49656 + 318480f commit 65f07af

3 files changed

Lines changed: 44 additions & 6 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ This component receives the following props :
191191
- `props.selectionIndex`
192192
- The index of the highlighted option for rendering
193193

194+
#### props.showOptionsWhenEmpty
195+
196+
Type: `boolean`
197+
Default: false
198+
199+
If true, options will still be rendered when there is no value.
194200

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

src/typeahead/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var fuzzy = require('fuzzy');
99
var classNames = require('classnames');
1010

1111
var IDENTITY_FN = function(input) { return input; };
12-
var SHOULD_SEARCH_VALUE = function(input) { return input && input.trim().length > 0; };
1312
var _generateAccessor = function(field) {
1413
return function(object) { return object[field]; };
1514
};
@@ -54,7 +53,8 @@ var Typeahead = React.createClass({
5453
customListComponent: React.PropTypes.oneOfType([
5554
React.PropTypes.element,
5655
React.PropTypes.func
57-
])
56+
]),
57+
showOptionsWhenEmpty: React.PropTypes.bool
5858
},
5959

6060
getDefaultProps: function() {
@@ -75,7 +75,8 @@ var Typeahead = React.createClass({
7575
onBlur: function(event) {},
7676
filterOption: null,
7777
defaultClassNames: true,
78-
customListComponent: TypeaheadSelector
78+
customListComponent: TypeaheadSelector,
79+
showOptionsWhenEmpty: false
7980
};
8081
},
8182

@@ -95,8 +96,14 @@ var Typeahead = React.createClass({
9596
};
9697
},
9798

99+
_shouldSkipSearch: function(input) {
100+
var emptyValue = !input || input.trim().length == 0;
101+
return !this.props.showOptionsWhenEmpty && emptyValue;
102+
},
103+
98104
getOptionsForValue: function(value, options) {
99-
if (!SHOULD_SEARCH_VALUE(value)) { return []; }
105+
if (this._shouldSkipSearch(value)) { return []; }
106+
100107
var filterOptions = this._generateFilterFunction();
101108
var result = filterOptions(value, options);
102109
if (this.props.maxVisible) {
@@ -132,7 +139,7 @@ var Typeahead = React.createClass({
132139

133140
_renderIncrementalSearchResults: function() {
134141
// Nothing has been entered into the textbox
135-
if (!this.state.entryValue) {
142+
if (this._shouldSkipSearch(this.state.entryValue)) {
136143
return "";
137144
}
138145

test/typeahead-test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,31 @@ describe('Typeahead Component', function() {
563563
var input = component.refs.entry;
564564
assert.equal(input.tagName.toLowerCase(), 'input');
565565
});
566-
})
566+
});
567+
568+
context('showOptionsWhenEmpty', function() {
569+
it('do not render options when value is empty by default', function() {
570+
var component = TestUtils.renderIntoDocument(
571+
<Typeahead
572+
options={ BEATLES }
573+
/>
574+
);
575+
576+
var results = TestUtils.scryRenderedComponentsWithType(component, TypeaheadOption);
577+
assert.equal(0, results.length);
578+
});
579+
580+
it('render options when value is empty when set to true', function() {
581+
var component = TestUtils.renderIntoDocument(
582+
<Typeahead
583+
options={ BEATLES }
584+
showOptionsWhenEmpty={ true }
585+
/>
586+
);
587+
588+
var results = TestUtils.scryRenderedComponentsWithType(component, TypeaheadOption);
589+
assert.equal(4, results.length);
590+
});
591+
});
567592
});
568593
});

0 commit comments

Comments
 (0)