Skip to content

Commit

Permalink
Pass suggestionIndex to onSuggestionSelected (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
oyeanuj authored and moroshko committed Dec 31, 2016
1 parent 01ddb0d commit ef352ab
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,14 @@ where:
This function is called when suggestion is selected. It has the following signature:

```js
function onSuggestionSelected(event, { suggestion, suggestionValue, sectionIndex, method })
function onSuggestionSelected(event, { suggestion, suggestionValue, suggestionIndex, sectionIndex, method })
```

where:

* `suggestion` - the selected suggestion
* `suggestionValue` - the value of the selected suggestion (equivalent to `getSuggestionValue(suggestion)`)
* `suggestionIndex` - the index of the selected suggestion in the `suggestions` array
* `sectionIndex` - when rendering [multiple sections](#multiSectionProp), this will be the section index (in [`suggestions`](#suggestionsProp)) of the selected suggestion. Otherwise, it will be `null`.
* `method` - string describing how user selected the suggestion. The possible values are:
* `'click'` - user clicked (or tapped) on the suggestion
Expand Down
2 changes: 2 additions & 0 deletions src/Autosuggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class Autosuggest extends Component {
this.onSuggestionSelected(event, {
suggestion: clickedSuggestion,
suggestionValue: clickedSuggestionValue,
suggestionIndex: suggestionIndex,
sectionIndex,
method: 'click'
});
Expand Down Expand Up @@ -371,6 +372,7 @@ class Autosuggest extends Component {
this.onSuggestionSelected(event, {
suggestion: focusedSuggestion,
suggestionValue: newValue,
suggestionIndex: focusedSuggestionIndex,
sectionIndex: focusedSectionIndex,
method: 'enter'
});
Expand Down
1 change: 1 addition & 0 deletions test/focus-first-suggestion/AutosuggestApp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ describe('Autosuggest with focusFirstSuggestion={true}', () => {
expect(onSuggestionSelected).to.have.been.calledWithExactly(syntheticEventMatcher, {
suggestion: { name: 'Perl', year: 1987 },
suggestionValue: 'Perl',
suggestionIndex: 0,
sectionIndex: null,
method: 'enter'
});
Expand Down
2 changes: 2 additions & 0 deletions test/plain-list/AutosuggestApp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ describe('Default Autosuggest', () => {
expect(onSuggestionSelected).to.have.been.calledWithExactly(syntheticEventMatcher, {
suggestion: { name: 'Javascript', year: 1995 },
suggestionValue: 'Javascript',
suggestionIndex: 1,
sectionIndex: null,
method: 'click'
});
Expand All @@ -557,6 +558,7 @@ describe('Default Autosuggest', () => {
expect(onSuggestionSelected).to.have.been.calledWithExactly(syntheticEventMatcher, {
suggestion: { name: 'Java', year: 1995 },
suggestionValue: 'Java',
suggestionIndex: 0,
sectionIndex: null,
method: 'enter'
});
Expand Down

0 comments on commit ef352ab

Please sign in to comment.