Skip to content
This repository has been archived by the owner on Oct 2, 2019. It is now read-only.

on blur select #1544

Closed
Kepro opened this issue Mar 31, 2016 · 3 comments
Closed

on blur select #1544

Kepro opened this issue Mar 31, 2016 · 3 comments

Comments

@Kepro
Copy link

Kepro commented Mar 31, 2016

it is possible to select value on blur? now users need to

  1. Enter the name in the box
  2. Click on the the keyword in the drop-down list to confirm or press enter

But actually users tend to forget the second step above and after entering a desired name, they will click on other space to do following jobs. But if then, the name users just entered will be removed and jump back to the default value. It wound be a little annoying if such case happens.

@user378230
Copy link
Contributor

user378230 commented Mar 31, 2016

You could try something like this:

app.directive('selectOnBlur', function() {
  return {
    require: 'uiSelect',
    link: function($scope, $element, attrs, $select) {
      var searchInput = $element.querySelectorAll('input.ui-select-search');
      if(searchInput.length !== 1) throw Error("bla");

      searchInput.on('blur', function() {
        $scope.$apply(function() {
          var item = $select.items[$select.activeIndex];
          $select.select(item);
        });
      });
      // don't forget to .off(..) on $scope.$destroy 
    }
  }
});

http://plnkr.co/edit/Hydhel0iqB0gc5VgeMS8?p=preview (first input)

It's a 5 minute attempt but you could probably tidy up and check for edge cases etc. as necessary. If you do improve on it, be sure to update us here - someone else might find it useful.

@Kepro
Copy link
Author

Kepro commented Apr 13, 2016

thanks!

@rysilva
Copy link

rysilva commented Jun 22, 2017

FYI for anyone looking at this, the solution suggested by @user378230 has a bug: if you have multiple choices in the dropdown and you click one of them, the blur event will fire before the dropdown item click event and it'll tokenize the typed text and not the clicked item.

The problem is you can't tell from the blur event whether the mouse event causing the blur occurred inside or outside the ui-select component. The alternative is to use a click event instead of a blur event. You can set up a click handler on the document and select the active item if the click occurred outside of the root ui-select node. The problem now is that it's likely your event handler will occur after the ui-select's document click handler which is used to close the dropdown, and the dropdown will be closed by the time your selectOnBlur code runs. The fix (which I don't love but works) is to attach the click handler on document.body so it will get run before ui-select closes itself.

The following works for me:

app.directive('selectOnBlur', function($document) {
  return {
    require: 'uiSelect',
    link(scope, elm, attrs, ctrl) {
      let body = $document[0].body,
        handleClick = (evt) => {
          //Look for elm (the ui-select root element) somewhere in the ancestors
          let target = evt.target;
          if (elm === target) {
            return;
          }
          while (target.parentNode) {
            target = target.parentNode;
            if (elm === target) {
              return;
            }
          }

          //Did not exit, therefore user clicked outside of the ui-select
          //only select if open and something is typed
          //Check ctrl.items also, because for ui-select with fixed data, if all possible entries
          //  have already been selected then activeIndex will = 0 but items will be [].
          if (ctrl.open && ctrl.search &&
            (ctrl.items && ctrl.items.length) && (
            ctrl.tagging.isActivated || ctrl.activeIndex >= 0)) {
            ctrl.select(ctrl.items[ctrl.activeIndex]);
          }
        };

      body.addEventListener('click', handleClick);
      scope.$on('$destroy', () => {
        body.removeEventListener('click', handleClick);
      });
    }
  };
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants