Skip to content
Merged
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 @@ -46,7 +46,7 @@ private async Task<List<SelectedItem>> OnGetAddressItemsAsync()
{
// 模拟数据库延时
await Task.Delay(500);
return Items.Select(i => new SelectedItem(i.Address!, i.Address!)).DistinctBy(i => i.Value).ToList();
return [.. Items.Select(i => new SelectedItem(i.Address!, i.Address!)).DistinctBy(i => i.Value)];
}

private Task<QueryData<Foo>> OnQueryAsync(QueryPageOptions options)
Expand Down
8 changes: 7 additions & 1 deletion src/BootstrapBlazor/Components/Filters/MultiFilter.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public partial class MultiFilter
[Parameter]
public RenderFragment? LoadingTemplate { get; set; }

/// <summary>
/// Gets or sets the string comparison option used for filtering operations. Default is <see cref="StringComparison.OrdinalIgnoreCase"/>
/// </summary>
[Parameter]
public StringComparison StringComparison { get; set; } = StringComparison.OrdinalIgnoreCase;

private string? _searchText;

private List<SelectedItem>? _source;
Expand Down Expand Up @@ -226,7 +232,7 @@ private Task OnSearchValueChanged(string? val)
{
if (!string.IsNullOrEmpty(_searchText))
{
_items = _source.Where(i => i.Text.Contains(_searchText)).ToList();
_items = [.. _source.Where(i => i.Text.Contains(_searchText, StringComparison))];
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions test/UnitTest/Components/TableFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ public async Task MultiFilter_Ok()
{
b.OpenComponent<MultiFilter>(0);
b.AddAttribute(1, nameof(MultiFilter.ShowSearch), true);
b.AddAttribute(2, nameof(MultiFilter.OnGetItemsAsync), () => Task.FromResult(new List<SelectedItem>() { new("test1", "test1") }));
b.AddAttribute(1, nameof(MultiFilter.AlwaysTriggerGetItems), true);
b.AddAttribute(2, nameof(MultiFilter.StringComparison), StringComparison.OrdinalIgnoreCase);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Consider adding tests to verify different StringComparison behaviors.

Add tests covering each StringComparison mode. For each (e.g. Ordinal, CurrentCulture, OrdinalIgnoreCase), use items with varied casing (e.g., “Apple”, “apple”, “APPLE”), perform a search, and assert that results match the expected case-sensitive or case-insensitive behavior.

Suggested implementation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

public class TableFilterStringComparisonTests
{
    [Fact]
    public async Task Test_StringComparison_Ordinal()
    {
         // Arrange: Ordinal comparison is case-sensitive so only the exact match should be returned.
         var filter = new MultiFilter
         {
             StringComparison = StringComparison.Ordinal
         };
         var items = new List<SelectedItem>
         {
             new SelectedItem("Apple", "Apple"),
             new SelectedItem("apple", "apple"),
             new SelectedItem("APPLE", "APPLE")
         };

         // Act: Perform search using "Apple" as query.
         var result = await filter.GetItemsAsync("Apple", items);

         // Assert: Only "Apple" should match.
         Assert.Single(result);
         Assert.Equal("Apple", result.First().Text);
    }

    [Fact]
    public async Task Test_StringComparison_OrdinalIgnoreCase()
    {
         // Arrange: OrdinalIgnoreCase comparison returns matches regardless of casing.
         var filter = new MultiFilter
         {
             StringComparison = StringComparison.OrdinalIgnoreCase
         };
         var items = new List<SelectedItem>
         {
             new SelectedItem("Apple", "Apple"),
             new SelectedItem("apple", "apple"),
             new SelectedItem("APPLE", "APPLE")
         };

         // Act: Perform search using "Apple" as query.
         var result = await filter.GetItemsAsync("Apple", items);

         // Assert: All three items should match.
         Assert.Equal(3, result.Count);
    }

    [Fact]
    public async Task Test_StringComparison_CurrentCulture()
    {
         // Arrange: CurrentCulture comparison is typically case-sensitive.
         var filter = new MultiFilter
         {
             StringComparison = StringComparison.CurrentCulture
         };
         var items = new List<SelectedItem>
         {
             new SelectedItem("Apple", "Apple"),
             new SelectedItem("apple", "apple"),
             new SelectedItem("APPLE", "APPLE")
         };

         // Act: Perform search using "Apple" as query.
         var result = await filter.GetItemsAsync("Apple", items);

         // Assert: Only the item with exact casing "Apple" should be returned.
         Assert.Single(result);
         Assert.Equal("Apple", result.First().Text);
    }
}

// End of file

Make sure that:

  1. The MultiFilter class has a GetItemsAsync(searchQuery, items) method or a similar method to perform the filtering, otherwise adjust the test to correctly call the filtering functionality.
  2. The SelectedItem class exposes a property (e.g., Text) that contains the string to be compared.
  3. The necessary using directives are present at the top of the file if they are not already included in your codebase.

b.AddAttribute(3, nameof(MultiFilter.OnGetItemsAsync), () => Task.FromResult(new List<SelectedItem>() { new("test1", "test1") }));
b.AddAttribute(4, nameof(MultiFilter.AlwaysTriggerGetItems), true);
b.CloseComponent();
}));
builder.CloseComponent();
Expand Down