Skip to content

Commit 0333554

Browse files
authored
Select SearhBox Enhancements (#144)
1 parent dc7e7bf commit 0333554

File tree

7 files changed

+64
-6
lines changed

7 files changed

+64
-6
lines changed

CodeBeam.MudExtensions/Components/ListExtended/MudListExtended.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
{
3333
<MudCheckBox CheckedIcon="@SelectAllCheckBoxIcon" Color="@Color" @bind-Checked="_allSelected" @onclick="() => SelectAllItems(_allSelected)" Dense="true" />
3434
}
35-
<MudTextField @bind-Value="@_searchString" Class="@ClassSearchBox" OnKeyUp="@(() => UpdateSelectedStyles())" Immediate="true" Variant="Variant.Outlined" Margin="Margin.Dense"
35+
<MudTextField @bind-Value="@_searchString" Class="@ClassSearchBox" Placeholder="@SearchBoxPlaceholder" OnKeyUp="@(() => UpdateSelectedStyles())" Immediate="true" Variant="Variant.Outlined" Margin="Margin.Dense"
3636
Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Filled.Search" AdornmentColor="Color" AutoFocus="@SearchBoxAutoFocus" />
3737
</div>
3838
</MudListSubheaderExtended>

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ public Func<T, string> ToStringFunc
143143
[Category(CategoryTypes.List.Behavior)]
144144
public ICollection<T> ItemCollection { get; set; } = null;
145145

146+
/// <summary>
147+
/// Custom search func for searchbox. If doesn't set, it search with "Contains" logic by default. Passed value and searchString values.
148+
/// </summary>
149+
[Parameter]
150+
[Category(CategoryTypes.FormComponent.ListBehavior)]
151+
public Func<T, string, bool> SearchFunc { get; set; }
152+
146153
/// <summary>
147154
/// If true, shows a searchbox for filtering items. Only works with ItemCollection approach.
148155
/// </summary>
@@ -164,6 +171,10 @@ public Func<T, string> ToStringFunc
164171
[Category(CategoryTypes.List.Behavior)]
165172
public string ClassSearchBox { get; set; }
166173

174+
[Parameter]
175+
[Category(CategoryTypes.List.Behavior)]
176+
public string SearchBoxPlaceholder { get; set; }
177+
167178
/// <summary>
168179
/// Allows virtualization. Only work if ItemCollection parameter is not null.
169180
/// </summary>
@@ -1323,6 +1334,11 @@ protected ICollection<T> GetSearchedItems()
13231334
return ItemCollection;
13241335
}
13251336

1337+
if (SearchFunc != null)
1338+
{
1339+
return ItemCollection.Where(x => SearchFunc.Invoke(x, _searchString)).ToList();
1340+
}
1341+
13261342
return ItemCollection.Where(x => Converter.Set(x).Contains(_searchString, StringComparison.CurrentCultureIgnoreCase)).ToList();
13271343
}
13281344

CodeBeam.MudExtensions/Components/SelectExtended/MudSelectExtended.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
Clickable="true" Color="@Color" Dense="@Dense" ItemCollection="@ItemCollection" Virtualize="@Virtualize" DisablePadding="@DisablePopoverPadding" DisableSelectedItemStyle="@DisableSelectedItemStyle"
112112
MultiSelection="@MultiSelection" MultiSelectionComponent="@MultiSelectionComponent" MultiSelectionAlign="@MultiSelectionAlign" SelectAll="@SelectAll" SelectAllPosition="@SelectAllPosition" SelectAllText="@SelectAllText"
113113
CheckedIcon="@CheckedIcon" UncheckedIcon="@UncheckedIcon" IndeterminateIcon="@IndeterminateIcon" SelectValueOnTab="@SelectValueOnTab" Comparer="@Comparer"
114-
ItemTemplate="@ItemTemplate" ItemSelectedTemplate="@ItemSelectedTemplate" ItemDisabledTemplate="@ItemDisabledTemplate" SearchBox="@SearchBox" SearchBoxAutoFocus="@SearchBoxAutoFocus" ToStringFunc="@ToStringFunc">
114+
ItemTemplate="@ItemTemplate" ItemSelectedTemplate="@ItemSelectedTemplate" ItemDisabledTemplate="@ItemDisabledTemplate" SearchBox="@SearchBox" SearchBoxAutoFocus="@SearchBoxAutoFocus" SearchFunc="@SearchFunc" SearchBoxPlaceholder="@SearchBoxPlaceholder" ToStringFunc="@ToStringFunc">
115115
@ChildContent
116116
</MudListExtended>
117117
</CascadingValue>

CodeBeam.MudExtensions/Components/SelectExtended/MudSelectExtended.razor.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ public MudSelectExtended()
150150
[Category(CategoryTypes.List.Appearance)]
151151
public bool DisableSelectedItemStyle { get; set; }
152152

153+
[Parameter]
154+
[Category(CategoryTypes.List.Behavior)]
155+
public string SearchBoxPlaceholder { get; set; }
156+
153157
/// <summary>
154158
/// If true, compact vertical padding will be applied to all Select items.
155159
/// </summary>
@@ -217,6 +221,13 @@ public bool Dense
217221
[Category(CategoryTypes.FormComponent.Behavior)]
218222
public Func<List<T>, string> MultiSelectionTextFunc { get; set; }
219223

224+
/// <summary>
225+
/// Custom search func for searchbox. If doesn't set, it search with "Contains" logic by default. Passed value and searchString values.
226+
/// </summary>
227+
[Parameter]
228+
[Category(CategoryTypes.FormComponent.ListBehavior)]
229+
public Func<T, string, bool> SearchFunc { get; set; }
230+
220231
/// <summary>
221232
/// If not null, select items will automatically created regard to the collection.
222233
/// </summary>

ComponentViewer.Docs/Pages/Components/SelectExtendedPage.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<SelectExtendedExample1 />
1111
</ExampleCard>
1212

13-
<ExampleCard ExampleName="SelectExtendedExample6" Title="SearchBox" Description="SearchBox can be added if items populated with ItemCollection parameter. Ctrl+Shift+F combination shows/hides the searchbox if the box not focused.">
13+
<ExampleCard ExampleName="SelectExtendedExample6" Title="SearchBox" Description="SearchBox can be added if items populated with ItemCollection parameter. Ctrl+Shift+F combination shows/hides the searchbox if the box not focused. Custom search funcs also supported.">
1414
<SelectExtendedExample6 />
1515
</ExampleCard>
1616

ComponentViewer.Docs/Pages/Examples/ListExtendedExample2.razor

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<MudItem xs="12" sm="8" Class="d-flex gap-8 align-top justify-center" Style="height: 500px">
66
<MudPaper Width="300px" Elevation="0">
77
<MudText Class="ma-4">Array List</MudText>
8-
<MudListExtended ItemCollection="_states" Clickable="true" MaxItems="_maxItems" SearchBox="_searchBox" @bind-SelectedValue="_selectedState" />
8+
<MudListExtended ItemCollection="_states" Clickable="true" MaxItems="_maxItems" SearchBox="_searchBox" SearchFunc="@(new Func<string, string, bool>(SearchItems))" @bind-SelectedValue="_selectedState" />
99
</MudPaper>
1010

1111
<MudPaper Width="300px" Elevation="0">
@@ -60,6 +60,21 @@
6060
"Washington", "West Virginia", "Wisconsin", "Wyoming",
6161
};
6262

63+
private bool SearchItems(string value, string searchString)
64+
{
65+
if (searchString == "")
66+
{
67+
return true;
68+
}
69+
70+
if (value.StartsWith(searchString, StringComparison.CurrentCultureIgnoreCase))
71+
{
72+
return true;
73+
}
74+
75+
return false;
76+
}
77+
6378
public enum Continent
6479
{
6580
Europe,

ComponentViewer.Docs/Pages/Examples/SelectExtendedExample6.razor

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
<MudGrid>
3-
<MudItem xs="12" sm="8">
4-
<MudSelectExtended MultiSelection="@_multiselection" ItemCollection="_states" SearchBox="true" SearchBoxAutoFocus="@_searchBoxAutoFocus" T="string" Label="US States" AnchorOrigin="Origin.BottomCenter" Variant="Variant.Outlined" />
3+
<MudItem xs="12" sm="8" Class="d-flex gap-4">
4+
<MudSelectExtended MultiSelection="@_multiselection" ItemCollection="_states" SearchBox="true" SearchBoxAutoFocus="@_searchBoxAutoFocus" T="string" Label="Standard Search" AnchorOrigin="Origin.BottomCenter" Variant="Variant.Outlined" HelperText="Search with 'Contains' logic" />
5+
<MudSelectExtended MultiSelection="@_multiselection" ItemCollection="_states" SearchBox="true" SearchBoxAutoFocus="@_searchBoxAutoFocus" SearchFunc="@(new Func<string, string, bool>(SearchItems))" T="string" Label="Custom Search Func" AnchorOrigin="Origin.BottomCenter" Variant="Variant.Outlined" SearchBoxPlaceholder="Some Placeholder" HelperText="Search with 'StartsWith' logic" />
56
</MudItem>
67

78
<MudItem xs="12" sm="4">
@@ -34,4 +35,19 @@
3435
"Washington", "West Virginia", "Wisconsin", "Wyoming",
3536
};
3637

38+
private bool SearchItems(string value, string searchString)
39+
{
40+
if (searchString == "")
41+
{
42+
return true;
43+
}
44+
45+
if (value.StartsWith(searchString, StringComparison.CurrentCultureIgnoreCase))
46+
{
47+
return true;
48+
}
49+
50+
return false;
51+
}
52+
3753
}

0 commit comments

Comments
 (0)