Skip to content

Commit 3d23fab

Browse files
authored
Merge pull request #181 from emncnozge/update-tree
Add Icon Support to TreeData
2 parents f083a01 + b233049 commit 3d23fab

File tree

5 files changed

+83
-28
lines changed

5 files changed

+83
-28
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,8 @@ treeNodes.Add("sample-child-1", new TreeNode()
13651365
Id = "sample-child-1",
13661366
Data = new TreeData()
13671367
{
1368-
Name = "Sample Child 1"
1368+
Name = "Sample Child 1",
1369+
Icon = "star"
13691370
},
13701371
HasChildren = false,
13711372
Children = new List<string>() {}

SiemensIXBlazor.Tests/TreeTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,22 @@ public async Task OnAfterRenderAsync_FirstRender_AttachesListeners()
206206
), Times.AtLeastOnce());
207207
}
208208
}
209+
210+
[Fact]
211+
public void TreeData_WithIcon_ShouldSerializeCorrectly()
212+
{
213+
// Arrange
214+
var treeData = new TreeData
215+
{
216+
Name = "Test Node",
217+
Icon = "star"
218+
};
219+
220+
// Act
221+
var json = Newtonsoft.Json.JsonConvert.SerializeObject(treeData);
222+
223+
// Assert
224+
Assert.Contains("\"name\":\"Test Node\"", json);
225+
Assert.Contains("\"icon\":\"star\"", json);
226+
}
209227
}

SiemensIXBlazor/Components/Tree/Tree.razor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using Newtonsoft.Json;
1313
using SiemensIXBlazor.Interops;
1414
using SiemensIXBlazor.Objects;
15-
using System.Dynamic;
1615
using System.Text.Json;
1716

1817
namespace SiemensIXBlazor.Components.Tree

SiemensIXBlazor/Objects/TreeData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ public class TreeData
1515
{
1616
[JsonProperty("name")]
1717
public string? Name { get; set; }
18+
[JsonProperty("icon")]
19+
public string? Icon { get; set; }
1820
}
1921
}

SiemensIXBlazor/wwwroot/js/siemens-ix/interops/treeInterop.js

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,78 @@
77
// LICENSE file in the root directory of this source tree.
88
// -----------------------------------------------------------------------
99

10+
11+
function getElementOrThrow(id) {
12+
const el = document.getElementById(id);
13+
if (!el) throw new Error(`Element with ID ${id} not found`);
14+
return el;
15+
}
16+
17+
function createElement(tag, props, children = []) {
18+
const el = document.createElement(tag);
19+
Object.assign(el, props);
20+
children.forEach(child => el.appendChild(child));
21+
return el;
22+
}
23+
1024
export function setTreeModel(id, treeModel) {
1125
try {
12-
const element = document.getElementById(id);
13-
if (!element) {
14-
throw new Error(`Element with ID ${id} not found`);
15-
}
26+
const element = getElementOrThrow(id);
27+
element.renderItem = (_, item, __, context, update) => {
28+
const { icon: iconName, name: itemName } = item.data || {};
29+
const children = [];
30+
31+
if (iconName) {
32+
children.push(createElement('ix-icon', {
33+
name: iconName,
34+
style: 'margin-right: 0.5rem'
35+
}));
36+
}
37+
38+
let nameEl;
39+
if (itemName) {
40+
nameEl = createElement('span', { innerText: itemName });
41+
children.push(nameEl);
42+
}
43+
44+
const el = createElement('ix-tree-item', {
45+
hasChildren: item.hasChildren,
46+
context: context[item.id]
47+
}, children);
48+
49+
update(updateTreeItem => {
50+
const uData = updateTreeItem.data || {};
51+
if (nameEl && uData.name) nameEl.innerText = uData.name;
52+
});
53+
54+
return el;
55+
};
1656
element.model = JSON.parse(treeModel);
17-
} catch {
18-
console.error("Failed to set tree model:", error);
57+
} catch (error) {
58+
console.error('Failed to set tree model:', error);
1959
}
2060
}
2161

2262
export function setTreeContext(id, treeContext) {
2363
try {
24-
const element = document.getElementById(id);
25-
if (!element) {
26-
throw new Error(`Element with ID ${id} not found`);
27-
}
64+
const element = getElementOrThrow(id);
2865
element.context = JSON.parse(treeContext);
29-
} catch {
30-
console.error("Failed to set tree context:", error);
66+
} catch (error) {
67+
console.error('Failed to set tree context:', error);
3168
}
3269
}
3370

3471
export function markItemAsDirty(treeId, itemIdentifiers) {
35-
console.log('markItemAsDirty called:', treeId, itemIdentifiers);
36-
37-
const treeElement = document.getElementById(treeId);
38-
if (!treeElement) {
39-
console.warn(`Tree element with id '${treeId}' not found`);
40-
return;
41-
}
42-
43-
if (typeof treeElement.markItemAsDirty === 'function') {
44-
treeElement.markItemAsDirty(itemIdentifiers);
45-
console.log('Items marked as dirty:', itemIdentifiers);
46-
} else {
47-
console.warn('markItemAsDirty method not available on tree element');
48-
}
72+
console.log('markItemAsDirty called:', treeId, itemIdentifiers);
73+
const treeElement = document.getElementById(treeId);
74+
if (!treeElement) {
75+
console.warn(`Tree element with id '${treeId}' not found`);
76+
return;
77+
}
78+
if (typeof treeElement.markItemAsDirty === 'function') {
79+
treeElement.markItemAsDirty(itemIdentifiers);
80+
console.log('Items marked as dirty:', itemIdentifiers);
81+
} else {
82+
console.warn('markItemAsDirty method not available on tree element');
83+
}
4984
}

0 commit comments

Comments
 (0)