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
10 changes: 5 additions & 5 deletions src/BootstrapBlazor/Components/Filters/FilterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ public abstract class FilterBase : BootstrapModuleComponentBase, IFilterAction
[Parameter]
public int Count { get; set; }

/// <summary>
/// 获得/设置 多个条件逻辑关系符号
/// </summary>
protected FilterLogic Logic { get; set; }

/// <summary>
/// 获得/设置 所属 TableFilter 实例
/// </summary>
Expand All @@ -55,6 +50,11 @@ public abstract class FilterBase : BootstrapModuleComponentBase, IFilterAction
[CascadingParameter]
protected FilterContext? FilterContext { get; set; }

/// <summary>
/// 获得/设置 多个条件逻辑关系符号
/// </summary>
protected FilterLogic Logic { get; set; }

/// <summary>
/// <inheritdoc/>
/// </summary>
Expand Down
17 changes: 15 additions & 2 deletions src/BootstrapBlazor/Components/Filters/LookupFilter.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,29 @@ public partial class LookupFilter
/// <summary>
/// <inheritdoc/>
/// </summary>
protected override void OnParametersSet()
protected override async Task OnParametersSetAsync()
{
base.OnParametersSet();
await base.OnParametersSetAsync();

if (TableColumnFilter != null)
{
var column = TableColumnFilter.Column;
_isShowSearch = column.ShowSearchWhenSelect;
_type = column.PropertyType;
_lookup = column;

if (string.IsNullOrEmpty(_value))
{
var service = _lookup.LookupService;
if (service != null)
{
var items = await _lookup.GetItemsAsync(service, _lookup.LookupServiceKey, _lookup.LookupServiceData);
if(items != null)
{
_value = items.FirstOrDefault()?.Value;
}
}
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/BootstrapBlazor/Components/Select/Select.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public partial class Select<TValue> : ISelect, ILookup
private ILookupService? InjectLookupService { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the "active" state should be used when the associated value is null.
/// 获得/设置 值为 null 时是否使用第一个选项作为默认值
/// <para>Gets or sets a value indicating whether the "active" state should be used when the associated value is null.</para>
/// </summary>
[Parameter]
public bool IsUseActiveWhenValueIsNull { get; set; }
Expand Down
106 changes: 104 additions & 2 deletions test/UnitTest/Components/TableLookupFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ public async Task FilterAction_Ok()
});
var lookup = cut.FindComponent<LookupFilter>();
var filter = lookup.Instance;

var newConditions = new FilterKeyValueAction()
{
Filters =
[
new FilterKeyValueAction() { FieldValue = "1" },
new FilterKeyValueAction() { FieldValue = "2" },
]
};
await cut.InvokeAsync(() => filter.SetFilterConditionsAsync(newConditions));
var conditions = filter.GetFilterConditions();
Assert.Single(conditions.Filters);
Assert.Equal("2", conditions.Filters[0].FieldValue);

await cut.InvokeAsync(() => filter.Reset());
conditions = filter.GetFilterConditions();
Assert.Empty(conditions.Filters);
Expand All @@ -64,6 +65,51 @@ public async Task FilterAction_Ok()
Assert.Empty(conditions.Filters);
}

[Fact]
public async Task LookupService_Ok()
{
var cut = Context.RenderComponent<TableColumnFilter>(pb =>
{
pb.Add(a => a.Table, new MockTable());
pb.Add(a => a.Column, new MockLookupServiceColumn());
});
var lookup = cut.FindComponent<LookupFilter>();
var filter = lookup.Instance;

// 由于 LookupFilter 默认值未设置使用候选项第一个
cut.WaitForAssertion(() => cut.Contains("value=\"LookupService-Test-1-async\""), TimeSpan.FromMilliseconds(100));
var newConditions = new FilterKeyValueAction()
{
Filters =
[
new FilterKeyValueAction() { FieldValue = "v2" },
]
};
await cut.InvokeAsync(() => filter.SetFilterConditionsAsync(newConditions));
var conditions = filter.GetFilterConditions();
Assert.Single(conditions.Filters);
Assert.Equal("v2", conditions.Filters[0].FieldValue);

await cut.InvokeAsync(() => filter.Reset());
conditions = filter.GetFilterConditions();
Assert.Empty(conditions.Filters);
}

[Fact]
public async Task LookupService_Empty()
{
var column = new MockEmptyLookupServiceColumn();
var cut = Context.RenderComponent<TableColumnFilter>(pb =>
{
pb.Add(a => a.Table, new MockTable());
pb.Add(a => a.Column, column);
});
var lookup = cut.FindComponent<LookupFilter>();
var filter = lookup.Instance;

await column.Task;
}

class MockTable : ITable
{
public Dictionary<string, IFilterAction> Filters { get; set; } = [];
Expand All @@ -89,4 +135,60 @@ public MockColumn()
};
}
}

class MockLookupServiceColumn : TableColumn<Foo, string>
{
public MockLookupServiceColumn()
{
PropertyType = typeof(string);
FieldName = "Lookup";
LookupService = new LookupFilterService();
LookupServiceKey = "LookupKey";
}
}

class MockEmptyLookupServiceColumn : TableColumn<Foo, string>
{
private LookupFilterService _service = new LookupFilterService();

public MockEmptyLookupServiceColumn()
{
PropertyType = typeof(string);
FieldName = "Lookup";
LookupService = _service;
LookupServiceKey = "LookupEmptyKey";
}

public Task Task => _service.Task;
}

class LookupFilterService : LookupServiceBase
{
private TaskCompletionSource _taskCompletionSource = new();

public override IEnumerable<SelectedItem>? GetItemsByKey(string? key, object? data) => null;

public override async Task<IEnumerable<SelectedItem>?> GetItemsByKeyAsync(string? key, object? data)
{
IEnumerable<SelectedItem>? ret = null;

if (key == "LookupKey")
{
await Task.Delay(30);
ret =
[
new SelectedItem("v1", "LookupService-Test-1-async"),
new SelectedItem("v2", "LookupService-Test-2-async")
];
}
else if (key == "LookupEmptyKey")
{
ret = [];
_taskCompletionSource.TrySetResult();
}
return ret;
}

public Task Task => _taskCompletionSource.Task;
}
}