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 1 commit
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 @@ -105,7 +105,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
5 changes: 5 additions & 0 deletions src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ uis.directive('uiSelect',
$select.sortable = sortable !== undefined ? sortable : uiSelectConfig.sortable;
});

scope.$watch('disableBackspaceReset', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you be watching the attribute?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry. I fixed it.

var disableBackspaceReset = scope.$eval(attrs.disableBackspaceReset);
$select.disableBackspaceReset = disableBackspaceReset !== undefined ? disableBackspaceReset : 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 @@ -159,6 +159,7 @@ describe('ui-select tests', function() {
if (attrs.allowClear !== undefined) { matchAttrsHtml += ' allow-clear="' + attrs.allowClear + '"';}
if (attrs.inputId !== undefined) { attrsHtml += ' input-id="' + attrs.inputId + '"'; }
if (attrs.ngClass !== undefined) { attrsHtml += ' ng-class="' + attrs.ngClass + '"'; }
if (attrs.disableBackspaceReset !== undefined) { attrsHtml += ' disable-backspace-reset="' + attrs.disableBackspaceReset + '"';}
}

return compileTemplate(
Expand Down Expand Up @@ -800,6 +801,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