Skip to content

Commit

Permalink
Add remote typeahead spinner and no results message, refs: hitobito/h…
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWalkingLeek committed Apr 30, 2024
1 parent b584e2f commit 71cbc34
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/helpers/standard_form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ def person_field(attr, html_options = {}) # rubocop:disable Metrics/MethodLength
disabled: disabled,
data: { provide: 'entity',
id_field: "#{object_name}_#{attr_id}",
no_results_message: I18n.t('global.no_list_entries'),
url: html_options&.dig(:data, :url) || @template.query_people_path })
end

Expand Down
31 changes: 22 additions & 9 deletions app/javascript/javascripts/modules/remote_autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,30 @@ import { mark } from "@tarekraafat/autocomplete.js/src/helpers/io";
if (input.dataset.typeaheadDisabled === "true") return;

try {
if (isQuickSearch) {
document.getElementById(QUICKSEARCH_ID).classList.add("input-loading");
}
input.classList.add("input-loading");

// Fetch data via AJAX request
const url = new URL(input.dataset.url, location.origin)
const queryKey = document.getElementById(input.id).dataset.param || "q";
url.searchParams.set(queryKey, query)
const url = new URL(input.dataset.url, location.origin);
const queryKey =
document.getElementById(input.id).dataset.param || "q";
url.searchParams.set(queryKey, query);
const source = await fetch(url);
const data = await source.json();

if (isQuickSearch) {
document.getElementById(QUICKSEARCH_ID).classList.remove("input-loading");
input.classList.remove("input-loading");
var noResultsMessage =
autoCompleteInput.input.dataset.noResultsMessage;
if (!!noResultsMessage && !data.length) {
return [
{
id: null,
label: noResultsMessage,
noResultsItem: true,
},
];
} else {
return data;
}
return data;
} catch (error) {
return error;
}
Expand All @@ -76,6 +85,10 @@ import { mark } from "@tarekraafat/autocomplete.js/src/helpers/io";
input: {
selection: (event) => {
var selection = event.detail.selection.value;
if (selection.noResultsItem) {
return;
}

autoCompleteInput.input.value = selection.label;

if (isQuickSearch) {
Expand Down

0 comments on commit 71cbc34

Please sign in to comment.