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 3 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 @@ -106,7 +106,8 @@ var uis = angular.module('ui.select', [])
generateId: function() {
return latestId++;
},
appendToBody: false
appendToBody: false,
disableBackspaceReset: false
})

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

scope.$watch(function () { return scope.$eval(attrs.disableBackspaceReset); }, function(newVal) {
$select.disableBackspaceReset = newVal !== undefined ? newVal : uiSelectConfig.disableBackspaceReset;
});

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.disableBackspaceReset === 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 @@ -161,6 +161,7 @@ describe('ui-select tests', function() {
if (attrs.ngClass !== undefined) { attrsHtml += ' ng-class="' + attrs.ngClass + '"'; }
if (attrs.resetSearchInput !== undefined) { attrsHtml += ' reset-search-input="' + attrs.resetSearchInput + '"'; }
if (attrs.closeOnSelect !== undefined) { attrsHtml += ' close-on-select="' + attrs.closeOnSelect + '"'; }
if (attrs.disableBackspaceReset !== undefined) { attrsHtml += ' disable-backspace-reset="' + attrs.disableBackspaceReset + '"';}
}

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

describe('disable backspace reset option', function(){
it('should undefined model when pressing BACKSPACE key if disableBackspaceReset=false', 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 disableBackspaceReset=true', function() {
var el = createUiSelect({disableBackspaceReset: true});
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