Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-8839: Search focus #775

Merged
merged 10 commits into from
Sep 25, 2023
5 changes: 5 additions & 0 deletions hdbt.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,8 @@ news-archive:
dependencies:
- core/drupalSettings
- core/drupal

search-helper:
version: 1.x
js:
src/js/search-helper.js: {}
7 changes: 7 additions & 0 deletions hdbt.theme
Original file line number Diff line number Diff line change
Expand Up @@ -1687,3 +1687,10 @@ function hdbt_theme_suggestions_container_alter(array &$suggestions, array &$var

array_unshift($suggestions, 'container__' . $type . '__' . $name);
}

/**
* Implements hook_preprocess_HOOK().
*/
function hdbt_preprocess_views_exposed_form(array &$variables) {
$variables['#attached']['library'][] = 'hdbt/search-helper';
}
15 changes: 15 additions & 0 deletions src/js/search-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function ($) {

// Set focus on search result count
$(document).ajaxComplete(function onDataLoaded(e, xhr, settings) {
// Check form ID to prevent mixing on multi-form page
const viewDomId = settings.extraData.view_dom_id;
const resultsContainerEl = $(`[data-id-number=${ viewDomId }]`);
const resultCountEl = $('[class$="__count-container"]', resultsContainerEl[0])[0];

if (!resultCountEl) return;
resultCountEl.setAttribute('tabindex', '-1');
resultCountEl.focus();
resultCountEl.scrollIntoView({behavior: 'smooth', block: 'center'});
});
})(jQuery);
17 changes: 10 additions & 7 deletions templates/views/views-view--service-list--block-search.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,22 @@
</header>
{% endif %}

{% if is_ajax_request or total_rows > pager_items_per_page %}
{{ exposed }}
{{ exposed }}
<div class="services-search__results" data-id-number="{{ dom_id }}">
<span class="service-list__count-container">
<span class="service-list__count">{{ total_rows ?? 0 }}</span>
{% trans with {'context': 'Service list count'}%}service{% plural total_rows %}services{% endtrans %}
</span>
{% endif %}
{% if total_rows %}
<span class="service-list__count">{{ total_rows }}</span>
{% trans with {'context': 'Service list count'}%}service{% plural total_rows %}services{% endtrans %}
{% endif %}

{{ empty }}
</span>
</div>
{{ attachment_before }}

{{ rows }}

{{ empty }}

{{ pager }}

{{ attachment_after }}
Expand Down
13 changes: 7 additions & 6 deletions templates/views/views-view--unit-search.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@
{{ exposed }}
{{ attachment_before }}

<div class="unit-search__results">
{% if total_rows %}
<div class="unit-search__results" data-id-number="{{ dom_id }}">
<span class="unit-search__count-container">
<span class="unit-search__count">{{ total_rows }}</span>
{% trans with {'context': 'Unit search count'}%}result{% plural total_rows %}results{% endtrans %}
{% if total_rows %}
<span class="unit-search__count">{{ total_rows }}</span>
{% trans with {'context': 'Unit search count'}%}result{% plural total_rows %}results{% endtrans %}
{% endif %}

{{ empty }}
</span>
{% endif %}

{{ rows }}

{{ empty }}
{{ pager }}
</div>
{{ attachment_after }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
*/
#}

<div class="services-search__results">
{% for row in rows %}
<div class="views-row">
{{- row.content -}}
</div>
{% endfor %}
</div>