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

Commit 9c43ae7

Browse files
bekospkozlowski-opensource
authored andcommitted
feat(accordion): support is-disabled state
Closes #1126
1 parent f88546d commit 9c43ae7

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

src/accordion/accordion.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
3535
this.removeGroup = function(group) {
3636
var index = this.groups.indexOf(group);
3737
if ( index !== -1 ) {
38-
this.groups.splice(this.groups.indexOf(group), 1);
38+
this.groups.splice(index, 1);
3939
}
4040
};
4141

@@ -63,7 +63,8 @@ angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
6363
templateUrl:'template/accordion/accordion-group.html',
6464
scope: {
6565
heading: '@', // Interpolate the heading attribute onto this scope
66-
isOpen: '=?'
66+
isOpen: '=?',
67+
isDisabled: '=?'
6768
},
6869
controller: function() {
6970
this.setHeading = function(element) {
@@ -78,6 +79,12 @@ angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
7879
accordionCtrl.closeOthers(scope);
7980
}
8081
});
82+
83+
scope.toggleOpen = function() {
84+
if ( !scope.isDisabled ) {
85+
scope.isOpen = !scope.isOpen;
86+
}
87+
};
8188
}
8289
};
8390
})

src/accordion/docs/demo.html

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<div ng-controller="AccordionDemoCtrl">
2+
<p>
3+
<button class="btn btn-default btn-sm" ng-click="status.open = !status.open">Toggle last panel</button>
4+
<button class="btn btn-default btn-sm" ng-click="status.isFirstDisabled = ! status.isFirstDisabled">Enable / Disable first panel</button>
5+
</p>
6+
27
<label class="checkbox">
38
<input type="checkbox" ng-model="oneAtATime">
49
Open only one at a time
510
</label>
6-
7-
<accordion close-others="oneAtATime" ng-init="isFirstOpen = true">
8-
<accordion-group heading="Static Header, initially expanded" is-open="isFirstOpen">
11+
<accordion close-others="oneAtATime">
12+
<accordion-group heading="Static Header, initially expanded" is-open="status.isFirstOpen" is-disabled="status.isFirstDisabled">
913
This content is straight in the template.
1014
</accordion-group>
1115
<accordion-group heading="{{group.title}}" ng-repeat="group in groups">
@@ -16,9 +20,9 @@
1620
<button class="btn btn-default btn-sm" ng-click="addItem()">Add Item</button>
1721
<div ng-repeat="item in items">{{item}}</div>
1822
</accordion-group>
19-
<accordion-group is-open="isopen">
23+
<accordion-group is-open="status.open">
2024
<accordion-heading>
21-
I can have markup, too! <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': isopen, 'glyphicon-chevron-right': !isopen}"></i>
25+
I can have markup, too! <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
2226
</accordion-heading>
2327
This is just some content to illustrate fancy headings.
2428
</accordion-group>

src/accordion/docs/demo.js

+5
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ function AccordionDemoCtrl($scope) {
1818
var newItemNo = $scope.items.length + 1;
1919
$scope.items.push('Item ' + newItemNo);
2020
};
21+
22+
$scope.status = {
23+
isFirstOpen: true,
24+
isFirstDisabled: false
25+
};
2126
}

src/accordion/test/accordion.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,42 @@ describe('accordion', function () {
305305
expect(scope.groups[0].open).toBe(false);
306306
});
307307
});
308+
309+
describe('`is-disabled` attribute', function() {
310+
var groupBody;
311+
beforeEach(function () {
312+
var tpl =
313+
'<accordion>' +
314+
'<accordion-group heading="title 1" is-disabled="disabled">Content 1</accordion-group>' +
315+
'</accordion>';
316+
element = angular.element(tpl);
317+
scope.disabled = true;
318+
$compile(element)(scope);
319+
scope.$digest();
320+
groups = element.find('.panel');
321+
groupBody = findGroupBody(0);
322+
});
323+
324+
it('should open the panel with isOpen set to true', function () {
325+
expect(groupBody.scope().isOpen).toBeFalsy();
326+
});
327+
328+
it('should not toggle if disabled', function() {
329+
findGroupLink(0).click();
330+
scope.$digest();
331+
expect(groupBody.scope().isOpen).toBeFalsy();
332+
});
333+
334+
it('should toggle after enabling', function() {
335+
scope.disabled = false;
336+
scope.$digest();
337+
expect(groupBody.scope().isOpen).toBeFalsy();
338+
339+
findGroupLink(0).click();
340+
scope.$digest();
341+
expect(groupBody.scope().isOpen).toBeTruthy();
342+
});
343+
});
308344

309345
describe('accordion-heading element', function() {
310346
beforeEach(function() {

template/accordion/accordion-group.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="panel panel-default">
22
<div class="panel-heading">
33
<h4 class="panel-title">
4-
<a class="accordion-toggle" ng-click="isOpen = !isOpen" accordion-transclude="heading">{{heading}}</a>
4+
<a class="accordion-toggle" ng-click="toggleOpen()" accordion-transclude="heading"><span ng-class="{'text-muted': isDisabled}">{{heading}}</span></a>
55
</h4>
66
</div>
77
<div class="panel-collapse" collapse="!isOpen">

0 commit comments

Comments
 (0)