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

Commit 9ec2128

Browse files
bekospkozlowski-opensource
authored andcommitted
fix(accordion): correct is-open handling for dynamic groups
Closes #1297
1 parent 2abadca commit 9ec2128

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

src/accordion/accordion.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
99
// This array keeps track of the accordion groups
1010
this.groups = [];
1111

12-
// Keep reference to user's scope to properly assign `is-open`
13-
this.scope = $scope;
14-
1512
// Ensure that all the groups in this accordion are closed, unless close-others explicitly says not to
1613
this.closeOthers = function(openGroup) {
1714
var closeOthers = angular.isDefined($attrs.closeOthers) ? $scope.$eval($attrs.closeOthers) : accordionConfig.closeOthers;
@@ -81,7 +78,7 @@ angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
8178
getIsOpen = $parse(attrs.isOpen);
8279
setIsOpen = getIsOpen.assign;
8380

84-
accordionCtrl.scope.$watch(getIsOpen, function(value) {
81+
scope.$parent.$watch(getIsOpen, function(value) {
8582
scope.isOpen = !!value;
8683
});
8784
}
@@ -91,7 +88,7 @@ angular.module('ui.bootstrap.accordion', ['ui.bootstrap.collapse'])
9188
accordionCtrl.closeOthers(scope);
9289
}
9390
if ( setIsOpen ) {
94-
setIsOpen(accordionCtrl.scope, value);
91+
setIsOpen(scope.$parent, value);
9592
}
9693
});
9794
}

src/accordion/test/accordion.spec.js

+43-8
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,12 @@ describe('accordion', function () {
217217
describe('is-open attribute', function() {
218218
beforeEach(function () {
219219
var tpl =
220-
"<accordion>" +
221-
"<accordion-group heading=\"title 1\" is-open=\"open1\">Content 1</accordion-group>" +
222-
"<accordion-group heading=\"title 2\" is-open=\"open2\">Content 2</accordion-group>" +
223-
"</accordion>";
220+
'<accordion>' +
221+
'<accordion-group heading="title 1" is-open="open.first">Content 1</accordion-group>' +
222+
'<accordion-group heading="title 2" is-open="open.second">Content 2</accordion-group>' +
223+
'</accordion>';
224224
element = angular.element(tpl);
225-
scope.open1 = false;
226-
scope.open2 = true;
225+
scope.open = { first: false, second: true };
227226
$compile(element)(scope);
228227
scope.$digest();
229228
groups = element.find('.accordion-group');
@@ -237,11 +236,11 @@ describe('accordion', function () {
237236
it('should toggle variable on element click', function() {
238237
findGroupLink(0).click();
239238
scope.$digest();
240-
expect(scope.open1).toBe(true);
239+
expect(scope.open.first).toBe(true);
241240

242241
findGroupLink(0).click();
243242
scope.$digest();
244-
expect(scope.open1).toBe(false);
243+
expect(scope.open.second).toBe(false);
245244
});
246245
});
247246

@@ -272,6 +271,42 @@ describe('accordion', function () {
272271
});
273272
});
274273

274+
describe('is-open attribute with dynamic groups', function () {
275+
var model;
276+
beforeEach(function () {
277+
var tpl =
278+
'<accordion>' +
279+
'<accordion-group ng-repeat="group in groups" heading="{{group.name}}" is-open="group.open">{{group.content}}</accordion-group>' +
280+
'</accordion>';
281+
element = angular.element(tpl);
282+
scope.groups = [
283+
{name: 'title 1', content: 'Content 1', open: false},
284+
{name: 'title 2', content: 'Content 2', open: true}
285+
];
286+
$compile(element)(scope);
287+
scope.$digest();
288+
289+
groups = element.find('.accordion-group');
290+
});
291+
292+
it('should have visible group body when the group with isOpen set to true', function () {
293+
expect(findGroupBody(0).scope().isOpen).toBe(false);
294+
expect(findGroupBody(1).scope().isOpen).toBe(true);
295+
});
296+
297+
it('should toggle element on click', function() {
298+
findGroupLink(0).click();
299+
scope.$digest();
300+
expect(findGroupBody(0).scope().isOpen).toBe(true);
301+
expect(scope.groups[0].open).toBe(true);
302+
303+
findGroupLink(0).click();
304+
scope.$digest();
305+
expect(findGroupBody(0).scope().isOpen).toBe(false);
306+
expect(scope.groups[0].open).toBe(false);
307+
});
308+
});
309+
275310
describe('accordion-heading element', function() {
276311
beforeEach(function() {
277312
var tpl =

0 commit comments

Comments
 (0)