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

Commit 33650f9

Browse files
committed
feat(templates): Support disabled options in select menu.
1 parent fc42fdf commit 33650f9

File tree

6 files changed

+80
-15
lines changed

6 files changed

+80
-15
lines changed

src/bootstrap/choices.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<li class="ui-select-choices-group">
55
<div class="divider" ng-show="$select.isGrouped && $index > 0"></div>
66
<div ng-show="$select.isGrouped" class="ui-select-choices-group-label dropdown-header">{{$group.name}}</div>
7-
<div class="ui-select-choices-row" ng-class="{active: $select.isActive(this)}">
7+
<div class="ui-select-choices-row" ng-class="{active: $select.isActive(this), disabled: $select.isDisabled(this)}">
88
<a href="javascript:void(0)" class="ui-select-choices-row-inner"></a>
99
</div>
1010
</li>

src/select.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@
136136
background-color: #428bca;
137137
}
138138

139+
.ui-select-bootstrap .ui-select-choices-row.disabled>a,
140+
.ui-select-bootstrap .ui-select-choices-row.active.disabled>a {
141+
color: #777;
142+
cursor: not-allowed;
143+
background-color: #fff;
144+
}
145+
139146
/* fix hide/show angular animation */
140147
.ui-select-match.ng-hide-add,
141148
.ui-select-search.ng-hide-add {

src/select.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -299,24 +299,39 @@
299299
return ctrl.items.indexOf(itemScope[ctrl.itemProperty]) === ctrl.activeIndex;
300300
};
301301

302+
ctrl.isDisabled = function(itemScope) {
303+
var itemIndex = ctrl.items.indexOf(itemScope[ctrl.itemProperty]);
304+
var isDisabled = false;
305+
var item;
306+
307+
if (itemIndex >= 0) {
308+
item = ctrl.items[itemIndex];
309+
isDisabled = item.disabled;
310+
}
311+
312+
return isDisabled;
313+
};
314+
302315
// When the user clicks on an item inside the dropdown
303316
ctrl.select = function(item) {
304317

305-
var locals = {};
306-
locals[ctrl.parserResult.itemName] = item;
318+
if (!item.disabled) {
319+
var locals = {};
320+
locals[ctrl.parserResult.itemName] = item;
307321

308-
ctrl.onSelectCallback($scope, {
309-
$item: item,
310-
$model: ctrl.parserResult.modelMapper($scope, locals)
311-
});
322+
ctrl.onSelectCallback($scope, {
323+
$item: item,
324+
$model: ctrl.parserResult.modelMapper($scope, locals)
325+
});
312326

313-
if(ctrl.multiple){
314-
ctrl.selected.push(item);
315-
ctrl.sizeSearchInput();
316-
} else {
317-
ctrl.selected = item;
327+
if(ctrl.multiple){
328+
ctrl.selected.push(item);
329+
ctrl.sizeSearchInput();
330+
} else {
331+
ctrl.selected = item;
332+
}
333+
ctrl.close();
318334
}
319-
ctrl.close();
320335
};
321336

322337
// Closes the dropdown

src/select2/choices.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<li class="ui-select-choices-group" ng-class="{'select2-result-with-children': $select.isGrouped}">
33
<div ng-show="$select.isGrouped" class="ui-select-choices-group-label select2-result-label">{{$group.name}}</div>
44
<ul ng-class="{'select2-result-sub': $select.isGrouped, 'select2-result-single': !$select.isGrouped}">
5-
<li class="ui-select-choices-row" ng-class="{'select2-highlighted': $select.isActive(this)}">
5+
<li class="ui-select-choices-row" ng-class="{'select2-highlighted': $select.isActive(this), 'select2-disabled': $select.isDisabled(this)}">
66
<div class="select2-result-label ui-select-choices-row-inner"></div>
77
</li>
88
</ul>

src/selectize/choices.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="ui-select-choices-content selectize-dropdown-content">
33
<div class="ui-select-choices-group optgroup">
44
<div ng-show="$select.isGrouped" class="ui-select-choices-group-label optgroup-header">{{$group.name}}</div>
5-
<div class="ui-select-choices-row" ng-class="{active: $select.isActive(this)}">
5+
<div class="ui-select-choices-row" ng-class="{active: $select.isActive(this), disabled: $select.isDisabled(this)}">
66
<div class="option ui-select-choices-row-inner" data-selectable></div>
77
</div>
88
</div>

test/select.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,49 @@ describe('ui-select tests', function() {
266266
expect(getMatchLabel(el)).toEqual('false');
267267
});
268268

269+
describe('disabled options', function() {
270+
function createUiSelect() {
271+
return compileTemplate(
272+
'<ui-select ng-model="selection.selected"> \
273+
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
274+
<ui-select-choices repeat="person in people | filter: $select.search"> \
275+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
276+
<div ng-bind-html="person.email | highlight: $select.search"></div> \
277+
</ui-select-choices> \
278+
</ui-select>'
279+
);
280+
}
281+
282+
beforeEach(function() {
283+
scope._people = angular.copy(scope.people);
284+
scope.people = scope.people.map(function (person) {
285+
if (person.name == 'Wladimir') {
286+
person.disabled = true;
287+
}
288+
return person;
289+
});
290+
291+
this.el = createUiSelect();
292+
});
293+
294+
afterEach(function() {
295+
scope.people = angular.copy(scope._people);
296+
});
297+
298+
it('should not allow disabled options to be selected', function() {
299+
clickItem(this.el, 'Wladimir');
300+
301+
expect(getMatchLabel(this.el)).not.toEqual('Wladimir');
302+
});
303+
304+
it('should set a disabled class on the option', function() {
305+
var option = $(this.el).find('.ui-select-choices-row div:contains("Wladimir")');
306+
var container = option.closest('.ui-select-choices-row');
307+
308+
expect(container.hasClass('disabled')).toBeTruthy();
309+
});
310+
});
311+
269312
describe('choices group', function() {
270313
function getGroupLabel(item) {
271314
return item.parent('.ui-select-choices-group').find('.ui-select-choices-group-label');

0 commit comments

Comments
 (0)