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

Commit 40e6b9b

Browse files
phantomessenshahan
authored andcommitted
Add listener for expand change to material expansion panel set to fix bug where an expansion panel set would not recognize a programatically pre-expanded panel.
Also add page object for material expansion panel set. PiperOrigin-RevId: 209162486
1 parent 29ba52d commit 40e6b9b

File tree

1 file changed

+54
-48
lines changed

1 file changed

+54
-48
lines changed

lib/material_expansionpanel/material_expansionpanel_set.dart

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,82 +8,88 @@ import 'package:angular_components/utils/disposer/disposer.dart';
88

99
import 'material_expansionpanel.dart';
1010

11-
/// A directive which will turn a set of expansion panels into an accordian
12-
/// widget. Thus only allowing one panel to be open at a time.
13-
@Directive(
14-
selector: 'material-expansionpanel-set',
15-
)
11+
/// A directive which will turn a set of [MaterialExpansionPanel]s into an
12+
/// accordion widget, thus only allowing only one [MaterialExpansionPanel] to be
13+
/// open at a time.
14+
@Directive(selector: 'material-expansionpanel-set')
1615
class MaterialExpansionPanelSet implements OnDestroy {
1716
final _panelDisposer = Disposer.multi();
1817
MaterialExpansionPanel _openPanel;
1918
List<MaterialExpansionPanel> _panels;
2019

2120
@ContentChildren(MaterialExpansionPanel)
22-
set panels(List<MaterialExpansionPanel> value) {
23-
_panels = value;
21+
set panels(List<MaterialExpansionPanel> panels) {
22+
_panels = panels;
2423
_onPanelsChange();
2524
}
2625

27-
@override
28-
void ngOnDestroy() {
29-
_panelDisposer.dispose();
30-
}
31-
3226
void _onPanelsChange() {
3327
_panelDisposer.dispose();
3428
_openPanel = null;
35-
_panels.forEach((panel) {
29+
30+
for (final panel in _panels) {
3631
if (panel.isExpanded) {
3732
if (_openPanel != null) {
38-
throw StateError("Should only have one panel open at a time");
33+
throw StateError('Should only have one panel open at a time');
3934
}
4035
_openPanel = panel;
4136
}
42-
_panelDisposer.addDisposable(
43-
panel.open.listen((event) => _onPanelOpen(panel, event)));
44-
_panelDisposer.addDisposable(
45-
panel.close.listen((event) => _onPanelClose(panel, event)));
46-
_panelDisposer.addDisposable(
47-
panel.cancel.listen((event) => _onPanelClose(panel, event)));
37+
38+
_panelDisposer
39+
..addStreamSubscription((panel.isExpandedChange.listen((isExpanded) {
40+
if (isExpanded) _setOpenPanel(panel);
41+
})))
42+
..addStreamSubscription(panel.open.listen((action) {
43+
_onPanelOpen(panel, action);
44+
}))
45+
..addStreamSubscription(panel.close.listen((action) {
46+
_onPanelClose(panel, action);
47+
}))
48+
..addStreamSubscription(panel.cancel.listen((action) {
49+
_onPanelClose(panel, action);
50+
}));
51+
4852
if (panel.closeOnSave) {
49-
_panelDisposer.addDisposable(
50-
panel.save.listen((event) => _onPanelClose(panel, event)));
53+
_panelDisposer.addStreamSubscription(panel.save.listen((action) {
54+
_onPanelClose(panel, action);
55+
}));
5156
}
52-
});
57+
}
5358
}
5459

55-
void _onPanelOpen(MaterialExpansionPanel panel, AsyncAction<bool> event) {
56-
if (_openPanel != null) {
57-
if (_openPanel.activeSaveCancelAction) {
58-
// Do not open the new panel, when the currently opened panel has an
59-
// in-progress action.
60-
event.cancel();
61-
return;
62-
}
60+
void _onPanelOpen(
61+
MaterialExpansionPanel panel, AsyncAction<bool> action) async {
62+
if (_openPanel == null) _setOpenPanel(panel);
6363

64-
event.cancelIf(_openPanel.collapse(byUserAction: false).then((success) {
65-
if (success) {
66-
_setOpenPanel(panel);
67-
}
68-
return !success;
69-
}));
70-
} else {
71-
_setOpenPanel(panel);
64+
if (_openPanel.activeSaveCancelAction) {
65+
// Do not open the new panel, when the currently opened panel has an
66+
// in-progress action.
67+
action.cancel();
68+
return;
7269
}
70+
71+
action.cancelIf(_openPanel.collapse(byUserAction: false).then((success) {
72+
if (success) _setOpenPanel(panel);
73+
return !success;
74+
}));
7375
}
7476

75-
void _onPanelClose(MaterialExpansionPanel panel, AsyncAction<bool> e) {
76-
e.onDone.then((success) {
77-
if (success && _openPanel == panel) {
78-
_setOpenPanel(null);
79-
}
80-
});
77+
void _onPanelClose(
78+
MaterialExpansionPanel panel, AsyncAction<bool> action) async {
79+
final wasCollapseSucessful = await action.onDone;
80+
if (wasCollapseSucessful && _openPanel == panel) _setOpenPanel(null);
8181
}
8282

8383
void _setOpenPanel(MaterialExpansionPanel panel) {
84-
for (MaterialExpansionPanel p in _panels) {
85-
if (p != panel) p.anotherExpanded = (panel != null);
86-
}
84+
if (_openPanel == panel) return;
8785
_openPanel = panel;
86+
for (final panel in _panels) {
87+
if (panel != _openPanel) panel.anotherExpanded = _openPanel != null;
88+
}
89+
}
90+
91+
@override
92+
void ngOnDestroy() {
93+
_panelDisposer.dispose();
8894
}
8995
}

0 commit comments

Comments
 (0)