From ef352ab66f5337f26ed8f64f820bf5af5382b265 Mon Sep 17 00:00:00 2001 From: Anuj Date: Fri, 30 Dec 2016 22:56:56 -0800 Subject: [PATCH] Pass suggestionIndex to onSuggestionSelected (#311) --- README.md | 3 ++- src/Autosuggest.js | 2 ++ test/focus-first-suggestion/AutosuggestApp.test.js | 1 + test/plain-list/AutosuggestApp.test.js | 2 ++ 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c0d4023f..46937ab5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Autosuggest.js b/src/Autosuggest.js index d5bf75e2..9f3a9a17 100644 --- a/src/Autosuggest.js +++ b/src/Autosuggest.js @@ -229,6 +229,7 @@ class Autosuggest extends Component { this.onSuggestionSelected(event, { suggestion: clickedSuggestion, suggestionValue: clickedSuggestionValue, + suggestionIndex: suggestionIndex, sectionIndex, method: 'click' }); @@ -371,6 +372,7 @@ class Autosuggest extends Component { this.onSuggestionSelected(event, { suggestion: focusedSuggestion, suggestionValue: newValue, + suggestionIndex: focusedSuggestionIndex, sectionIndex: focusedSectionIndex, method: 'enter' }); diff --git a/test/focus-first-suggestion/AutosuggestApp.test.js b/test/focus-first-suggestion/AutosuggestApp.test.js index 6579fb7c..2804323d 100644 --- a/test/focus-first-suggestion/AutosuggestApp.test.js +++ b/test/focus-first-suggestion/AutosuggestApp.test.js @@ -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' }); diff --git a/test/plain-list/AutosuggestApp.test.js b/test/plain-list/AutosuggestApp.test.js index da585cde..f3f16fcb 100644 --- a/test/plain-list/AutosuggestApp.test.js +++ b/test/plain-list/AutosuggestApp.test.js @@ -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' }); @@ -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' });