Nick Kahn
Nick Kahn

Reputation: 20078

Open on focus tabs over to ui-select

I have a simple thing I want to do - when someone tabs over to my ui-select, I want it to drop down automatically. Unfortunately, the ng-focus doesn't seem to fire when the focus changes to the ui-select. Is there a workaround?

Upvotes: 3

Views: 4793

Answers (2)

Steve Ellis
Steve Ellis

Reputation: 494

The showOnFocus directive was not working for me. I added an additional input(hidden, but not invisible) become focused, and turned off the tabindex of the ui-select.

<custom-directive>
  <input class="custom-focuser" tabindex="0" style="left: 10000px"/>
  <ui-select tabindex="-1"/> 
</custom-directive>

Then in the custom directive's link function, I attached to the focus event of the additional input.

app.directive('customDirective', function () {
  return {
    // ...
    link: function ($scope, el, attrs, ctrl) {
      el.find('.custom-focuser').focus(function (event) {
        //your custom logic here
      });
    }
  };
});

Upvotes: 0

Karthik
Karthik

Reputation: 1377

Try this if you are looking for a quick workaround:

app.directive('showOnFocus', function() {
   return {
       restrict: 'A',
       link: function (scope, element) {
           var focused = false, opened = false;
           var select = element.children('.selectize-input');
           var toggleInput = element.find('input')[0];

           var onfocus = function(){
               if(!focused && !opened) {
                  toggleInput.click();
                  opened = focused = true;
               } else if(opened) {
                  opened = false;
               }
           };
           var onhover = function(){
              if(!focused && !opened){
                  toggleInput.click();
                  opened = focused = true;
               };
           };

           var onblur = function(){
              focused = false;
           };
           element.bind('mouseenter', onhover);
           element.bind('click',onblur);
           select.bind('blur', onblur);
           select.bind('focus', onfocus);

           //show on pageload
           onhover(); 
       }
    };
});

And apply the directive in your ui-select element

<ui-select show-on-focus>..</ui-select>

Hope this helps.

Upvotes: 1

Related Questions