Skip to content

Commit

Permalink
Fixed problem, that search field and search options did not close whe…
Browse files Browse the repository at this point in the history
…n clicking outside

This fixes issue #701. For the search field this was caused by algolia/autocomplete lib, which do not support multiple autocomplete fields on a single page. If initailly loaded on the homepage, which features a second autocomplete, this one "steals" the input listening, and the one in the navbar do not close anymore when clicking outside.
Custom code which triggers the closing of the autocomplete manually when clicking outside, was added as a workaround.
  • Loading branch information
jbtronics committed Oct 13, 2024
1 parent 7145bce commit abc5c61
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions assets/controllers/elements/part_search_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,15 @@ export default class extends Controller {
];
},
});

//Try to find the input field and register a defocus handler. This is necessarry, as by default the autocomplete
//lib has problems when multiple inputs are present on the page. (see https://github.com/algolia/autocomplete/issues/1216)
const inputs = this.element.getElementsByClassName('aa-Input');
for (const input of inputs) {
input.addEventListener('blur', () => {
this._autocomplete.setIsOpen(false);
});
}

}
}
2 changes: 1 addition & 1 deletion templates/components/search.macro.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% macro settings_drodown(show_label_instead_icon = true) %}

<div class="dropdown">
<button class="btn dropdown-toggle my-2" type="button" id="navbar-search-options" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" data-bs-auto-close="false">
<button class="btn dropdown-toggle my-2" type="button" id="navbar-search-options" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" data-bs-auto-close="true">
{% if show_label_instead_icon %}{% trans %}search.options.label{% endtrans %}{% else %}<i class="fa-solid fa-gear"></i>{% endif %}
<span class="caret"></span>
</button>
Expand Down

0 comments on commit abc5c61

Please sign in to comment.