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

feat(accordion): support disabled state #1126

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 9 additions & 2 deletions src/accordion/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
this.removeGroup = function(group) {
var index = this.groups.indexOf(group);
if ( index !== -1 ) {
this.groups.splice(this.groups.indexOf(group), 1);
this.groups.splice(index, 1);
}
};

Expand Down Expand Up @@ -63,7 +63,8 @@ angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
templateUrl:'template/accordion/accordion-group.html',
scope: {
heading: '@', // Interpolate the heading attribute onto this scope
isOpen: '=?'
isOpen: '=?',
isDisabled: '=?'
},
controller: function() {
this.setHeading = function(element) {
Expand All @@ -78,6 +79,12 @@ angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
accordionCtrl.closeOthers(scope);
}
});

scope.toggleOpen = function() {
if ( !scope.isDisabled ) {
scope.isOpen = !scope.isOpen;
}
};
}
};
})
Expand Down
14 changes: 9 additions & 5 deletions src/accordion/docs/demo.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<div ng-controller="AccordionDemoCtrl">
<p>
<button class="btn btn-default btn-sm" ng-click="status.open = !status.open">Toggle last panel</button>
<button class="btn btn-default btn-sm" ng-click="status.isFirstDisabled = ! status.isFirstDisabled">Enable / Disable first panel</button>
</p>

<label class="checkbox">
<input type="checkbox" ng-model="oneAtATime">
Open only one at a time
</label>

<accordion close-others="oneAtATime" ng-init="isFirstOpen = true">
<accordion-group heading="Static Header, initially expanded" is-open="isFirstOpen">
<accordion close-others="oneAtATime">
<accordion-group heading="Static Header, initially expanded" is-open="status.isFirstOpen" is-disabled="status.isFirstDisabled">
This content is straight in the template.
</accordion-group>
<accordion-group heading="{{group.title}}" ng-repeat="group in groups">
Expand All @@ -16,9 +20,9 @@
<button class="btn btn-default btn-sm" ng-click="addItem()">Add Item</button>
<div ng-repeat="item in items">{{item}}</div>
</accordion-group>
<accordion-group is-open="isopen">
<accordion-group is-open="status.open">
<accordion-heading>
I can have markup, too! <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': isopen, 'glyphicon-chevron-right': !isopen}"></i>
I can have markup, too! <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
</accordion-heading>
This is just some content to illustrate fancy headings.
</accordion-group>
Expand Down
5 changes: 5 additions & 0 deletions src/accordion/docs/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ function AccordionDemoCtrl($scope) {
var newItemNo = $scope.items.length + 1;
$scope.items.push('Item ' + newItemNo);
};

$scope.status = {
isFirstOpen: true,
isFirstDisabled: false
};
}
36 changes: 36 additions & 0 deletions src/accordion/test/accordion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,42 @@ describe('accordion', function () {
expect(scope.groups[0].open).toBe(false);
});
});

describe('`is-disabled` attribute', function() {
var groupBody;
beforeEach(function () {
var tpl =
'<accordion>' +
'<accordion-group heading="title 1" is-disabled="disabled">Content 1</accordion-group>' +
'</accordion>';
element = angular.element(tpl);
scope.disabled = true;
$compile(element)(scope);
scope.$digest();
groups = element.find('.panel');
groupBody = findGroupBody(0);
});

it('should open the panel with isOpen set to true', function () {
expect(groupBody.scope().isOpen).toBeFalsy();
});

it('should not toggle if disabled', function() {
findGroupLink(0).click();
scope.$digest();
expect(groupBody.scope().isOpen).toBeFalsy();
});

it('should toggle after enabling', function() {
scope.disabled = false;
scope.$digest();
expect(groupBody.scope().isOpen).toBeFalsy();

findGroupLink(0).click();
scope.$digest();
expect(groupBody.scope().isOpen).toBeTruthy();
});
});

describe('accordion-heading element', function() {
beforeEach(function() {
Expand Down
2 changes: 1 addition & 1 deletion template/accordion/accordion-group.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" ng-click="isOpen = !isOpen" accordion-transclude="heading">{{heading}}</a>
<a class="accordion-toggle" ng-click="toggleOpen()" accordion-transclude="heading"><span ng-class="{'text-muted': isDisabled}">{{heading}}</span></a>
</h4>
</div>
<div class="panel-collapse" collapse="!isOpen">
Expand Down