indrimuska / angular-selector

A native AngularJS directive that transform a simple <select> box into a full html select with typeahead.
http://indrimuska.github.io/angular-selector
MIT License
96 stars 36 forks source link

Clicking 'Add [custom-tag]' in dropdown doesn't work #23

Closed kmturley closed 8 years ago

kmturley commented 8 years ago

Where we added the new custom tag stuff it doesn't allowed you to click the custom tag to add it:

'<li class="selector-option active" ng-if="filteredOptions.length == 0">' +
    'Add <i ng-bind="search"></i>' +
'</li>' +

Suggest you modify to be:

'<li class="selector-option active" ng-if="filteredOptions.length == 0" ng-mouseover="highlight(index)" ng-click="addCustom(search)">' +
    'Add <i ng-bind="search"></i>' +
'</li>' +

And add the function to scope:

scope.addCustom = function (val) {
    if (val) {
        var option = {};
        if (typeof attrs.create === 'function') {
            option = scope.create({ input: val });
        } else {
            option[scope.labelAttr] = val;
            option[scope.valueAttr || 'value'] = val;
        }
        scope.options.push(option);
        scope.set(option);
    }
};

Which can be reused here too:

case KEYS.enter:
case KEYS.comma:
    if (scope.isOpen) {
        if (scope.filteredOptions.length) {
            scope.set();
        } else if (attrs.create) {
            scope.addCustom(e.target.value);
        }
        e.preventDefault();
    }
    break;
indrimuska commented 8 years ago

Hey @kmturley, another nice contribution! Thank you for this, give me 5 to add this feature.

indrimuska commented 8 years ago

Now available in v1.1.2.

kmturley commented 8 years ago

nice!!