templates/common/scripts/select2-set-focus-resolver.html.twig line 1

Open in your IDE?
  1. <script>
  2.     function setFocusToSearchTextField() {
  3.         // focus event (ex. tab btn): open the menu
  4.         $(document).on('focus', '.select2-selection.select2-selection--single', function () {
  5.             // do not open menu, if field is disable for editing
  6.             if ($(this).attr('aria-disabled') === "true") {
  7.                 return
  8.             }
  9.             const aria_labelledby_container = this.getAttribute("aria-labelledby")
  10.             // returns more than one for datetime widget (hours and minutes, both container are on the same html lvl)
  11.             // so the only way how to detect the correct one is to use data from attribute
  12.             let menu_containers = $(this).closest(".select2-container").siblings('select:enabled')
  13.             if (menu_containers.length > 1) {
  14.                 menu_containers.each(function(){
  15.                     // find according container
  16.                     if (aria_labelledby_container.includes(this.id)) {
  17.                         $(this).select2('open')
  18.                     }
  19.                 })
  20.                 return
  21.             }
  22.             menu_containers.select2('open')
  23.         })
  24.         // menu open event: focus on search field if such one exists
  25.         $(document).on('select2:open', function (e) {
  26.             window.setTimeout(function() {
  27.                 $(".select2-container--open .select2-search__field").get(0).focus();
  28.             }, 200);
  29.         });
  30.     }
  31. </script>