Skip to content

Commit ab64f92

Browse files
authored
MudJsonTreeView: Exposed "Root" as a parameter for setting values directly. Fixed issue where array values wouldn't display. (#473)
1 parent 53e990f commit ab64f92

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

CodeBeam.MudBlazor.Extensions/Components/JsonTreeView/MudJsonTreeView.razor.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,62 @@ namespace MudExtensions;
1212
public partial class MudJsonTreeView : MudComponentBase
1313
{
1414
private string? _json;
15+
private JsonNode? _root;
1516

1617
/// <summary>
17-
/// Gets or sets the JSON to be displayed.
18+
/// The JSON to be displayed.
1819
/// </summary>
20+
/// <remarks>
21+
/// Use the <see cref="Root"/> parameter instead if you have a <see cref="JsonNode"/> available.
22+
/// </remarks>
1923
[Parameter]
20-
[EditorRequired]
2124
public string? Json
2225
{
2326
get => _json;
2427
set => SetJson(value);
2528
}
2629

30+
/// <summary>
31+
/// The root node of the JSON to display.
32+
/// </summary>
33+
/// <remarks>
34+
/// Use the <see cref="Json"/> parameter instead if you only have JSON available as a string.
35+
/// </remarks>
36+
[Parameter]
37+
public JsonNode? Root
38+
{
39+
get => _root;
40+
set => SetJson(value);
41+
}
42+
2743
/// <summary>
2844
/// Sets the <see cref="Json"/> property and raises the <see cref="OnJsonChanged"/> event.
2945
/// </summary>
3046
/// <param name="json">The new JSON to use.</param>
3147
protected void SetJson(string? json)
3248
{
3349
_json = json;
34-
Root = string.IsNullOrEmpty(_json) ? null : JsonNode.Parse(_json);
35-
OnJsonChanged.InvokeAsync(_json);
50+
_root = string.IsNullOrEmpty(_json) ? null : JsonNode.Parse(_json);
51+
OnJsonChanged.InvokeAsync(Root);
3652
StateHasChanged();
3753
}
3854

3955
/// <summary>
40-
/// Occurs when the JSON has changed.
56+
/// Sets the <see cref="Json"/> property and raises the <see cref="OnJsonChanged"/> event.
4157
/// </summary>
42-
public EventCallback<string> OnJsonChanged { get; set; }
58+
/// <param name="json">The new JSON to use.</param>
59+
protected void SetJson(JsonNode? json)
60+
{
61+
_json = json?.ToJsonString();
62+
_root = json;
63+
OnJsonChanged.InvokeAsync(Root);
64+
StateHasChanged();
65+
}
4366

4467
/// <summary>
45-
/// Gets or sets the root node of the JSON to display.
68+
/// Occurs when the JSON has changed.
4669
/// </summary>
47-
public JsonNode? Root { get; set; }
70+
public EventCallback<JsonNode> OnJsonChanged { get; set; }
4871

4972
/// <summary>
5073
/// Gets or sets a value indicating whether the tree contents are compacted.

CodeBeam.MudBlazor.Extensions/Components/JsonTreeView/MudJsonTreeViewNode.razor

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,49 @@ else if (Node is JsonArray)
7575
<MudJsonTreeViewNode Node="@child" Sorted="@Sorted" />
7676
</MudTreeViewItem>
7777
}
78+
}
79+
else if (Node is JsonNode)
80+
{
81+
var valueKind = Node.AsValue().GetValue<JsonElement>().ValueKind;
82+
switch (valueKind)
83+
{
84+
case JsonValueKind.String:
85+
var str = Node.AsValue().GetValue<string>();
86+
@* Could be a date *@
87+
if (DateTime.TryParse(str, out DateTime date))
88+
{
89+
<MudTreeViewItem T="string" Icon="@Icons.Material.Filled.DateRange" EndText="@date.ToString()"></MudTreeViewItem>
90+
}
91+
@* Could be a GUID *@
92+
else if (Guid.TryParse(str, out Guid guid))
93+
{
94+
<MudTreeViewItem T="string" Icon="@Icons.Material.Filled.Key" EndText="@str.ToUpperInvariant()"></MudTreeViewItem>
95+
}
96+
@* Fall back to string *@
97+
else
98+
{
99+
<MudTreeViewItem T="string" Icon="@Icons.Material.Filled.TextSnippet" EndText="@str"></MudTreeViewItem>
100+
}
101+
break;
102+
case JsonValueKind.Number:
103+
JsonValue jsonVal = Node.AsValue();
104+
string endText = string.Empty;
105+
@* We try for int first, because an int can always be converted to double but not the other way around*@
106+
if (jsonVal.TryGetValue<int>(out int intVal))
107+
{
108+
endText = intVal.ToString();
109+
}
110+
else if (jsonVal.TryGetValue<double>(out double doubleVal))
111+
{
112+
endText = doubleVal.ToString();
113+
}
114+
<MudTreeViewItem T="string" Icon="@Icons.Material.Filled.Numbers" EndText="@endText"></MudTreeViewItem>
115+
break;
116+
case JsonValueKind.True:
117+
<MudTreeViewItem T="string" Icon="@Icons.Material.Filled.CheckBox" EndText="true"></MudTreeViewItem>
118+
break;
119+
case JsonValueKind.False:
120+
<MudTreeViewItem T="string" Icon="@Icons.Material.Filled.CheckBoxOutlineBlank" EndText="false"></MudTreeViewItem>
121+
break;
122+
}
78123
}

0 commit comments

Comments
 (0)