Skip to content

Commit e79c22b

Browse files
pierregasteBogaerts Kristof
authored and
Bogaerts Kristof
committed
feat(uiSelectSingle): add option to avoid backspace resetting the model
Adds `backspace-reset` attribute (default: `true`). Currently, when you select an option and you press the backspace key, it will reset the model. Setting `backspace-reset="false"` will disable this behavior and so avoid resetting the model. Closes angular-ui#926 angular-ui#525
1 parent 2bc3467 commit e79c22b

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/common.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ var uis = angular.module('ui.select', [])
108108
},
109109
appendToBody: false,
110110
spinnerEnabled: false,
111-
spinnerClass: 'glyphicon-refresh ui-select-spin'
111+
spinnerClass: 'glyphicon-refresh ui-select-spin',
112+
backspaceReset: true
112113
})
113114

114115
// See Rename minErr and make it accessible from outside https://github.com/angular/angular.js/issues/6913

src/uiSelectDirective.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ uis.directive('uiSelect',
8383
$select.sortable = sortable !== undefined ? sortable : uiSelectConfig.sortable;
8484
});
8585

86+
attrs.$observe('backspaceReset', function() {
87+
// $eval() is needed otherwise we get a string instead of a boolean
88+
var backspaceReset = scope.$eval(attrs.backspaceReset);
89+
$select.backspaceReset = backspaceReset !== undefined ? backspaceReset : true;
90+
});
91+
8692
attrs.$observe('limit', function() {
8793
//Limit the number of selections allowed
8894
$select.limit = (angular.isDefined(attrs.limit)) ? parseInt(attrs.limit, 10) : undefined;

src/uiSelectSingleDirective.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ uis.directive('uiSelectSingle', ['$timeout','$compile', function($timeout, $comp
8585
});
8686
focusser.bind("keydown", function(e){
8787

88-
if (e.which === KEY.BACKSPACE) {
88+
if (e.which === KEY.BACKSPACE && $select.backspaceReset !== false) {
8989
e.preventDefault();
9090
e.stopPropagation();
9191
$select.select(undefined);

test/select.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ describe('ui-select tests', function() {
167167
if (attrs.spinnerClass !== undefined) { attrsHtml += ' spinner-class="' + attrs.spinnerClass + '"'; }
168168
if (attrs.refresh !== undefined) { choicesAttrsHtml += ' refresh="' + attrs.refresh + '"'; }
169169
if (attrs.refreshDelay !== undefined) { choicesAttrsHtml += ' refresh-delay="' + attrs.refreshDelay + '"'; }
170+
if (attrs.backspaceReset !== undefined) { attrsHtml += ' backspace-reset="' + attrs.backspaceReset + '"';}
170171
}
171172

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

812+
describe('backspace reset option', function(){
813+
it('should undefined model when pressing BACKSPACE key if backspaceReset=true', function() {
814+
var el = createUiSelect();
815+
var focusserInput = el.find('.ui-select-focusser');
816+
817+
clickItem(el, 'Samantha');
818+
triggerKeydown(focusserInput, Key.Backspace);
819+
expect(scope.selection.selected).toBeUndefined();
820+
});
821+
822+
it('should NOT reset model when pressing BACKSPACE key if backspaceReset=false', function() {
823+
var el = createUiSelect({backspaceReset: false});
824+
var focusserInput = el.find('.ui-select-focusser');
825+
826+
clickItem(el, 'Samantha');
827+
triggerKeydown(focusserInput, Key.Backspace);
828+
expect(scope.selection.selected).toBe(scope.people[5]);
829+
});
830+
});
831+
811832
describe('disabled options', function() {
812833
function createUiSelect(attrs) {
813834
var attrsDisabled = '';

0 commit comments

Comments
 (0)