autocomplete.ftl

 1   <#--
 2   Macro: autocomplete
 3   Description: Generates an autocomplete dropdown
 4   Parameters:
 5   - id (string): the ID of the autocomplete search input element.
 6   - name (string): the name attribute of the hidden input element.
 7   - suggestionsUrl (string): the URL to fetch the suggestions from.
 8   - suggestionsPath (string, optional): the path to the suggestions list in the response object.
 9   - itemValueFieldName (string, optional): the value property of the suggestion object.
 10   - btnColor (string, optional): the color of the dropdown button.
 11   - btnSize (string, optional): the size of the dropdown button.
 12   - itemLabelFieldNames (array, optional): an array of suggestion object property names to display as the title.
 13   - itemTitleFieldNames (array, optional): an array of suggestion object property names to display as the description.
 14   - itemDescriptionFieldNames (array, optional): an array of suggestion object property names to display as the description.
 15   - itemTagsFieldNames (array, optional): an array of suggestion object property names to display as tags.
 16   - currentValue (string, optional): the current value of the autocomplete input.
 17   - currentLabel (string, optional): the current label displayed on the dropdown button.
 18   - required (boolean, optional): whether the input is required or not.
 19   - minimumInputLength (integer, optional): the minimum number of characters needed to trigger the search.
 20   - minimumInputLenghtLabel (string, optional): the label for the minimum input length information.
 21   - searchLabel (string, optional): the label displayed on the dropdown button.
 22   - emptyLabel (string, optional): the label displayed when there are no suggestions.
 23   - additionnalRequestParamInputId (string, optional): Id of a second input for the search.
 24   -->
 25   <#macro autocomplete id name suggestionsUrl suggestionsPath="" itemValueFieldName="value" btnColor="light" btnSize="" itemLabelFieldNames="[]" itemTitleFieldNames=itemLabelFieldNames itemDescriptionFieldNames="[]" itemTagsFieldNames="[]" copyFields="[]" currentValue="" currentLabel="" required=false minimumInputLength=1 minimumInputLenghtLabel="#i18n{portal.util.labelMinimumSearchLenght}" searchLabel="#i18n{portal.util.labelSearch}" emptyLabel="#i18n{portal.util.labelNoItem}" additionnalRequestParamInputId="" >
 26   <div id="${id}" class="lutece-autocomplete dropdown form-group form-floating " data-itemTitleFieldNames=${itemTitleFieldNames} data-suggestionsUrl="${suggestionsUrl}" data-suggestionsPath="${suggestionsPath}" data-itemValueFieldName=${itemValueFieldName} data-minimumInputLength=${minimumInputLength} data-itemDescriptionFieldNames=${itemDescriptionFieldNames} data-itemTagsFieldNames=${itemTagsFieldNames} data-copyFields=${copyFields} data-emptyLabel="${emptyLabel}" data-currentValue="${currentValue}" data-currentLabel="${currentLabel}" data-suggestionItemClass='["list-group-item", "p-3"]' data-titleClass='["mb-0", "fw-bolder"]' data-descriptionClass='["text-muted", "mb-0"]' data-tagClass='["badge", "bg-blue-lt", "me-1"]' data-loaderIconClasses='{"loading": ["ti-loader-2", "icon-rotate"], "error": ["ti-zoom-exclamation", "text-danger"], "search": ["ti-search"]}' data-emptyClass='["list-group-item", "p-3", "text-muted", "text-center"]' data-searchLabel="${searchLabel}">
 27     <@formGroup formStyle='floating' class='' labelFor='${id}' labelKey='${searchLabel}' mandatory=mandatory>
 28       <@inputGroup>
 29         <@input type='text' name="${name}" class="lutece-autocomplete-search-input w-75" value="${currentValue!}"  id="${id}-input" params='role="combobox" autocomplete="off" aria-expanded="false" aria-owns="${id}-list-container" aria-autocomplete="list" aria-describedby="${id}-lutece-autocomplete-assistiveHint"' />
 30         <span role="button" class="input-group-text lutece-autocomplete-remove <#if currentValue=''>d-none</#if> text-danger">
 31   				<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M18 6l-12 12" /><path d="M6 6l12 12" /></svg>
 32   			</span>
 33   			<span role="button" class="input-group-text lutece-autocomplete-search-icon">
 34   				<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M10 10m-7 0a7 7 0 1 0 14 0a7 7 0 1 0 -14 0" /><path d="M21 21l-6 -6" /></svg>
 35   			</span>	
 36       </@inputGroup>
 37       <div id="${id}-dropdown" class="lutece-autocomplete-dropdown dropdown-menu p-0" aria-labelledby="dropdownMenuButton">
 38   			<ul id="${id}-list-container" class="lutece-autocomplete-result-container list-group list-group-flush overflow-auto bg-white" role="listbox" id="suggestions-list" aria-labelledby="${id}-input" style="max-height:15rem;"></ul>
 39   			<span id="${id}-lutece-autocomplete-default-assistive" class="sr-only visually-hidden">#i18n{portal.util.message.autocomplete.avalailbleResults}</span>
 40   			<div class="sr-only lutece-autocomplete-status" role="status" aria-atomic="true" aria-live="polite"></div>
 41   		</div>
 42     </@formGroup>
 43   </div>
 44   <script type="module">
 45   import LuteceAutoComplete from "./themes/shared/modules/luteceAutoComplete.js";
 46   new LuteceAutoComplete(document.getElementById(`${id}`), document.getElementById(`${additionnalRequestParamInputId}`));
 47   </script>
 48   </#macro>