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

Commit ad5fcea

Browse files
committed
Merge pull request #747 from rwlogel/allowClear_Fix
Watch the allowClear attribute for changes
2 parents 90d2617 + 7a3e4a0 commit ad5fcea

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/uiSelectMatchDirective.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ uis.directive('uiSelectMatch', ['uiSelectConfig', function(uiSelectConfig) {
1616
$select.placeholder = placeholder !== undefined ? placeholder : uiSelectConfig.placeholder;
1717
});
1818

19-
$select.allowClear = (angular.isDefined(attrs.allowClear)) ? (attrs.allowClear === '') ? true : (attrs.allowClear.toLowerCase() === 'true') : false;
19+
function setAllowClear(allow) {
20+
$select.allowClear = (angular.isDefined(allow)) ? (allow === '') ? true : (allow.toLowerCase() === 'true') : false;
21+
}
22+
23+
attrs.$observe('allowClear', setAllowClear);
24+
setAllowClear(attrs.allowClear);
2025

2126
if($select.multiple){
2227
$select.sizeSearchInput();

test/select.spec.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ describe('ui-select tests', function() {
9494
}
9595

9696
function createUiSelect(attrs) {
97-
var attrsHtml = '';
97+
var attrsHtml = '',
98+
matchAttrsHtml = '';
9899
if (attrs !== undefined) {
99100
if (attrs.disabled !== undefined) { attrsHtml += ' ng-disabled="' + attrs.disabled + '"'; }
100101
if (attrs.required !== undefined) { attrsHtml += ' ng-required="' + attrs.required + '"'; }
@@ -104,11 +105,12 @@ describe('ui-select tests', function() {
104105
if (attrs.taggingTokens !== undefined) { attrsHtml += ' tagging-tokens="' + attrs.taggingTokens + '"'; }
105106
if (attrs.title !== undefined) { attrsHtml += ' title="' + attrs.title + '"'; }
106107
if (attrs.appendToBody != undefined) { attrsHtml += ' append-to-body="' + attrs.appendToBody + '"'; }
108+
if (attrs.allowClear != undefined) { matchAttrsHtml += ' allow-clear="' + attrs.allowClear + '"';}
107109
}
108110

109111
return compileTemplate(
110112
'<ui-select ng-model="selection.selected"' + attrsHtml + '> \
111-
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
113+
<ui-select-match placeholder="Pick one..."' + matchAttrsHtml + '>{{$select.selected.name}}</ui-select-match> \
112114
<ui-select-choices repeat="person in people | filter: $select.search"> \
113115
<div ng-bind-html="person.name | highlight: $select.search"></div> \
114116
<div ng-bind-html="person.email | highlight: $select.search"></div> \
@@ -296,6 +298,43 @@ describe('ui-select tests', function() {
296298
expect($select.open).toEqual(false);
297299
});
298300

301+
it('should clear selection', function() {
302+
scope.selection.selected = scope.people[0];
303+
304+
var el = createUiSelect({theme : 'select2', allowClear: 'true'});
305+
var $select = el.scope().$select;
306+
307+
// allowClear should be true.
308+
expect($select.allowClear).toEqual(true);
309+
310+
// Trigger clear.
311+
el.find('.select2-search-choice-close').click();
312+
expect(scope.selection.selected).toEqual(undefined);
313+
314+
// If there is no selection it the X icon should be gone.
315+
expect(el.find('.select2-search-choice-close').length).toEqual(0);
316+
317+
});
318+
319+
it('should toggle allow-clear directive', function() {
320+
scope.selection.selected = scope.people[0];
321+
scope.isClearAllowed = false;
322+
323+
var el = createUiSelect({theme : 'select2', allowClear: '{{isClearAllowed}}'});
324+
var $select = el.scope().$select;
325+
326+
expect($select.allowClear).toEqual(false);
327+
expect(el.find('.select2-search-choice-close').length).toEqual(0);
328+
329+
// Turn clear on
330+
scope.isClearAllowed = true;
331+
scope.$digest();
332+
333+
expect($select.allowClear).toEqual(true);
334+
expect(el.find('.select2-search-choice-close').length).toEqual(1);
335+
});
336+
337+
299338
it('should pass tabindex to focusser', function() {
300339
var el = createUiSelect({tabindex: 5});
301340

0 commit comments

Comments
 (0)