Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use IEqualityComparer<TOption> instead of Func<TOption, TOption, bool>
  • Loading branch information
MarvinKlein1508 committed Jun 5, 2025
commit 72fc933e4f393d77172fa84adfa2445ce1269b15
Original file line number Diff line number Diff line change
Expand Up @@ -6408,7 +6408,6 @@
<member name="P:Microsoft.FluentUI.AspNetCore.Components.ListComponentBase`1.OptionComparer">
<summary>
Gets or sets the function used to determine if an option is already added to the internal list.
The first parameter is the item from the internal list to check and the second parameter is the actual selected item from the menu.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.ListComponentBase`1.Items">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Class="w-100"
Placeholder="Name"
OnOptionsSearch="@OnSearchUserAsync"
OptionComparer="IsOptionAdded"
OptionComparer="MyComparer.Instance"
OptionText="@(item => $"{item.Firstname} {item.Lastname}" )"
@bind-SelectedOptions="Users2" />
</div>
Expand All @@ -26,12 +26,6 @@
public IEnumerable<SimplePerson> Users1 { get; set; } = [new SimplePerson { Firstname = "Marvin", Lastname = "Klein", Age = 28 }];
public IEnumerable<SimplePerson> Users2 { get; set; } = [new SimplePerson { Firstname = "Marvin", Lastname = "Klein", Age = 28 }];

private bool IsOptionAdded(SimplePerson listItem, SimplePerson selectedItem)
{
// Compare both objects with each other to determine if they are equal
return listItem.Firstname == selectedItem.Firstname && listItem.Lastname == selectedItem.Lastname;
}

private Task OnSearchUserAsync(OptionsSearchEventArgs<SimplePerson> e)
{
// Simulate new instances for every search. Typically you would retrieve these from a database or an API.
Expand All @@ -46,4 +40,28 @@

return Task.CompletedTask;
}

public class MyComparer : IEqualityComparer<SimplePerson>
{
public static readonly MyComparer Instance = new();

public bool Equals(SimplePerson? x, SimplePerson? y)
{
if (ReferenceEquals(x, y))
{
return true;
}

if (x is null || y is null)
{
return false;
}

return x.Firstname == y.Firstname &&
x.Lastname == y.Lastname &&
x.Age == y.Age;
}

public int GetHashCode(SimplePerson obj) => HashCode.Combine(obj.Firstname, obj.Lastname, obj.Age);
}
}
5 changes: 2 additions & 3 deletions src/Core/Components/List/ListComponentBase.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,9 @@ protected string? InternalValue
public virtual Func<TOption, bool>? OptionSelected { get; set; }
/// <summary>
/// Gets or sets the function used to determine if an option is already added to the internal list.
/// The first parameter is the item from the internal list to check and the second parameter is the actual selected item from the menu.
/// </summary>
[Parameter]
public virtual Func<TOption, TOption, bool>? OptionComparer { get; set; }
public virtual IEqualityComparer<TOption>? OptionComparer { get; set; }

/// <summary>
/// Gets or sets the content source of all items to display in this list.
Expand Down Expand Up @@ -544,7 +543,7 @@ protected virtual async Task OnSelectedItemChangedHandlerAsync(TOption? item)
RemoveSelectedItem(item);
await RaiseChangedEventsAsync();
}
else if (OptionComparer is not null && _selectedOptions.FirstOrDefault(x => OptionComparer(x, item)) is TOption addedItem)
else if (OptionComparer is not null && _selectedOptions.FirstOrDefault(x => OptionComparer.Equals(x, item)) is TOption addedItem)
{
RemoveSelectedItem(addedItem);
await RaiseChangedEventsAsync();
Expand Down
24 changes: 21 additions & 3 deletions tests/Core/List/FluentAutocompleteTests.razor
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@
var cut = Render<FluentAutocomplete<Customer>>(
@<FluentAutocomplete TOption="Customer"
SelectValueOnTab="true"
OptionComparer="IsItemAdded"
OptionComparer="CustomerComparer.Instance"
@bind-SelectedOptions="@SelectedItems"
OnOptionsSearch="@OnSearchNewInstance" />
);
Expand Down Expand Up @@ -551,8 +551,26 @@
return Task.CompletedTask;
}

private bool IsItemAdded(Customer listItem, Customer selectedItem)
public class CustomerComparer : IEqualityComparer<Customer>
{
return listItem.Id == selectedItem.Id;
public static readonly CustomerComparer Instance = new();

public bool Equals(Customer? x, Customer? y)
{
if (ReferenceEquals(x, y))
{
return true;
}

if (x is null || y is null)
{
return false;
}

return x.Id == y.Id &&
x.Name == y.Name;
}

public int GetHashCode(Customer obj) => HashCode.Combine(obj.Id, obj.Name);
}
}