From f4727c39220b2f8e69569b501c2dc1c145830e25 Mon Sep 17 00:00:00 2001 From: Pedro Jordao Date: Thu, 6 Jul 2023 14:52:18 -0300 Subject: [PATCH] MDL-73213 autocomplete: keep selected value when the state changes. The selected value of the dropdown was being cleared when the dropdown was opened, this was causing an error when submitting the form if the field was required. The main cause was that we were emptying the select before loading new values, this solution was created because of an issue where we could not deselect values if the list was reloaded. To fix this problem, I added an empty option as the first element of the select only when deselecting a certain item. --- lib/amd/build/form-autocomplete.min.js | 2 +- lib/amd/build/form-autocomplete.min.js.map | 2 +- lib/amd/src/form-autocomplete.js | 8 ++++---- lib/form/tests/behat/autocomplete.feature | 21 +++++++++++++++++++++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/amd/build/form-autocomplete.min.js b/lib/amd/build/form-autocomplete.min.js index da25882d176..895a40ee988 100644 --- a/lib/amd/build/form-autocomplete.min.js +++ b/lib/amd/build/form-autocomplete.min.js @@ -6,6 +6,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since 3.0 */ -define("core/form-autocomplete",["jquery","core/log","core/str","core/templates","core/notification","core/loadingicon","core/aria","core_form/changechecker"],(function($,log,str,templates,notification,LoadingIcon,Aria,FormChangeChecker){var KEYS_DOWN=40,KEYS_ENTER=13,KEYS_SPACE=32,KEYS_ESCAPE=27,KEYS_COMMA=44,KEYS_UP=38,KEYS_LEFT=37,KEYS_RIGHT=39,uniqueId=Date.now(),activateSelection=function(index,state){var selectionElement=$(document.getElementById(state.selectionId)),length=selectionElement.children("[aria-selected=true]").length;for(index%=length;index<0;)index+=length;var element=$(selectionElement.children("[aria-selected=true]").get(index)),itemId=state.selectionId+"-"+index;return selectionElement.children().attr("data-active-selection",null).attr("id",""),element.attr("data-active-selection",!0).attr("id",itemId),selectionElement.attr("aria-activedescendant",itemId),selectionElement.attr("data-active-value",element.attr("data-value")),$.Deferred().resolve()},updateActiveSelectionFromState=function(state){var activeElement=function(state){var _selectionRegion$attr,selectionRegion=$(document.getElementById(state.selectionId)),activeId=selectionRegion.attr("aria-activedescendant");if(activeId){var activeElement=$(document.getElementById(activeId));if(activeElement.length)return activeElement}var activeValue=null===(_selectionRegion$attr=selectionRegion.attr("data-active-value"))||void 0===_selectionRegion$attr?void 0:_selectionRegion$attr.replace(/"/g,'\\"');return selectionRegion.find('[data-value="'+activeValue+'"]')}(state),activeValue=activeElement.attr("data-value"),selectionRegion=$(document.getElementById(state.selectionId));if(activeValue){var activeIndex=selectionRegion.find("[aria-selected=true]").index(activeElement);if(-1!==activeIndex)return void activateSelection(activeIndex,state)}activateSelection(0,state)},updateSelectionList=function(options,state,originalSelect){var pendingKey="form-autocomplete-updateSelectionList-"+state.inputId;M.util.js_pending(pendingKey);var items=[],newSelection=$(document.getElementById(state.selectionId));if(originalSelect.children("option").each((function(index,ele){var label;$(ele).prop("selected")&&(""!==(label=$(ele).data("html")?$(ele).data("html"):$(ele).html())&&items.push({label:label,value:$(ele).attr("value")}))})),!hasItemListChanged(state,items))return M.util.js_complete(pendingKey),Promise.resolve();state.items=items;var context=$.extend(options,state);return templates.render(options.templates.items,context).then((function(html,js){templates.replaceNodeContents(newSelection,html,js),updateActiveSelectionFromState(state)})).then((function(){return M.util.js_complete(pendingKey)})).catch(notification.exception)},hasItemListChanged=function(state,items){return state.items.length!==items.length||state.items.filter((item=>-1===items.indexOf(item))).length>0},notifyChange=function(originalSelect){FormChangeChecker.markFormChangedFromNode(originalSelect[0]),originalSelect[0].dispatchEvent(new Event("change"))},deselectItem=function(options,state,item,originalSelect){var selectedItemValue=$(item).attr("data-value");return originalSelect.children("option").each((function(index,ele){$(ele).attr("value")==selectedItemValue&&($(ele).prop("selected",!1),$(ele).attr("data-iscustom")&&$(ele).remove())})),updateSelectionList(options,state,originalSelect).then((function(){notifyChange(originalSelect)}))},activateItem=function(index,state){var inputElement=$(document.getElementById(state.inputId)),suggestionsElement=$(document.getElementById(state.suggestionsId)),length=suggestionsElement.children(":not([aria-hidden])").length;for(index%=length;index<0;)index+=length;var element=$(suggestionsElement.children(":not([aria-hidden])").get(index)),globalIndex=$(suggestionsElement.children("[role=option]")).index(element),itemId=state.suggestionsId+"-"+globalIndex;suggestionsElement.children().attr("aria-selected",!1).attr("id",""),element.attr("aria-selected",!0).attr("id",itemId),inputElement.attr("aria-activedescendant",itemId);var scrollPos=element.offset().top-suggestionsElement.offset().top+suggestionsElement.scrollTop()-suggestionsElement.height()/2;return suggestionsElement.animate({scrollTop:scrollPos},100).promise()},closeSuggestions=function(state){var inputElement=$(document.getElementById(state.inputId)),suggestionsElement=$(document.getElementById(state.suggestionsId));return"true"===inputElement.attr("aria-expanded")&&inputElement.attr("aria-expanded",!1),inputElement.attr("aria-activedescendant",state.selectionId),Aria.hide(suggestionsElement.get()),suggestionsElement.hide(),$.Deferred().resolve()},updateSuggestions=function(options,state,query,originalSelect){var pendingKey="form-autocomplete-updateSuggestions-"+state.inputId;M.util.js_pending(pendingKey);var inputElement=$(document.getElementById(state.inputId)),suggestionsElement=$(document.getElementById(state.suggestionsId)),matchingElements=!1,suggestions=[];originalSelect.children("option").each((function(index,option){!0!==$(option).prop("selected")&&(suggestions[suggestions.length]={label:option.innerHTML,value:$(option).attr("value")})}));var searchquery=state.caseSensitive?query:query.toLocaleLowerCase(),context=$.extend({options:suggestions},options,state);return templates.render("core/form_autocomplete_suggestions",context).then((function(html,js){return templates.replaceNode(suggestionsElement,html,js),suggestionsElement=$(document.getElementById(state.suggestionsId)),Aria.unhide(suggestionsElement.get()),suggestionsElement.show(),suggestionsElement.children().each((function(index,node){node=$(node),options.caseSensitive&&node.text().indexOf(searchquery)>-1||!options.caseSensitive&&node.text().toLocaleLowerCase().indexOf(searchquery)>-1?(Aria.unhide(node.get()),node.show(),matchingElements=!0):(node.hide(),Aria.hide(node.get()))})),inputElement.attr("aria-expanded",!0),originalSelect.attr("data-notice")?suggestionsElement.html(originalSelect.attr("data-notice")):matchingElements?options.tags||activateItem(0,state):str.get_string("nosuggestions","form").done((function(nosuggestionsstr){suggestionsElement.html(nosuggestionsstr)})),suggestionsElement})).then((function(){return M.util.js_complete(pendingKey)})).catch(notification.exception)},createItem=function(options,state,originalSelect){var inputElement=$(document.getElementById(state.inputId)),tags=inputElement.val().split(","),found=!1;return $.each(tags,(function(tagindex,tag){if(""!==(tag=tag.trim())&&(options.multiple||originalSelect.children("option").prop("selected",!1),originalSelect.children("option").each((function(index,ele){$(ele).attr("value")==tag&&(found=!0,$(ele).prop("selected",!0))})),!found)){var option=$("