Skip to content

Commit 7ba6256

Browse files
authored
MudListExtended & MudSelectExtended: SearchBox (#105)
* Initialize * Finalize * Last Changes * Add ClassSearchBox
1 parent 675bc83 commit 7ba6256

File tree

15 files changed

+127
-16
lines changed

15 files changed

+127
-16
lines changed

CodeBeam.MudExtensions/Components/ListExtended/MudListExtended.razor

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,20 @@
1111
<MudListItemExtended T="T" IsFunctional="true" Icon="@SelectAllCheckBoxIcon" IconColor="@Color" Text="@SelectAllText" OverrideMultiSelectionComponent="MultiSelectionComponent.None" OnClick="@(() => SelectAllItems(_allSelected))" OnClickHandlerPreventDefault="true" Dense="@Dense" Class="mb-2" />
1212
<MudDivider />
1313
}
14+
1415
@if (ItemCollection != null)
1516
{
16-
<MudVirtualize IsEnabled="@Virtualize" Items="ItemCollection" Context="item" OverscanCount="@OverscanCount">
17+
@if (SearchBox == true)
18+
{
19+
<MudListSubheaderExtended T="T" Style="position: sticky; top: -12px; background-color: var(--mud-palette-background); z-index: 10">
20+
<div @onkeydown:stopPropagation>
21+
<MudTextField @bind-Value="@_searchString" Class="@ClassSearchBox" OnKeyUp="@(() => UpdateSelectedStyles())" Immediate="true" Variant="Variant.Outlined" Margin="Margin.Dense"
22+
Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Filled.Search" IconColor="Color" />
23+
</div>
24+
25+
</MudListSubheaderExtended>
26+
}
27+
<MudVirtualize IsEnabled="@Virtualize" Items="GetSearchedItems()" Context="item" OverscanCount="@OverscanCount">
1728
@if (MudSelectExtended != null)
1829
{
1930
<MudSelectItemExtended Value="@item" Text="@(MudSelectExtended.ToStringFunc == null ? item.ToString() : MudSelectExtended.ToStringFunc(item))" />
@@ -22,8 +33,6 @@
2233
{
2334
<MudListItemExtended Value="@item" Text="@item.ToString()" />
2435
}
25-
@*Upper commented block will replace after Experimental Select*@
26-
@*<MudListItemExtended Value="@item" Text="@item.ToString()" />*@
2736
</MudVirtualize>
2837
}
2938
else

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using Microsoft.AspNetCore.Components;
@@ -9,6 +10,7 @@
910
using MudBlazor.Utilities;
1011
using MudExtensions.Enums;
1112
using MudExtensions.Services;
13+
using static MudBlazor.CategoryTypes;
1214

1315
namespace MudExtensions
1416
{
@@ -26,6 +28,7 @@ public partial class MudListExtended<T> : MudComponentBase, IDisposable
2628
private List<MudListExtended<T>> _childLists = new();
2729
internal MudListItemExtended<T> _lastActivatedItem;
2830
internal bool? _allSelected = false;
31+
private string _searchString;
2932

3033
protected string Classname =>
3134
new CssBuilder("mud-list-extended")
@@ -111,6 +114,20 @@ public IEqualityComparer<T> Comparer
111114
[Category(CategoryTypes.List.Behavior)]
112115
public ICollection<T> ItemCollection { get; set; } = null;
113116

117+
/// <summary>
118+
/// If true, shows a searchbox for filtering items. Only works with ItemCollection approach.
119+
/// </summary>
120+
[Parameter]
121+
[Category(CategoryTypes.List.Behavior)]
122+
public bool SearchBox { get; set; }
123+
124+
/// <summary>
125+
/// SearchBox's CSS classes, seperated by space.
126+
/// </summary>
127+
[Parameter]
128+
[Category(CategoryTypes.List.Behavior)]
129+
public string ClassSearchBox { get; set; }
130+
114131
/// <summary>
115132
/// Allows virtualization. Only work if ItemCollection parameter is not null.
116133
/// </summary>
@@ -663,6 +680,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
663680
}
664681
_firstRendered = true;
665682
}
683+
666684
_centralCommanderResultRendered = true;
667685
}
668686

@@ -846,7 +864,7 @@ protected internal void SetSelectedValue(MudListItemExtended<T> item, bool force
846864
_lastActivatedItem = item;
847865
}
848866

849-
protected internal void UpdateSelectedStyles(bool deselectFirst = true)
867+
protected internal void UpdateSelectedStyles(bool deselectFirst = true, bool update = true)
850868
{
851869
var items = CollectAllMudListItems(true);
852870
if (deselectFirst)
@@ -868,7 +886,10 @@ protected internal void UpdateSelectedStyles(bool deselectFirst = true)
868886
items.Where(x => SelectedValues.Contains(x.Value, Comparer == null ? null : Comparer)).ToList().ForEach(x => x.SetSelected(true));
869887
}
870888

871-
StateHasChanged();
889+
if (update == true)
890+
{
891+
StateHasChanged();
892+
}
872893
}
873894

874895
protected bool IsSelectable()
@@ -1214,7 +1235,7 @@ public async Task ActiveLastItem()
12141235
#endregion
12151236

12161237

1217-
#region Others (Clear, Scroll)
1238+
#region Others (Clear, Scroll, Search)
12181239

12191240
/// <summary>
12201241
/// Clears value(s) and item(s) and deactive all items.
@@ -1239,6 +1260,16 @@ public void Clear()
12391260
protected internal ValueTask ScrollToMiddleAsync(MudListItemExtended<T> item)
12401261
=> ScrollManagerExtended.ScrollToMiddleAsync(_elementId, item.ItemId);
12411262

1263+
protected ICollection<T> GetSearchedItems()
1264+
{
1265+
if (SearchBox == false || ItemCollection == null || _searchString == null)
1266+
{
1267+
return ItemCollection;
1268+
}
1269+
1270+
return ItemCollection.Where(x => Converter.Set(x).Contains(_searchString, StringComparison.CurrentCultureIgnoreCase)).ToList();
1271+
}
1272+
12421273
#endregion
12431274
}
12441275
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ public partial class MudListItemExtended<T> : MudComponentBase, IDisposable
2424
.AddClass("mud-list-item-dense-extended", Dense == true || MudListExtended?.Dense == true)
2525
.AddClass("mud-list-item-gutters-extended", !DisableGutters && !(MudListExtended?.DisableGutters == true))
2626
.AddClass("mud-list-item-clickable-extended", MudListExtended?.Clickable)
27-
.AddClass("mud-ripple", MudListExtended?.Clickable == true && !DisableRipple && !Disabled)
27+
.AddClass("mud-ripple", MudListExtended?.Clickable == true && !DisableRipple && !Disabled && !IsFunctional)
2828
.AddClass($"mud-selected-item mud-{MudListExtended?.Color.ToDescriptionString()}-text mud-{MudListExtended?.Color.ToDescriptionString()}-hover", _selected && !Disabled && NestedList == null && !MudListExtended.DisableSelectedItemStyle)
2929
.AddClass("mud-list-item-hilight-extended", _active && !Disabled && NestedList == null && IsFunctional == false)
3030
.AddClass("mud-list-item-disabled-extended", Disabled)
3131
.AddClass("mud-list-item-nested-background-extended", MudListExtended != null && MudListExtended.SecondaryBackgroundForNestedItemHeader && NestedList != null)
32+
.AddClass("mud-list-item-functional", IsFunctional)
3233
.AddClass(Class)
3334
.Build();
3435

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">
103+
ItemTemplate="@ItemTemplate" ItemSelectedTemplate="@ItemSelectedTemplate" ItemDisabledTemplate="@ItemDisabledTemplate" SearchBox="@SearchBox">
104104
@ChildContent
105105
</MudListExtended>
106106
</CascadingValue>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,13 @@ public bool Dense
296296
[Category(CategoryTypes.FormComponent.Behavior)]
297297
public bool Clearable { get; set; } = false;
298298

299+
/// <summary>
300+
/// If true, shows a searchbox for filtering items. Only works with ItemCollection approach.
301+
/// </summary>
302+
[Parameter]
303+
[Category(CategoryTypes.List.Behavior)]
304+
public bool SearchBox { get; set; }
305+
299306
/// <summary>
300307
/// If true, prevent scrolling while dropdown is open.
301308
/// </summary>

CodeBeam.MudExtensions/Styles/Components/_listextended.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ $mud-palette-colors: primary, secondary, tertiary, info, success, warning, error
5656
-webkit-tap-highlight-color: transparent;
5757
transition: background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
5858

59-
&:hover {
59+
&:hover:not(.mud-list-item-functional) {
6060
background-color: var(--mud-palette-action-default-hover);
6161
}
6262
}

CodeBeam.MudExtensions/Styles/MudExtensions.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@
537537
-webkit-appearance: none;
538538
-webkit-tap-highlight-color: transparent;
539539
transition: background-color 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms; }
540-
.mud-list-item-clickable-extended:hover {
540+
.mud-list-item-clickable-extended:hover:not(.mud-list-item-functional) {
541541
background-color: var(--mud-palette-action-default-hover); }
542542

543543
.mud-list-item-gutters-extended {

CodeBeam.MudExtensions/Styles/MudExtensions.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodeBeam.MudExtensions/wwwroot/MudExtensions.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ComponentViewer.Docs/Pages/Components/ListExtendedPage.razor

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

13-
<ExampleCard ExampleName="ListExtendedExample2" Title="ItemCollection" Description="Extended list can also populate with ItemCollection parameter.">
13+
<ExampleCard ExampleName="ListExtendedExample2" Title="ItemCollection and SearchBox" Description="Extended list can also populate with ItemCollection parameter.">
1414
<ListExtendedExample2 />
1515
</ExampleCard>
1616

0 commit comments

Comments
 (0)