Skip to content

Commit

Permalink
Merge branch 'better_filtering' of github.com:HubSpot/react-typeahead…
Browse files Browse the repository at this point in the history
… into hubspot
  • Loading branch information
wolfd committed May 17, 2016
2 parents b5ce4fd + 2a80cf3 commit 76c04d8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ A function to filter the provided `options` based on the current input value. Fo

If provided as a string, it will interpret it as a field name and use that field from each option object.

#### props.filterOptions
#### props.searchOptions

Type: `Function`

Expand All @@ -358,7 +358,7 @@ Make sure to use the `displayOption`, `inputDisplayOption`, and `formInputOption

Type: `String` or `Function`

A function that maps the internal state of the visible options into the value stored in the text value field of the input when an option is selected.
A function that maps the internal state of the visible options into the value stored in the text value field of the visible input when an option is selected.

Receives `(option)`.

Expand All @@ -370,7 +370,7 @@ If no value is set, the input will be set using `displayOption` when an option i

Type: `String` or `Function`

A function to map an option onto a string to include in HTML forms (see `props.name`). Receives `(option)` as arguments. Must return a string.
A function to map an option onto a string to include in HTML forms as a hidden field (see `props.name`). Receives `(option)` as arguments. Must return a string.

If specified as a string, it will interpret it as a field name and use that field from each option object.

Expand Down
18 changes: 9 additions & 9 deletions src/typeahead/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var Typeahead = React.createClass({
React.PropTypes.string,
React.PropTypes.func
]),
filterOptions: React.PropTypes.func,
searchOptions: React.PropTypes.func,
displayOption: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.func
Expand Down Expand Up @@ -75,7 +75,7 @@ var Typeahead = React.createClass({
onFocus: function(event) {},
onBlur: function(event) {},
filterOption: null,
filterOptions: null,
searchOptions: null,
inputDisplayOption: null,
defaultClassNames: true,
customListComponent: TypeaheadSelector,
Expand Down Expand Up @@ -107,8 +107,8 @@ var Typeahead = React.createClass({
getOptionsForValue: function(value, options) {
if (this._shouldSkipSearch(value)) { return []; }

var filterOptions = this._generateFilterFunction();
var result = filterOptions(value, options);
var searchOptions = this._generateSearchFunction();
var result = searchOptions(value, options);
if (this.props.maxVisible) {
result = result.slice(0, this.props.maxVisible);
}
Expand Down Expand Up @@ -357,14 +357,14 @@ var Typeahead = React.createClass({
);
},

_generateFilterFunction: function() {
var filterOptionsProp = this.props.filterOptions;
_generateSearchFunction: function() {
var searchOptionsProp = this.props.searchOptions;
var filterOptionProp = this.props.filterOption;
if (typeof filterOptionsProp === 'function') {
if (typeof searchOptionsProp === 'function') {
if (filterOptionProp !== null) {
console.warn('filterOptions prop is being used, filterOption prop will be ignored');
console.warn('searchOptions prop is being used, filterOption prop will be ignored');
}
return filterOptionsProp;
return searchOptionsProp;
} else if (typeof filterOptionProp === 'function') {
return function(value, options) {
return options.filter(function(o) { return filterOptionProp(value, o); });
Expand Down
8 changes: 4 additions & 4 deletions test/typeahead-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ describe('Typeahead Component', function() {
});
});

context('filterOptions', function() {
context('searchOptions', function() {
it('maps correctly when specified with map function', function() {
var createObject = function(o) {
return { len: o.length, orig: o };
};

var component = TestUtils.renderIntoDocument(<Typeahead
options={ BEATLES }
filterOptions={ function(inp, opts) { return opts.map(createObject); } }
searchOptions={ function(inp, opts) { return opts.map(createObject); } }
displayOption={ function(o, i) { return 'Score: ' + o.len + ' ' + o.orig; } }
inputDisplayOption={ function(o, i) { return o.orig; } }
/>);
Expand All @@ -220,7 +220,7 @@ describe('Typeahead Component', function() {

var component = TestUtils.renderIntoDocument(<Typeahead
options={ BEATLES }
filterOptions={ function(inp, opts) { return opts.map(function(o) { return o; }).sort().map(createObject); } }
searchOptions={ function(inp, opts) { return opts.map(function(o) { return o; }).sort().map(createObject); } }
displayOption={ function(o, i) { return 'Score: ' + o.len + ' ' + o.orig; } }
inputDisplayOption={ function(o, i) { return o.orig; } }
/>);
Expand All @@ -238,7 +238,7 @@ describe('Typeahead Component', function() {

var component = TestUtils.renderIntoDocument(<Typeahead
options={ BEATLES }
filterOptions={ function(inp, opts) { return opts.map(function(o) { return o; }).sort().map(createObject); } }
searchOptions={ function(inp, opts) { return opts.map(function(o) { return o; }).sort().map(createObject); } }
displayOption={ function(o, i) { return 'Score: ' + o.len + ' ' + o.orig; } }
inputDisplayOption={ function(o, i) { return o.orig; } }
/>);
Expand Down

0 comments on commit 76c04d8

Please sign in to comment.