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

fix(uiSelectController): invoke onRemove callback method for single s… #1977

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,16 @@ uis.controller('uiSelectCtrl',
};

ctrl.clear = function($event) {
var locals = {};
locals[ctrl.parserResult.itemName] = ctrl.selected;

ctrl.select(null);
$event.stopPropagation();
$timeout(function() {
ctrl.onRemoveCallback($scope, {
Copy link
Contributor

@Jefiozie Jefiozie Jun 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this function been refactored to be on the uiSingleSelect directive?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean then the clear function. As the uiSelectMultiple has his own remove function. I think it would be good to keep this kind of functionality on the correct place. What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jefiozie I see what you are saying. I might be overlooking something here but it seems to me that uiSelectMultiple has the removeChoice method on its controller and then the uiSelectMatchDirective view uses the uiSelectMultiple controller to call it like so ng-click="$selectMultiple.removeChoice($index)".

Currently uiSingleSelect doesn't have a controller so I would have to add the method directly to the scope of uiSingleSelect in the link function. Then I would call it from the uiSelectMatchDirective single view like so ng-click="remove($event)". I just haven't seen too many methods bound directly to the scope in the library so I wasn't sure if this was correct. What do you think?

$item: ctrl.items[ctrl.activeIndex],
$model: ctrl.parserResult.modelMapper($scope, locals)
});
ctrl.focusser[0].focus();
}, 0, false);
};
Expand Down
22 changes: 22 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ describe('ui-select tests', function () {
if (attrs.backspaceReset !== undefined) { attrsHtml += ' backspace-reset="' + attrs.backspaceReset + '"'; }
if (attrs.uiDisableChoice !== undefined) { choicesAttrsHtml += ' ui-disable-choice="' + attrs.uiDisableChoice + '"'; }
if (attrs.removeSelected !== undefined) { attrsHtml += ' remove-selected="' + attrs.removeSelected + '"'; }
if (attrs.onRemove !== undefined) { attrsHtml += ' on-remove="' + attrs.onRemove + '"'; }
}

return compileTemplate(
Expand Down Expand Up @@ -1369,6 +1370,27 @@ describe('ui-select tests', function () {

});

it('should invoke remove callback on remove for single select with allowClear enabled', function () {
scope.selection.selected = scope.people[5];

scope.onRemoveFn = function ($item, $model) {
scope.$item = $item;
scope.$model = $model;
};

var el = createUiSelect({ onRemove: 'onRemoveFn($item, $model)', allowClear: true });

expect(scope.$item).toBeFalsy();
expect(scope.$model).toBeFalsy();

el.find('.glyphicon.glyphicon-remove').click();
$timeout.flush();

expect(scope.$item).toEqual(scope.people[5]);
expect(scope.$model).toEqual(scope.$item);

});

it('should set $item & $model correctly when invoking callback on remove and no single prop. binding', function () {

scope.onRemoveFn = function ($item, $model, $label) {
Expand Down