Skip to content

Commit 42029ec

Browse files
authored
Code improvements (#308)
1 parent e919cdd commit 42029ec

2 files changed

Lines changed: 47 additions & 66 deletions

File tree

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

Lines changed: 39 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
using MudExtensions.Enums;
77
using MudExtensions.Extensions;
88
using MudExtensions.Services;
9-
using System.Text.RegularExpressions;
10-
using static MudBlazor.CategoryTypes;
9+
using System.Runtime.InteropServices;
1110

1211
namespace MudExtensions
1312
{
@@ -30,7 +29,7 @@ public MudComboBox()
3029
private string multiSelectionText;
3130
private IKeyInterceptor _keyInterceptor;
3231

33-
public List<MudComboBoxItem<T>> Items = new();
32+
public List<MudComboBoxItem<T>> Items { get; set; } = new();
3433
internal MudComboBoxItem<T> _lastActivatedItem;
3534
protected internal List<MudComboBoxItem<T>> EligibleItems { get; set; } = new();
3635
private MudInputExtended<string> _inputReference;
@@ -61,8 +60,8 @@ public MudComboBox()
6160
.AddStyle("display", "inline", Value != null || SelectedValues.Any())
6261
.Build();
6362

64-
private string _elementId = "combobox_" + Guid.NewGuid().ToString().Substring(0, 8);
65-
private string _popoverId = "comboboxpopover_" + Guid.NewGuid().ToString().Substring(0, 8);
63+
private string _elementId = string.Concat("combobox_", Guid.NewGuid().ToString().AsSpan(0, 8));
64+
private string _popoverId = string.Concat("comboboxpopover_", Guid.NewGuid().ToString().AsSpan(0, 8));
6665

6766
/// <summary>
6867
/// If true, combobox goes to autocomplete mode.
@@ -403,23 +402,12 @@ public MudComboBox()
403402
[Category(CategoryTypes.FormComponent.ListAppearance)]
404403
public string IndeterminateIcon { get; set; } = Icons.Material.Filled.IndeterminateCheckBox;
405404

406-
private bool _multiSelection = false;
407405
/// <summary>
408406
/// If true, multiple values can be selected via checkboxes which are automatically shown in the dropdown
409407
/// </summary>
410408
[Parameter]
411409
[Category(CategoryTypes.FormComponent.ListBehavior)]
412-
public bool MultiSelection
413-
{
414-
get => _multiSelection;
415-
set
416-
{
417-
if (value != _multiSelection)
418-
{
419-
_multiSelection = value;
420-
}
421-
}
422-
}
410+
public bool MultiSelection { get; set; }
423411

424412
[Parameter]
425413
[Category(CategoryTypes.FormComponent.ListBehavior)]
@@ -510,8 +498,7 @@ public IEnumerable<T> SelectedValues
510498
{
511499
get
512500
{
513-
if (_selectedValues == null)
514-
_selectedValues = new HashSet<T>(_comparer);
501+
_selectedValues ??= new HashSet<T>(_comparer);
515502
return _selectedValues;
516503
}
517504
set
@@ -598,12 +585,10 @@ protected Task UpdateDataVisualiserTextAsync()
598585
else
599586
{
600587
var item = Items.FirstOrDefault(x => Value == null ? x.Value == null : Comparer != null ? Comparer.Equals(Value, x.Value) : Value.Equals(x.Value));
601-
if (item == null)
602-
{
603-
_dataVisualiserText = Converter.Set(Value);
604-
return Task.CompletedTask;
605-
}
606-
_dataVisualiserText = (!string.IsNullOrEmpty(item.Text) ? item.Text : Converter.Set(item.Value));
588+
_dataVisualiserText = item is null
589+
? Converter.Set(Value)
590+
: (!string.IsNullOrEmpty(item.Text) ? item.Text : Converter.Set(item.Value));
591+
607592
return Task.CompletedTask;
608593
}
609594
}
@@ -1113,7 +1098,7 @@ public async Task BeginValidatePublic()
11131098
if (item == null)
11141099
return false;
11151100
bool? result = null;
1116-
if (Items?.Select(x => x.Value).Contains(item.Value) == false)
1101+
if (Items.Select(x => x.Value).Contains(item.Value) == false)
11171102
{
11181103
Items.Add(item);
11191104
if (MultiSelection == true && SelectedValues.Contains(item.Value))
@@ -1215,7 +1200,7 @@ protected async Task SelectAllItems()
12151200
if (_allSelected == null || _allSelected == false)
12161201
{
12171202
SelectedValues = new List<T>();
1218-
foreach (var item in Items.Where(x => x.Eligible == true))
1203+
foreach (var item in Items.Where(x => x.Eligible))
12191204
{
12201205
item.Selected = true;
12211206
SelectedValues = SelectedValues.Append(item.Value);
@@ -1226,7 +1211,7 @@ protected async Task SelectAllItems()
12261211
}
12271212
else
12281213
{
1229-
foreach (var item in Items)
1214+
foreach (var item in Items.Where(x => x.Selected))
12301215
{
12311216
item.Selected = false;
12321217
}
@@ -1264,19 +1249,19 @@ protected async Task ForceUpdateItems()
12641249

12651250
#region Active (Hilight)
12661251

1267-
protected int GetActiveItemIndex()
1268-
{
1269-
if (_lastActivatedItem == null)
1270-
{
1271-
var a = Items.FindIndex(x => x.Active == true);
1272-
return a;
1273-
}
1274-
else
1275-
{
1276-
var a = Items.FindIndex(x => _lastActivatedItem.Value == null ? x.Value == null : Comparer != null ? Comparer.Equals(_lastActivatedItem.Value, x.Value) : _lastActivatedItem.Value.Equals(x.Value));
1277-
return a;
1278-
}
1279-
}
1252+
//protected int GetActiveItemIndex()
1253+
//{
1254+
// if (_lastActivatedItem == null)
1255+
// {
1256+
// var a = Items.FindIndex(x => x.Active == true);
1257+
// return a;
1258+
// }
1259+
// else
1260+
// {
1261+
// var a = Items.FindIndex(x => _lastActivatedItem.Value == null ? x.Value == null : Comparer != null ? Comparer.Equals(_lastActivatedItem.Value, x.Value) : _lastActivatedItem.Value.Equals(x.Value));
1262+
// return a;
1263+
// }
1264+
//}
12801265

12811266
protected int GetActiveProperItemIndex()
12821267
{
@@ -1316,9 +1301,10 @@ protected internal void UpdateLastActivatedItem(T value)
13161301

13171302
protected void DeactiveAllItems()
13181303
{
1319-
foreach (var item in Items)
1304+
foreach (var item in CollectionsMarshal.AsSpan(Items))
13201305
{
1321-
item.SetActive(false);
1306+
if (item.Active)
1307+
item.SetActive(false);
13221308
}
13231309
}
13241310

@@ -1345,7 +1331,7 @@ public async Task ActiveFirstItem(string startChar = null)
13451331
}
13461332

13471333
// find first item that starts with the letter
1348-
var possibleItems = Items.Where(x => (x.Text ?? Converter.Set(x.Value) ?? "").StartsWith(startChar, StringComparison.OrdinalIgnoreCase)).ToList();
1334+
var possibleItems = Items.Where(x => (x.Text ?? Converter.Set(x.Value) ?? string.Empty).StartsWith(startChar, StringComparison.OrdinalIgnoreCase)).ToList();
13491335
if (possibleItems == null || !possibleItems.Any())
13501336
{
13511337
if (_lastActivatedItem == null)
@@ -1404,21 +1390,17 @@ public async Task ActiveAdjacentItem(int changeCount)
14041390

14051391
public async Task ActiveLastItem()
14061392
{
1407-
if (Items == null || Items.Count == 0)
1408-
{
1393+
if (!(Items.Count > 0))
14091394
return;
1410-
}
1395+
14111396
DeactiveAllItems();
14121397
var properItems = GetEligibleAndNonDisabledItems();
1413-
if (properItems.Any())
1414-
{
1415-
properItems.Last().SetActive(true);
1416-
_lastActivatedItem = properItems.Last();
1417-
}
1418-
else
1419-
_lastActivatedItem = null;
1398+
var lastItem = properItems.LastOrDefault();
1399+
lastItem?.SetActive(true);
1400+
_lastActivatedItem = lastItem;
14201401

1421-
await ScrollToMiddleAsync(_lastActivatedItem);
1402+
if (_lastActivatedItem is not null)
1403+
await ScrollToMiddleAsync(_lastActivatedItem);
14221404
}
14231405
#pragma warning restore BL0005
14241406

@@ -1450,15 +1432,7 @@ protected Typo GetTypo()
14501432
return Typo.body1;
14511433
}
14521434

1453-
protected internal ValueTask ScrollToMiddleAsync(MudComboBoxItem<T> item)
1454-
{
1455-
if (item == null)
1456-
{
1457-
return ValueTask.CompletedTask;
1458-
}
1459-
ScrollManagerExtended.ScrollToMiddleAsync(_popoverId, item.ItemId);
1460-
return ValueTask.CompletedTask;
1461-
}
1462-
1435+
protected internal ValueTask ScrollToMiddleAsync(MudComboBoxItem<T> item) =>
1436+
item is not null ? ScrollManagerExtended.ScrollToMiddleAsync(_popoverId, item.ItemId) : ValueTask.CompletedTask;
14631437
}
14641438
}

ComponentViewer.Docs/Pages/Examples/ComboboxExample1.razor

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
<MudItem xs="12" sm="4">
4848
<MudStack Spacing="4">
49-
<MudText>Item Count: @_combobox?.Items.Count()</MudText>
49+
<MudText>Item Count: @_combobox?.Items.Count</MudText>
5050
<MudText>Value: @_value</MudText>
5151
<MudText>Text: @_text</MudText>
5252
<MudText>Selected Values: @(string.Join(", ", _selectedValues ?? new List<string>()))</MudText>
@@ -102,4 +102,11 @@
102102
"Texas", "Utah", "Vermont", "Virgin Island", "Virginia",
103103
"Washington", "West Virginia", "Wisconsin", "Wyoming",
104104
};
105+
106+
protected override void OnAfterRender(bool firstRender)
107+
{
108+
base.OnAfterRender(firstRender);
109+
if (firstRender)
110+
StateHasChanged();
111+
}
105112
}

0 commit comments

Comments
 (0)