Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit c95f213

Browse files
author
Pierre Gasté
committed
feat(uiSelectSingleDirective): add an option to avoid backspace model reset
Currently, when you select an option and you press the backspace key, it will reset the model. With this option, we could disable this behavior to avoid the model resetting. Closes angular-ui#926 angular-ui#525
1 parent 3dfde71 commit c95f213

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/common.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ var uis = angular.module('ui.select', [])
105105
generateId: function() {
106106
return latestId++;
107107
},
108-
appendToBody: false
108+
appendToBody: false,
109+
disableBackspaceReset: false
109110
})
110111

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

src/uiSelectDirective.js

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

86+
scope.$watch('disableBackspaceReset', function() {
87+
var disableBackspaceReset = scope.$eval(attrs.disableBackspaceReset);
88+
$select.disableBackspaceReset = disableBackspaceReset !== undefined ? disableBackspaceReset : uiSelectConfig.disableBackspaceReset;
89+
});
90+
8691
attrs.$observe('limit', function() {
8792
//Limit the number of selections allowed
8893
$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.disableBackspaceReset === 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
@@ -159,6 +159,7 @@ describe('ui-select tests', function() {
159159
if (attrs.allowClear !== undefined) { matchAttrsHtml += ' allow-clear="' + attrs.allowClear + '"';}
160160
if (attrs.inputId !== undefined) { attrsHtml += ' input-id="' + attrs.inputId + '"'; }
161161
if (attrs.ngClass !== undefined) { attrsHtml += ' ng-class="' + attrs.ngClass + '"'; }
162+
if (attrs.disableBackspaceReset !== undefined) { attrsHtml += ' disable-backspace-reset="' + attrs.disableBackspaceReset + '"';}
162163
}
163164

164165
return compileTemplate(
@@ -800,6 +801,26 @@ describe('ui-select tests', function() {
800801
expect(getMatchLabel(el)).toEqual('-- None Selected --');
801802
});
802803

804+
describe('disable backspace reset option', function(){
805+
it('should undefined model when pressing BACKSPACE key if disableBackspaceReset=false', function() {
806+
var el = createUiSelect();
807+
var focusserInput = el.find('.ui-select-focusser');
808+
809+
clickItem(el, 'Samantha');
810+
triggerKeydown(focusserInput, Key.Backspace);
811+
expect(scope.selection.selected).toBeUndefined();
812+
});
813+
814+
it('should NOT reset model when pressing BACKSPACE key if disableBackspaceReset=true', function() {
815+
var el = createUiSelect({disableBackspaceReset: true});
816+
var focusserInput = el.find('.ui-select-focusser');
817+
818+
clickItem(el, 'Samantha');
819+
triggerKeydown(focusserInput, Key.Backspace);
820+
expect(scope.selection.selected).toBe(scope.people[5]);
821+
});
822+
});
823+
803824
describe('disabled options', function() {
804825
function createUiSelect(attrs) {
805826
var attrsDisabled = '';

0 commit comments

Comments
 (0)