Skip to content

Commit 7022407

Browse files
committed
Now remembering the user-selected state of the TreeNode Expanded value. Fixes #68
1 parent 43401c7 commit 7022407

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

samples/AfterBlazorServerSide/Shared/MainLayout.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
<div class="main">
88
<div class="top-row px-4">
9-
<a href="https://github.com/FritzAndFriends/BlazorWebFormsComponents" target="_blank">About BlazorWebFormsComponents</a>
9+
<a target="_blank" href="https://fritzandfriends.github.io/BlazorWebFormsComponents/">BlazorWebFormsComponents Reference Documentation</a>
10+
<a target="_blank" href="https://github.com/FritzAndFriends/BlazorWebFormsComponents">About BlazorWebFormsComponents</a>
1011
</div>
1112

1213
<div class="content px-4">

samples/AfterBlazorServerSide/Shared/NavMenu.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,4 @@
113113
{
114114
collapseNavMenu = !collapseNavMenu;
115115
}
116-
}
116+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
@inherits TestComponentBase
2+
@** Addresses issue #68 -- Remember the collapsed state across clicks *@
3+
4+
<Fixture Test="PersistExpandedState">
5+
<ComponentUnderTest>
6+
<TreeView id="SampleTreeView" runat="server">
7+
<Nodes>
8+
<TreeNode Value="Parent"
9+
Text="Parent"
10+
Expanded="false">
11+
<TreeNode Value="Child" Text="Child">
12+
<TreeNode Value="GrandChild_1" Text="GrandChild_1" />
13+
<TreeNode Value="GrandChild_2" Text="GrandChild_2" />
14+
</TreeNode>
15+
</TreeNode>
16+
</Nodes>
17+
</TreeView>
18+
</ComponentUnderTest>
19+
20+
@code {
21+
22+
private TreeNode _ExpandedNode;
23+
24+
void PersistExpandedState(Fixture fixture)
25+
{
26+
var cut = fixture.GetComponentUnderTest<TreeView>();
27+
var rootNode = cut.FindComponents<TreeNode>().First(n => n.Instance.Value == "Parent");
28+
var childNode = cut.FindComponents<TreeNode>().First(n => n.Instance.Text == "Child");
29+
30+
// Expand the root node
31+
rootNode.Find("a").Click();
32+
rootNode.Instance.Expanded.ShouldBeTrue("Root node was not expanded");
33+
34+
// Collapse the child node
35+
childNode.Find("a").Click();
36+
childNode.Instance.Expanded.ShouldBeFalse("Child node was not collapsed");
37+
38+
// Collapse the root node
39+
rootNode.Find("a").Click();
40+
rootNode.Instance.Expanded.ShouldBeFalse("Root node was not collapsed");
41+
42+
// Expand the root node again
43+
rootNode.Find("a").Click();
44+
rootNode.Instance.Expanded.ShouldBeTrue("Root node was not expanded a second time");
45+
46+
// The Child node should still be collapsed
47+
childNode.Instance.Expanded.ShouldBeFalse("Child node did not persist the collapsed state");
48+
49+
}
50+
}
51+
52+
</Fixture>

src/BlazorWebFormsComponents/TreeNode.razor.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ public partial class TreeNode : ComponentBase
2020
[Parameter]
2121
public byte Depth { get; set; } = 0;
2222

23+
private bool _Expanded = true;
24+
private bool? _UserExpanded;
2325
[Parameter]
24-
public bool Expanded { get; set; } = true;
26+
public bool Expanded
27+
{
28+
get { return _UserExpanded.HasValue ? _UserExpanded.Value : _Expanded; }
29+
set { _Expanded = value; }
30+
}
2531

2632
// TODO: Implement
2733
[Parameter]
@@ -179,9 +185,11 @@ protected override Task OnInitializedAsync()
179185

180186
Parent?.AddChildNode(this);
181187

188+
182189
return base.OnInitializedAsync();
183190
}
184191

192+
185193
protected override void OnParametersSet() {
186194

187195
if (!ParentTreeView.Nodes.Contains(this))
@@ -193,7 +201,7 @@ protected override void OnParametersSet() {
193201

194202
public void HandleNodeExpand() {
195203

196-
Expanded = !Expanded;
204+
_UserExpanded = !Expanded;
197205

198206
if (Expanded) ParentTreeView.OnTreeNodeExpanded.InvokeAsync(new TreeNodeEventArgs(this));
199207
else ParentTreeView.OnTreeNodeCollapsed.InvokeAsync(new TreeNodeEventArgs(this));

0 commit comments

Comments
 (0)