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

Change to set null instead of undefined when ctrl is clea #1918

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 4 additions & 4 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ uis.controller('uiSelectCtrl',

// When the user selects an item with ENTER or clicks the dropdown
ctrl.select = function(item, skipFocusser, $event) {
if (item === undefined || !_isItemDisabled(item)) {
if (item === undefined || item === null || !_isItemDisabled(item)) {

if ( ! ctrl.items && ! ctrl.search && ! ctrl.tagging.isActivated) return;

Expand All @@ -390,7 +390,7 @@ uis.controller('uiSelectCtrl',
// if taggingLabel is disabled and item is undefined we pull from ctrl.search
if ( ctrl.taggingLabel === false ) {
if ( ctrl.activeIndex < 0 ) {
if (item === undefined) {
if (item === undefined || item === null) {
item = ctrl.tagging.fct !== undefined ? ctrl.tagging.fct(ctrl.search) : ctrl.search;
}
if (!item || angular.equals( ctrl.items[0], item ) ) {
Expand All @@ -406,7 +406,7 @@ uis.controller('uiSelectCtrl',
if ( ctrl.activeIndex === 0 ) {
// ctrl.tagging pushes items to ctrl.items, so we only have empty val
// for `item` if it is a detected duplicate
if ( item === undefined ) return;
if ( item === undefined || item === null) return;

// create new item on the fly if we don't already have one;
// use tagging function if we have one
Expand Down Expand Up @@ -451,7 +451,7 @@ uis.controller('uiSelectCtrl',
};

ctrl.clear = function($event) {
ctrl.select(undefined);
ctrl.select(null);
$event.stopPropagation();
$timeout(function() {
ctrl.focusser[0].focus();
Expand Down
21 changes: 14 additions & 7 deletions src/uiSelectMultipleDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,16 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
result,
resultMultiple = [];
for (var j = $select.selected.length - 1; j >= 0; j--) {
locals = {};
locals[$select.parserResult.itemName] = $select.selected[j];
result = $select.parserResult.modelMapper(scope, locals);
// Keep original value for undefined and null
var selected = $select.selected[j];
if ( selected === undefined || selected === null) {
result = selected;
} else {
locals = {};
locals[$select.parserResult.itemName] = selected;
result = $select.parserResult.modelMapper(scope, locals);
}

resultMultiple.unshift(result);
}
return resultMultiple;
Expand Down Expand Up @@ -178,7 +185,7 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
return;
}
$select.selected.push(item);
var locals = {};
var locals = {};
locals[$select.parserResult.itemName] = item;

$timeout(function(){
Expand Down Expand Up @@ -261,11 +268,11 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
} else {
return curr;
}

} else {
// If nothing yet selected, select last item
return last;
}
return last;
}
break;
case KEY.DELETE:
// Remove selected item and select next item
Expand Down
14 changes: 11 additions & 3 deletions src/uiSelectSingleDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ uis.directive('uiSelectSingle', ['$timeout','$compile', function($timeout, $comp
ngModel.$parsers.unshift(function (inputValue) {
var locals = {},
result;
locals[$select.parserResult.itemName] = inputValue;
result = $select.parserResult.modelMapper(scope, locals);

// Keep original value for undefined and null
if (inputValue === undefined || inputValue === null) {
result = inputValue;
}
else {
locals[$select.parserResult.itemName] = inputValue;
result = $select.parserResult.modelMapper(scope, locals);
}

return result;
});

Expand Down Expand Up @@ -51,7 +59,7 @@ uis.directive('uiSelectSingle', ['$timeout','$compile', function($timeout, $comp

scope.$on('uis:select', function (event, item) {
$select.selected = item;
var locals = {};
var locals = {};
locals[$select.parserResult.itemName] = item;

$timeout(function(){
Expand Down
8 changes: 4 additions & 4 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ describe('ui-select tests', function() {

// Trigger clear.
el.find('.select2-search-choice-close').click();
expect(scope.selection.selected).toEqual(undefined);
expect(scope.selection.selected).toEqual(null);

// If there is no selection it the X icon should be gone.
expect(el.find('.select2-search-choice-close').length).toEqual(0);
Expand Down Expand Up @@ -3231,12 +3231,12 @@ describe('ui-select tests', function() {
expect(el.scope().$select.spinnerClass).toBe('randomclass');
});
});

describe('With refresh on active', function(){
it('should refresh when is activated', function(){
scope.fetchFromServer = function(){};
var el = createUiSelect({refresh:"fetchFromServer($select.search)",refreshDelay:0});
spyOn(scope, 'fetchFromServer');
spyOn(scope, 'fetchFromServer');
expect(el.scope().$select.open).toEqual(false);
el.scope().$select.activate();
$timeout.flush();
Expand All @@ -3248,7 +3248,7 @@ describe('ui-select tests', function() {
it('should refresh when open is set to true', function(){
scope.fetchFromServer = function(){};
var el = createUiSelect({refresh:"fetchFromServer($select.search)",refreshDelay:0});
spyOn(scope, 'fetchFromServer');
spyOn(scope, 'fetchFromServer');
expect(el.scope().$select.open).toEqual(false);
openDropdown(el);
$timeout.flush();
Expand Down