Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Prop: selectFirst #186

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ Default: false

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

#### props.selectFirst

Type: `boolean`
Default: false

If true, first option will be selected by default.

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

#### typeahead.focus
Expand Down
67 changes: 37 additions & 30 deletions dist/react-typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fuzzy.match = function(pattern, string, opts) {
pattern = opts.caseSensitive && pattern || pattern.toLowerCase();

// For each character in the string, either add it to the result
// or wrap in template if its the next string in the pattern
// or wrap in template if it's the next string in the pattern
for(var idx = 0; idx < len; idx++) {
ch = string[idx];
if(compareString[idx] === pattern[patternIdx]) {
Expand Down Expand Up @@ -141,40 +141,40 @@ fuzzy.match = function(pattern, string, opts) {
// // string to put after matching character
// , post: '</b>'
//
// // Optional function. Input is an element from the passed in
// // `arr`, output should be the string to test `pattern` against.
// // Optional function. Input is an entry in the given arr`,
// // output should be the string to test `pattern` against.
// // In this example, if `arr = [{crying: 'koala'}]` we would return
// // 'koala'.
// , extract: function(arg) { return arg.crying; }
// }
fuzzy.filter = function(pattern, arr, opts) {
opts = opts || {};
return arr
.reduce(function(prev, element, idx, arr) {
var str = element;
if(opts.extract) {
str = opts.extract(element);
}
var rendered = fuzzy.match(pattern, str, opts);
if(rendered != null) {
prev[prev.length] = {
string: rendered.rendered
, score: rendered.score
, index: idx
, original: element
};
}
return prev;
}, [])

// Sort by score. Browsers are inconsistent wrt stable/unstable
// sorting, so force stable by using the index in the case of tie.
// See http://ofb.net/~sethml/is-sort-stable.html
.sort(function(a,b) {
var compare = b.score - a.score;
if(compare) return compare;
return a.index - b.index;
});
.reduce(function(prev, element, idx, arr) {
var str = element;
if(opts.extract) {
str = opts.extract(element);
}
var rendered = fuzzy.match(pattern, str, opts);
if(rendered != null) {
prev[prev.length] = {
string: rendered.rendered
, score: rendered.score
, index: idx
, original: element
};
}
return prev;
}, [])

// Sort by score. Browsers are inconsistent wrt stable/unstable
// sorting, so force stable by using the index in the case of tie.
// See http://ofb.net/~sethml/is-sort-stable.html
.sort(function(a,b) {
var compare = b.score - a.score;
if(compare) return compare;
return a.index - b.index;
});
};


Expand Down Expand Up @@ -554,6 +554,7 @@ var Typeahead = React.createClass({
formInputOption: React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.func]),
defaultClassNames: React.PropTypes.bool,
customListComponent: React.PropTypes.oneOfType([React.PropTypes.element, React.PropTypes.func]),
selectFirst: React.PropTypes.bool,
showOptionsWhenEmpty: React.PropTypes.bool
},

Expand All @@ -578,6 +579,7 @@ var Typeahead = React.createClass({
filterOption: null,
defaultClassNames: true,
customListComponent: TypeaheadSelector,
selectFirst: false,
showOptionsWhenEmpty: false
};
},
Expand Down Expand Up @@ -791,9 +793,14 @@ var Typeahead = React.createClass({
},

componentWillReceiveProps: function (nextProps) {
this.setState({
var typeaheadOptionsState = {
visible: this.getOptionsForValue(this.state.entryValue, nextProps.options)
});
};
if (this.props.selectFirst && nextProps.options.length) {
typeaheadOptionsState.selectionIndex = 0;
}

this.setState(typeaheadOptionsState);
},

render: function () {
Expand Down
11 changes: 9 additions & 2 deletions src/typeahead/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var Typeahead = React.createClass({
React.PropTypes.element,
React.PropTypes.func
]),
selectFirst: React.PropTypes.bool,
showOptionsWhenEmpty: React.PropTypes.bool
},

Expand All @@ -72,6 +73,7 @@ var Typeahead = React.createClass({
filterOption: null,
defaultClassNames: true,
customListComponent: TypeaheadSelector,
selectFirst: false,
showOptionsWhenEmpty: false
};
},
Expand Down Expand Up @@ -288,9 +290,14 @@ var Typeahead = React.createClass({
},

componentWillReceiveProps: function(nextProps) {
this.setState({
var typeaheadOptionsState = {
visible: this.getOptionsForValue(this.state.entryValue, nextProps.options)
});
}
if (this.props.selectFirst && nextProps.options.length) {
typeaheadOptionsState.selectionIndex = 0;
}

this.setState(typeaheadOptionsState);
},

render: function() {
Expand Down
22 changes: 22 additions & 0 deletions test/typeahead-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,28 @@ describe('Typeahead Component', function() {
});
});

context('selectFirst', function() {
context('options are present', function() {
it('sets the selectionIndex to 0 (first option) by default', function() {
var component = TestUtils.renderIntoDocument(<Typeahead
options={[]} selectFirst={true}
/>);
component.componentWillReceiveProps({options: BEATLES})
assert.equal(0, component.state.selectionIndex);
});
});
context('options is empty', function() {
it('does not set selectionIndex', function() {
var component = TestUtils.renderIntoDocument(<Typeahead
options={[]}
/>);
component.componentWillReceiveProps({options: []})
assert.equal(null, component.state.selectionIndex);
});
});

});

context('showOptionsWhenEmpty', function() {
it('do not render options when value is empty by default', function() {
var component = TestUtils.renderIntoDocument(
Expand Down