Skip to content

Commit

Permalink
Merge pull request #40 from whitespace-se/feat/searchasyoutype-filter
Browse files Browse the repository at this point in the history
feat: Add `AlgoliaIndex/SearchAsYouType` filter
  • Loading branch information
sebastianthulin authored May 17, 2024
2 parents b51bd44 + 9fb2b6b commit c819fad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
24 changes: 20 additions & 4 deletions source/js/algolia-index-js-searchpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,29 @@ document.addEventListener('DOMContentLoaded', function() {

/* Searchbox */
const renderSearchBox = (renderOptions, isFirstRender) => {
const { query, refine, clear, isSearchStalled, widgetParams } = renderOptions;
const { query, refine, clear, isSearchStalled, widgetParams: { searchAsYouType = true } = {} } = renderOptions;
if (isFirstRender) {
const searchInput = document.getElementById('input_searchboxfield');
searchInput.value = algoliaSearchData.searchQuery;
refine(algoliaSearchData.searchQuery);
searchInput.addEventListener('input', event => {
refine(event.target.value);
});
if (searchAsYouType) {
searchInput.addEventListener('input', event => {
refine(event.target.value);
});
} else {
// Detect Enter key press
searchInput.addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
refine(event.target.value);
}
});
// Detect clear action (when input value changes to empty)
searchInput.addEventListener('input', (event) => {
if (event.target.value === '') {
refine(event.target.value);
}
});
}
}
};
const customSearchBox = connectSearchBox(
Expand All @@ -65,6 +80,7 @@ document.addEventListener('DOMContentLoaded', function() {
//Do search
search(query);
},
searchAsYouType: Boolean(algoliaSearchData.searchAsYouType),

}),

Expand Down
1 change: 1 addition & 0 deletions source/php/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function enqueueScripts()
'applicationId' => \AlgoliaIndex\Helper\Options::applicationId(),
'indexName' => \AlgoliaIndex\Helper\Options::indexName(),
'searchQuery' => get_search_query(),
'searchAsYouType' => apply_filters('AlgoliaIndex/SearchAsYouType', true),
'clientConfig' => apply_filters('AlgoliaIndex/ClientConfig', []),
]);

Expand Down

0 comments on commit c819fad

Please sign in to comment.