Skip to content

Commit 25a0c67

Browse files
authored
ListExtended & SelectExtended: Search with Complex Types (#106)
* Initialize * Finalize
1 parent 3505509 commit 25a0c67

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

CodeBeam.MudExtensions/Components/ListExtended/MudListExtended.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
<MudVirtualize IsEnabled="@Virtualize" Items="GetSearchedItems()" Context="item" OverscanCount="@OverscanCount">
2828
@if (MudSelectExtended != null)
2929
{
30-
<MudSelectItemExtended Value="@item" Text="@(MudSelectExtended.ToStringFunc == null ? item.ToString() : MudSelectExtended.ToStringFunc(item))" />
30+
<MudSelectItemExtended Value="@item" Text="@(ToStringFunc == null ? item.ToString() : ToStringFunc(item))" />
3131
}
3232
else
3333
{
34-
<MudListItemExtended Value="@item" Text="@item.ToString()" />
34+
<MudListItemExtended Value="@item" Text="@(ToStringFunc == null ? item.ToString() : ToStringFunc(item))" />
3535
}
3636
</MudVirtualize>
3737
}

CodeBeam.MudExtensions/Components/ListExtended/MudListExtended.razor.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ public IEqualityComparer<T> Comparer
107107
}
108108
}
109109

110+
private Func<T, string> _toStringFunc = x => x?.ToString();
111+
/// <summary>
112+
/// Defines how values are displayed in the drop-down list
113+
/// </summary>
114+
[Parameter]
115+
[Category(CategoryTypes.FormComponent.ListBehavior)]
116+
public Func<T, string> ToStringFunc
117+
{
118+
get => _toStringFunc;
119+
set
120+
{
121+
if (_toStringFunc == value)
122+
return;
123+
_toStringFunc = value;
124+
Converter = new DefaultConverter<T>
125+
{
126+
SetFunc = _toStringFunc ?? (x => x?.ToString()),
127+
};
128+
}
129+
}
130+
110131
/// <summary>
111132
/// Predefined enumerable items. If its not null, creates list items automatically.
112133
/// </summary>

CodeBeam.MudExtensions/Components/SelectExtended/MudSelectExtended.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
Clickable="true" Color="@Color" Dense="@Dense" ItemCollection="@ItemCollection" Virtualize="@Virtualize" DisablePadding="@DisablePopoverPadding" DisableSelectedItemStyle="@DisableSelectedItemStyle"
101101
MultiSelection="@MultiSelection" MultiSelectionComponent="@MultiSelectionComponent" MultiSelectionAlign="@MultiSelectionAlign" SelectAll="@SelectAll" SelectAllText="@SelectAllText"
102102
CheckedIcon="@CheckedIcon" UncheckedIcon="@UncheckedIcon" IndeterminateIcon="@IndeterminateIcon" SelectValueOnTab="@SelectValueOnTab" Comparer="@Comparer"
103-
ItemTemplate="@ItemTemplate" ItemSelectedTemplate="@ItemSelectedTemplate" ItemDisabledTemplate="@ItemDisabledTemplate" SearchBox="@SearchBox">
103+
ItemTemplate="@ItemTemplate" ItemSelectedTemplate="@ItemSelectedTemplate" ItemDisabledTemplate="@ItemDisabledTemplate" SearchBox="@SearchBox" ToStringFunc="@ToStringFunc">
104104
@ChildContent
105105
</MudListExtended>
106106
</CascadingValue>

ComponentViewer.Docs/Pages/Examples/ListExtendedExample2.razor

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
<MudText Class="ma-4">Enum List</MudText>
1313
<MudListExtended T="Continent" ItemCollection="(ICollection<Continent>)Enum.GetValues<Continent>()" Clickable="true" MaxItems="_maxItems" SearchBox="_searchBox" />
1414
</MudPaper>
15+
16+
<MudPaper Width="300px" Elevation="0">
17+
<MudText Class="ma-4">Complex Type List</MudText>
18+
<MudListExtended T="ComplexTypes" ItemCollection="_complexTypeCollection" Clickable="true" MaxItems="_maxItems" SearchBox="_searchBox"
19+
ToStringFunc="@(e => e?.Name + " " + e?.SurName)" />
20+
</MudPaper>
1521
</MudItem>
1622

1723
<MudItem xs="12" sm="4">
@@ -28,6 +34,13 @@
2834
int _maxItems = 8;
2935
bool _searchBox = false;
3036
string _selectedState;
37+
ICollection<ComplexTypes> _complexTypeCollection = new List<ComplexTypes>()
38+
{
39+
new ComplexTypes() { Name = "John", SurName = "Star" },
40+
new ComplexTypes() { Name = "David", SurName = "Codewriter" },
41+
new ComplexTypes() { Name = "Mary", SurName = "Hardcoded" },
42+
new ComplexTypes() { Name = "Jessica", SurName = "Fastfinger" },
43+
};
3144

3245
private string[] _states =
3346
{
@@ -56,4 +69,10 @@
5669
Australia,
5770
Antarctica
5871
}
72+
73+
public class ComplexTypes
74+
{
75+
public string Name { get; set; }
76+
public string SurName { get; set; }
77+
}
5978
}

0 commit comments

Comments
 (0)