Skip to content

Commit f8039bf

Browse files
authored
Code tweaks (#311)
Added IDisposable to MudComboBox
1 parent c5b0843 commit f8039bf

4 files changed

Lines changed: 40 additions & 42 deletions

File tree

CodeBeam.MudBlazor.Extensions/Components/ComboBox/MudComboBox.razor

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,4 @@
157157

158158
</CascadingValue>
159159
<!-- mousedown instead of click needed to close the menu before OnLostFocus runs -->
160-
<MudOverlay Visible="_isOpen" @onmousedown="@(() => CloseMenu())" LockScroll="@LockScroll" />
161-
162-
160+
<MudOverlay Visible="@_isOpen" @onmousedown="@CloseMenu" LockScroll="@LockScroll" />

CodeBeam.MudBlazor.Extensions/Components/ComboBox/MudComboBox.razor.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace MudExtensions
1212
{
13-
public partial class MudComboBox<T> : MudBaseInputExtended<T>
13+
public partial class MudComboBox<T> : MudBaseInputExtended<T>, IDisposable
1414
{
1515
#region Constructor, Injected Services, Parameters, Fields
1616

@@ -31,7 +31,7 @@ protected internal void SetSearchString(T value)
3131
}
3232

3333
internal string _searchString { get; set; }
34-
private string multiSelectionText;
34+
private readonly string multiSelectionText;
3535
private IKeyInterceptor _keyInterceptor;
3636

3737
public List<MudComboBoxItem<T>> Items { get; set; } = new();
@@ -65,8 +65,8 @@ protected internal void SetSearchString(T value)
6565
.AddStyle("display", "inline", Value != null || SelectedValues.Any())
6666
.Build();
6767

68-
private string _elementId = string.Concat("combobox_", Guid.NewGuid().ToString().AsSpan(0, 8));
69-
private string _popoverId = string.Concat("comboboxpopover_", Guid.NewGuid().ToString().AsSpan(0, 8));
68+
private readonly string _elementId = string.Concat("combobox_", Guid.NewGuid().ToString().AsSpan(0, 8));
69+
private readonly string _popoverId = string.Concat("comboboxpopover_", Guid.NewGuid().ToString().AsSpan(0, 8));
7070

7171
/// <summary>
7272
/// If true, combobox goes to autocomplete mode.
@@ -535,7 +535,7 @@ public IEnumerable<T> SelectedValues
535535

536536
protected Task UpdateDataVisualiserTextAsync()
537537
{
538-
List<string> textList = new List<string>();
538+
var textList = new List<string>();
539539
if (Items != null && Items.Any())
540540
{
541541
if (false) // ItemCollection != null
@@ -1032,7 +1032,7 @@ protected internal async Task ToggleOption(MudComboBoxItem<T> item, bool selecte
10321032
{
10331033
if (ToggleSelection)
10341034
{
1035-
await UpdateComboBoxValueAsync(default(T), updateText: true, updateSearchString: true);
1035+
await UpdateComboBoxValueAsync(default, updateText: true, updateSearchString: true);
10361036
item.Selected = false;
10371037
}
10381038
}
@@ -1115,14 +1115,7 @@ public async Task BeginValidatePublic()
11151115
return result;
11161116
}
11171117

1118-
protected internal void Remove(MudComboBoxItem<T> item)
1119-
{
1120-
if (Items == null)
1121-
{
1122-
return;
1123-
}
1124-
Items.Remove(item);
1125-
}
1118+
protected internal void RemoveItem(MudComboBoxItem<T> item) => Items.Remove(item);
11261119

11271120
#endregion
11281121

@@ -1134,7 +1127,7 @@ protected internal void Remove(MudComboBoxItem<T> item)
11341127
/// </summary>
11351128
protected async ValueTask ClearButtonClickHandlerAsync(MouseEventArgs e)
11361129
{
1137-
await UpdateComboBoxValueAsync(default(T));
1130+
await UpdateComboBoxValueAsync(default);
11381131
_searchString = null;
11391132
await SetTextAsync(default, false);
11401133
_selectedValues.Clear();
@@ -1313,7 +1306,6 @@ protected void DeactiveAllItems()
13131306
}
13141307
}
13151308

1316-
#pragma warning disable BL0005
13171309
public async Task ActiveFirstItem(string startChar = null)
13181310
{
13191311
if (Items == null || Items.Count == 0 || Items[0].Disabled)
@@ -1407,7 +1399,6 @@ public async Task ActiveLastItem()
14071399
if (_lastActivatedItem is not null)
14081400
await ScrollToMiddleAsync(_lastActivatedItem);
14091401
}
1410-
#pragma warning restore BL0005
14111402

14121403
#endregion
14131404

@@ -1439,5 +1430,11 @@ protected Typo GetTypo()
14391430

14401431
protected internal ValueTask ScrollToMiddleAsync(MudComboBoxItem<T> item) =>
14411432
item is not null ? ScrollManagerExtended.ScrollToMiddleAsync(_popoverId, item.ItemId) : ValueTask.CompletedTask;
1433+
1434+
1435+
public void Dispose()
1436+
{
1437+
Items.Clear();
1438+
}
14421439
}
14431440
}

CodeBeam.MudBlazor.Extensions/Components/ComboBox/MudComboBoxItem.razor

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33
@inherits MudBaseSelectItem
44
@typeparam T
55

6-
<div tabindex="0" @attributes="UserAttributes" id="@ItemId" class="@Classname" @onclick="HandleOnClick" @onclick:stopPropagation="true" style="@Style">
7-
8-
@if (MudComboBox != null && MudComboBox.MultiSelection == true && MudComboBox.ShowCheckbox == true)
9-
{
10-
<MudCheckBox Class="me-4" Color="@MudComboBox.Color" Disabled="@Disabled" @bind-Checked="Selected" @onclick="HandleOnClick" Dense="true" />
11-
}
6+
@if (MudComboBox is not null)
7+
{
8+
<div tabindex="0" @attributes="@UserAttributes" id="@ItemId" class="@Classname" @onclick="@HandleOnClick" @onclick:stopPropagation="true" style="@Style">
129

13-
<MudText Typo="GetTypo()">
14-
@if (ChildContent != null && MudComboBox?.ItemPresenter == ValuePresenter.ItemContent)
10+
@if (MudComboBox.MultiSelection && MudComboBox.ShowCheckbox)
1511
{
16-
@ChildContent
12+
<MudCheckBox Class="me-4" Color="@MudComboBox.Color" Disabled="@Disabled" @bind-Checked="@Selected" @onclick="@HandleOnClick" Dense="true" />
1713
}
18-
else
19-
{
20-
<MudHighlighter Text="@DisplayString" HighlightedText="@(MudComboBox.Highlight == false ? null : MudComboBox?.GetSearchString())" Class="@HighlighterClassname" />
21-
}
22-
</MudText>
23-
</div>
14+
15+
<MudText Typo="GetTypo()">
16+
@if (ChildContent != null && MudComboBox.ItemPresenter == ValuePresenter.ItemContent)
17+
{
18+
@ChildContent
19+
}
20+
else
21+
{
22+
<MudHighlighter Text="@DisplayString" HighlightedText="@(MudComboBox.Highlight ? MudComboBox.GetSearchString() : null)" Class="@HighlighterClassname" />
23+
}
24+
</MudText>
25+
</div>
26+
}

CodeBeam.MudBlazor.Extensions/Components/ComboBox/MudComboBoxItem.razor.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public partial class MudComboBoxItem<T> : MudBaseSelectItem, IDisposable
2828
.AddClass(MudComboBox?.HighlightClass)
2929
.Build();
3030

31-
internal string ItemId { get; } = "_" + Guid.NewGuid().ToString().Substring(0, 8);
31+
internal string ItemId { get; } = string.Concat("_", Guid.NewGuid().ToString().AsSpan(0, 8));
3232

3333
/// <summary>
3434
/// The parent select component
@@ -119,11 +119,11 @@ protected override void OnInitialized()
119119
MudComboBox?.Add(this);
120120
}
121121

122-
bool? _oldMultiselection;
123-
bool? _oldSelected;
122+
//bool? _oldMultiselection;
123+
//bool? _oldSelected;
124124
bool _selectedChanged = false;
125-
bool? _oldEligible = true;
126-
bool _eligibleChanged = false;
125+
//bool? _oldEligible = true;
126+
//bool _eligibleChanged = false;
127127
protected override void OnParametersSet()
128128
{
129129
base.OnParametersSet();
@@ -238,9 +238,9 @@ public void Dispose()
238238
{
239239
try
240240
{
241-
MudComboBox?.Remove(this);
241+
MudComboBox?.RemoveItem(this);
242242
}
243-
catch (Exception) { }
243+
catch { }
244244
}
245245
}
246246
}

0 commit comments

Comments
 (0)