Skip to content

Commit

Permalink
Merge pull request #572 from tswfi/search_icon_click_focus_on_search_…
Browse files Browse the repository at this point in the history
…field
  • Loading branch information
boherm authored Jan 4, 2024
2 parents 562f517 + 5f2af06 commit 3d20f3e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions modules/ps_searchbar/ps_searchbar.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<div id="search_widget" class="search-widgets js-search-widget" data-search-controller-url="{$search_controller_url}">
<form method="get" action="{$search_controller_url}">
<input type="hidden" name="controller" value="search">
<i class="material-icons search" aria-hidden="true">search</i>
<input class="js-search-input" type="text" name="s" value="{$search_string}" placeholder="{l s='Search our catalog' d='Shop.Theme.Catalog'}" aria-label="{l s='Search' d='Shop.Theme.Catalog'}">
<i class="material-icons search js-search-icon" aria-hidden="true">search</i>
<input class="js-search-input" type="search" name="s" value="{$search_string}" placeholder="{l s='Search our catalog' d='Shop.Theme.Catalog'}" aria-label="{l s='Search' d='Shop.Theme.Catalog'}">
<i class="material-icons clear" aria-hidden="true">clear</i>
</form>

Expand Down
1 change: 1 addition & 0 deletions src/js/constants/selectors-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const searchBar = {
searchResults: '.js-search-results',
searchTemplate: '.js-search-template',
searchInput: '.js-search-input',
searchIcon: '.js-search-icon',
};

export const checkout = {
Expand Down
15 changes: 15 additions & 0 deletions src/js/modules/ps_searchbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,23 @@ const initSearchbar = () => {
const searchResults = document.querySelector<HTMLElement>(SearchBarMap.searchResults);
const searchTemplate = document.querySelector<HTMLTemplateElement>(SearchBarMap.searchTemplate);
const searchInput = document.querySelector<HTMLInputElement>(SearchBarMap.searchInput);
const searchIcon = document.querySelector<HTMLElement>(SearchBarMap.searchIcon);
const searchUrl = searchWidget?.dataset.searchControllerUrl;

// focus on the input field when clicking on the widget area
// helps to have a larger "area" for touch devices
searchWidget?.addEventListener('click', () => {
searchInput?.focus();
});

// if input has text then submit search when clicking on the icon
// usability for people without "enter" key
searchIcon?.addEventListener('click', () => {
if (searchInput?.value) {
searchInput?.form?.submit();
}
});

searchCanvas?.addEventListener('hidden.bs.offcanvas', () => {
if (searchDropdown) {
searchDropdown.innerHTML = '';
Expand Down
1 change: 1 addition & 0 deletions types/selectors.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ declare type searchBar = {
searchResults: string,
searchTemplate: string,
searchInput: string,
searchIcon: string,
};

declare type checkout = {
Expand Down

0 comments on commit 3d20f3e

Please sign in to comment.