From b9f7913417f749357f8cf974e73a5b24b056a601 Mon Sep 17 00:00:00 2001 From: maxiloc Date: Thu, 7 Jul 2016 14:14:49 +0200 Subject: [PATCH] allow to override handleSelected. Fixes #121 (#123) --- README.md | 13 +++++++++++++ src/lib/DocSearch.js | 10 ++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index daeb4ad5..8be856a7 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,19 @@ search.autocomplete.on('autocomplete:opened', function(e) { }); ``` +We already bind the autocomplete:selected event inside the docsearch. +If you want to replace the default behavior you can pass the handleSelected option + +```javascript +var search = docsearch({ + apiKey: '', + indexName: '', + inputSelector: '', + handleSelected: function (input, event, suggestion) { + } +}); +``` + ### Algolia options You can also pass any specific option to the Algolia API to change the way diff --git a/src/lib/DocSearch.js b/src/lib/DocSearch.js index 71a59766..ce492109 100644 --- a/src/lib/DocSearch.js +++ b/src/lib/DocSearch.js @@ -42,10 +42,11 @@ class DocSearch { autoselect: true }, transformData = false, + handleSelected = false, enhancedSearchInput = false, layout = 'collumns' }) { - DocSearch.checkArguments({apiKey, indexName, inputSelector, debug, algoliaOptions, autocompleteOptions, transformData, enhancedSearchInput, layout}); + DocSearch.checkArguments({apiKey, indexName, inputSelector, debug, algoliaOptions, autocompleteOptions, transformData, handleSelected, enhancedSearchInput, layout}); this.apiKey = apiKey; this.appId = appId; @@ -59,6 +60,8 @@ class DocSearch { prefix: 'ds' }; + handleSelected = handleSelected || this.handleSelected; + this.isSimpleLayout = (layout === 'simple'); this.client = algoliasearch(this.appId, this.apiKey); @@ -73,13 +76,12 @@ class DocSearch { source: this.getAutocompleteSource(transformData), templates: { suggestion: DocSearch.getSuggestionTemplate(this.isSimpleLayout), - footer: templates.footer, - empty: DocSearch.getEmptyTemplate() + footer: templates.footer } }]); this.autocomplete.on( 'autocomplete:selected', - this.handleSelected.bind(null, this.autocomplete.autocomplete) + handleSelected.bind(null, this.autocomplete.autocomplete) ) this.autocomplete.on( 'autocomplete:shown',