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

feat(uiSelectSingleDirective): add an option to avoid backspace model reset #1736

Merged
merged 8 commits into from
Nov 2, 2016
Merged
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
3 changes: 2 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ var uis = angular.module('ui.select', [])
},
appendToBody: false,
spinnerEnabled: false,
spinnerClass: 'glyphicon-refresh ui-select-spin'
spinnerClass: 'glyphicon-refresh ui-select-spin',
backspaceReset: true
})

// See Rename minErr and make it accessible from outside https://github.com/angular/angular.js/issues/6913
Expand Down
6 changes: 6 additions & 0 deletions src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ uis.directive('uiSelect',
$select.sortable = sortable !== undefined ? sortable : uiSelectConfig.sortable;
});

attrs.$observe('backspaceReset', function() {
// $eval() is needed otherwise we get a string instead of a boolean
var backspaceReset = scope.$eval(attrs.backspaceReset);
$select.backspaceReset = backspaceReset !== undefined ? backspaceReset : true;
});

attrs.$observe('limit', function() {
//Limit the number of selections allowed
$select.limit = (angular.isDefined(attrs.limit)) ? parseInt(attrs.limit, 10) : undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/uiSelectSingleDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ uis.directive('uiSelectSingle', ['$timeout','$compile', function($timeout, $comp
});
focusser.bind("keydown", function(e){

if (e.which === KEY.BACKSPACE) {
if (e.which === KEY.BACKSPACE && $select.backspaceReset !== false) {
e.preventDefault();
e.stopPropagation();
$select.select(undefined);
Expand Down
21 changes: 21 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ describe('ui-select tests', function() {
if (attrs.spinnerClass !== undefined) { attrsHtml += ' spinner-class="' + attrs.spinnerClass + '"'; }
if (attrs.refresh !== undefined) { choicesAttrsHtml += ' refresh="' + attrs.refresh + '"'; }
if (attrs.refreshDelay !== undefined) { choicesAttrsHtml += ' refresh-delay="' + attrs.refreshDelay + '"'; }
if (attrs.backspaceReset !== undefined) { attrsHtml += ' backspace-reset="' + attrs.backspaceReset + '"';}
}

return compileTemplate(
Expand Down Expand Up @@ -808,6 +809,26 @@ describe('ui-select tests', function() {
expect(getMatchLabel(el)).toEqual('-- None Selected --');
});

describe('backspace reset option', function(){
it('should undefined model when pressing BACKSPACE key if backspaceReset=true', function() {
var el = createUiSelect();
var focusserInput = el.find('.ui-select-focusser');

clickItem(el, 'Samantha');
triggerKeydown(focusserInput, Key.Backspace);
expect(scope.selection.selected).toBeUndefined();
});

it('should NOT reset model when pressing BACKSPACE key if backspaceReset=false', function() {
var el = createUiSelect({backspaceReset: false});
var focusserInput = el.find('.ui-select-focusser');

clickItem(el, 'Samantha');
triggerKeydown(focusserInput, Key.Backspace);
expect(scope.selection.selected).toBe(scope.people[5]);
});
});

describe('disabled options', function() {
function createUiSelect(attrs) {
var attrsDisabled = '';
Expand Down