Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@
return;
_comparer = value;
// Apply comparer and refresh selected values
_selectedValues = new HashSet<T?>(_selectedValues, _comparer);

Check warning on line 581 in src/CodeBeam.MudBlazor.Extensions/Components/ComboBox/MudComboBox.razor.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'collection' in 'HashSet<T?>.HashSet(IEnumerable<T?> collection, IEqualityComparer<T?>? comparer)'.
SelectedValues = _selectedValues;
}
}
Expand Down Expand Up @@ -811,7 +811,7 @@
{
DeselectAllItems();
if (ReadValue is not null)
Items.FirstOrDefault(x => x.Value?.Equals(ReadValue) == true).Selected = true;

Check warning on line 814 in src/CodeBeam.MudBlazor.Extensions/Components/ComboBox/MudComboBox.razor.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}
}
if ((ReadValue == null && _oldValue != null) || (ReadValue != null && ReadValue.Equals(_oldValue) == false))
Expand Down Expand Up @@ -1062,7 +1062,7 @@
if (Strict)
{
// Check if the user-provided search string is an exact (case-insensitive) match against an item in the collection.
var item = Items.FirstOrDefault(x => ConvertSet(x.Value)?.Equals(_searchString, StringComparison.OrdinalIgnoreCase) == true);
var item = Items.FirstOrDefault(x => !x.Disabled && ConvertSet(x.Value)?.Equals(_searchString, StringComparison.OrdinalIgnoreCase) == true);
if (item is not null)
await ToggleOption(item, true);

Expand Down Expand Up @@ -1229,7 +1229,7 @@
/// <returns></returns>
protected internal async Task ToggleOption(MudComboBoxItem<T>? item, bool selected)
{
if (item == null)
if (item == null || item.Disabled)
{
return;
}
Expand All @@ -1246,7 +1246,7 @@
}
else if (MultiSelection == true && SelectedValues?.Contains(item.Value) == true)
{
SelectedValues = SelectedValues.Where(x => x.Equals(item.Value) == false);

Check warning on line 1249 in src/CodeBeam.MudBlazor.Extensions/Components/ComboBox/MudComboBox.razor.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
await SetValueAndUpdateTextAsync(SelectedValues.LastOrDefault(), false);
item.Selected = false;
_allSelected = GetAllSelectedState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ protected void SyncSelected()
/// <returns></returns>
protected async Task HandleOnClick()
{
if (Disabled)
return;

if (MudComboBox is not null)
{
await MudComboBox.ToggleOption(this, !Selected);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@namespace MudExtensions.UnitTests.TestComponents
<MudPopoverProvider></MudPopoverProvider>

<MudComboBox T="string" @bind-Value="value" Editable="@Editable" MultiSelection="@MultiSelection" ShowCheckbox="@ShowCheckbox" Bordered="@Bordered"
<MudComboBox T="string" @bind-Value="value" Editable="@Editable" Strict="@Strict" MultiSelection="@MultiSelection" ShowCheckbox="@ShowCheckbox" Bordered="@Bordered"
SearchFunc="@SearchFunc" HighlightClass="@HighlightClass" Highlight="@Highlight">
<MudComboBoxItem Value="@("apple")" Text="Apple" OnClick="@(() => ClickCount++)" Class="item-apple" />
<MudComboBoxItem Value="@("banana")" Class="item-banana" Ripple="false" />
<MudComboBoxItem Value="@("cherry")" Text="Cherry" Disabled="true" Class="item-cherry" />
<MudComboBoxItem Value="@("cherry")" Text="Cherry" Disabled="true" OnClick="@(() => ClickCount++)" Class="item-cherry" />
<MudComboBoxItem Value="@("date")" Class="item-date" TextColor="Color.Error">
<span class="item-child-marker">date content</span>
</MudComboBoxItem>
Expand All @@ -18,6 +18,8 @@

[Parameter] public bool Editable { get; set; }

[Parameter] public bool Strict { get; set; }

[Parameter] public bool MultiSelection { get; set; }

[Parameter] public bool ShowCheckbox { get; set; } = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Bunit;
using MudExtensions.UnitTests.TestComponents;
using AwesomeAssertions;
using Microsoft.AspNetCore.Components.Web;
using MudBlazor;
using MudExtensions.UnitTests.Extensions;

Expand Down Expand Up @@ -81,6 +82,48 @@ public void ComboBoxItem_ClickShouldSetParentValue()
comp.WaitForAssertion(() => comp.FindComponent<MudComboBox<string>>().Instance.Value.Should().Be("banana"));
}

/// <summary>Clicking a disabled item does not invoke its OnClick callback.</summary>
[Test]
public void ComboBoxItem_DisabledClickShouldNotInvokeOnClick()
{
var comp = Context.Render<ComboBoxItemTest>();
OpenMenu(comp);

comp.Find("div.item-cherry").Click();

comp.Instance.ClickCount.Should().Be(0);
}

/// <summary>In multi selection, clicking a disabled item does not select it.</summary>
[Test]
public void ComboBoxItem_MultiSelectionDisabledClickShouldNotSelect()
{
var comp = Context.Render<ComboBoxItemTest>(p => p.Add(x => x.MultiSelection, true));
OpenMenu(comp);

comp.Find("div.item-cherry").Click();

comp.FindComponent<MudComboBox<string>>().Instance.SelectedValues.Should().BeEmpty();
}

/// <summary>In strict mode, blurring with a disabled item's text does not select it and clears the text.</summary>
[Test]
public async Task ComboBoxItem_StrictBlurShouldNotSelectDisabledItem()
{
var comp = Context.Render<ComboBoxItemTest>(p =>
{
p.Add(x => x.Editable, true);
p.Add(x => x.Strict, true);
});
var combobox = comp.FindComponent<MudComboBox<string>>();
await FilterAsync(comp, "Cherry");

await comp.InvokeAsync(() => combobox.Instance.HandleOnBlur(new FocusEventArgs()));

combobox.Instance.Value.Should().BeNull();
combobox.Instance.GetSearchString().Should().BeNullOrEmpty();
}

/// <summary>The Disabled
[Test]
public void ComboBoxItem_DisabledShouldHaveDisabledClass()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ public void ComboBox_CustomClassShouldBeApplied()

/// <summary>Clicking a disabled item does not select it.</summary>
[Test]
[Ignore("Known bug: MudComboBoxItem.HandleOnClick does not check Disabled, so disabled items can be selected.")]
public void ComboBox_DisabledItemShouldNotBeSelectable()
{
var comp = Context.Render<ComboBoxFeatureTest>();
Expand All @@ -336,6 +335,19 @@ public void ComboBox_DisabledItemShouldNotBeSelectable()
combobox.Instance.GetState(x => x.Value).Should().BeNullOrEmpty();
}

/// <summary>ToggleOption does not select a disabled item.</summary>
[Test]
public async Task ComboBox_ToggleOptionShouldIgnoreDisabledItem()
{
var comp = Context.Render<ComboBoxFeatureTest>();
var combobox = comp.FindComponent<MudComboBox<string>>();
var disabledItem = combobox.Instance.Items.Single(x => x.Disabled);

await comp.InvokeAsync(() => combobox.Instance.ToggleOption(disabledItem, true));

combobox.Instance.GetState(x => x.Value).Should().BeNullOrEmpty();
}

/// <summary>Disabled items are excluded from the enabled and eligible item list.</summary>
[Test]
public void ComboBox_DisabledItemShouldNotBeEnabledAndEligible()
Expand Down
Loading