Skip to content

Commit

Permalink
Ensure toggled Accordion only triggers one event on change of active …
Browse files Browse the repository at this point in the history
…Card (#2934)
  • Loading branch information
philippjfr committed Nov 19, 2021
1 parent b86b65e commit 314a1df
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
19 changes: 12 additions & 7 deletions panel/layout/accordion.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,20 @@ def _apply_style(self, i):
def _set_active(self, *events):
if self._updating_active:
return
active = []
self._updating_active = True
try:
for i, pane in enumerate(self.objects):
if id(pane) not in self._panels:
continue
elif not self._panels[id(pane)].collapsed:
active.append(i)
self.active = active
if self.toggle and not events[0].new:
active = [list(self._panels.values()).index(events[0].obj)]
else:
active = []
for i, pane in enumerate(self.objects):
if id(pane) not in self._panels:
continue
elif not self._panels[id(pane)].collapsed:
active.append(i)

if not self.toggle or active:
self.active = active
finally:
self._updating_active = False

Expand Down
31 changes: 31 additions & 0 deletions panel/tests/layout/test_accordion.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,45 @@ def test_accordion_active(document, comm, accordion):
def test_accordion_set_card_collapsed(document, comm, accordion):
accordion.get_root(document, comm=comm)

events = []

accordion.param.watch(lambda e: events.append(e), 'active')

c1, c2 = accordion._panels.values()

c1.collapsed = False
assert accordion.active == [0]

assert len(events) == 1

c2.collapsed = False
assert accordion.active == [0, 1]

assert len(events) == 2

c1.collapsed = True
c2.collapsed = True
assert accordion.active == []

assert len(events) == 4


def test_accordion_set_card_collapsed_toggle(document, comm, accordion):
accordion.toggle = True
accordion.get_root(document, comm=comm)

events = []

accordion.param.watch(lambda e: events.append(e), 'active')

c1, c2 = accordion._panels.values()

c1.collapsed = False
assert accordion.active == [0]

assert len(events) == 1

c2.collapsed = False
assert accordion.active == [1]

assert len(events) == 2

0 comments on commit 314a1df

Please sign in to comment.