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

Commit c5d925d

Browse files
committed
Handle disabled attribute
1 parent 5725598 commit c5d925d

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

examples/select2-bootstrap3.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@
8787
</div>
8888
</div>
8989

90+
<div class="form-group">
91+
<label class="col-sm-3 control-label">Disabled</label>
92+
<div class="col-sm-6">
93+
94+
<ui-select ng-model="person.selected" theme="select2" class="form-control" ng-disabled="true">
95+
<match placeholder="Select or search a person in the list...">{{$select.selected.name}}</match>
96+
<choices repeat="item in people | filter: $select.search">
97+
<div ng-bind-html="trustAsHtml((item.name | highlight: $select.search))"></div>
98+
<small ng-bind-html="trustAsHtml((item.email | highlight: $select.search))"></small>
99+
</choices>
100+
</ui-select>
101+
102+
</div>
103+
</div>
104+
90105
</fieldset>
91106
</form>
92107

src/select.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ angular.module('ui.select', [])
6969
var $select = controllers[0];
7070
var ngModelCtrl = controllers[1];
7171

72+
attrs.$observe('disabled', function() {
73+
$select.disabled = attrs.disabled ? true : false;
74+
});
75+
7276
scope.$watch('$select.selected', function(newVal, oldVal) {
7377
if (ngModelCtrl.$viewValue !== newVal) ngModelCtrl.$setViewValue(newVal);
7478
});

src/select2/select.tpl.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<div class="select2 select2-container"
2-
ng-class="{'select2-container-active select2-dropdown-open': $select.open}">
2+
ng-class="{'select2-container-active select2-dropdown-open': $select.open && !$select.disabled,
3+
'select2-container-disabled': $select.disabled}">
34
<div class="ui-select-match"></div>
4-
<div ng-class="{'select2-display-none': !$select.open}"
5+
<div ng-class="{'select2-display-none': !$select.open || $select.disabled}"
56
class="select2-drop select2-with-searchbox select2-drop-active">
67
<div class="select2-search">
78
<input type="text"

test/select.spec.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ describe('ui-select tests', function() {
2727
return el;
2828
}
2929

30-
function createUiSelect() {
30+
function createUiSelect(attrs) {
31+
var attrsHtml = '';
32+
if (attrs !== undefined) {
33+
if (attrs.disabled !== undefined) { attrsHtml += ' ng-disabled="' + attrs.disabled + '"'; }
34+
if (attrs.required !== undefined) { attrsHtml += ' ng-required="' + attrs.required + '"'; }
35+
}
36+
3137
return compileTemplate(
32-
'<ui-select ng-model="selection"> \
38+
'<ui-select ng-model="selection"' + attrsHtml + '> \
3339
<match placeholder="Pick one...">{{$select.selected.name}}</match> \
3440
<choices repeat="item in matches | filter: $select.search"> \
3541
<div ng-bind-html="trustAsHtml((item.name | highlight: $select.search))"></div> \
@@ -140,4 +146,15 @@ describe('ui-select tests', function() {
140146

141147
expect(el.scope().$select.open).toEqual(false);
142148
});
149+
150+
it('should be disabled if the attribute says so', function() {
151+
var el1 = createUiSelect({disabled: true});
152+
expect(el1.scope().$select.disabled).toEqual(true);
153+
154+
var el2 = createUiSelect({disabled: false});
155+
expect(el2.scope().$select.disabled).toEqual(false);
156+
157+
var el3 = createUiSelect();
158+
expect(el3.scope().$select.disabled).toEqual(false);
159+
});
143160
});

0 commit comments

Comments
 (0)