Skip to content

Commit f8d6acc

Browse files
authored
fix(LookupFilter): use first item as default value when value is null (#6338)
* refactor: 调整参数位置 * Revert "fix(EnumFilter): incorrect selected value (#6334)" This reverts commit daf9af9. * Revert "fix(BoolFilter): set default value is empty" This reverts commit 26e33c3. * Reapply "fix(BoolFilter): set default value is empty" This reverts commit 4509634. * Reapply "fix(EnumFilter): incorrect selected value (#6334)" This reverts commit 3f3e125. * refactor: 增加中文注释 * refactor: 支持默认值 * test: 增加单元测试
1 parent 1bcf624 commit f8d6acc

File tree

4 files changed

+126
-10
lines changed

4 files changed

+126
-10
lines changed

src/BootstrapBlazor/Components/Filters/FilterBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ public abstract class FilterBase : BootstrapModuleComponentBase, IFilterAction
3838
[Parameter]
3939
public int Count { get; set; }
4040

41-
/// <summary>
42-
/// 获得/设置 多个条件逻辑关系符号
43-
/// </summary>
44-
protected FilterLogic Logic { get; set; }
45-
4641
/// <summary>
4742
/// 获得/设置 所属 TableFilter 实例
4843
/// </summary>
@@ -55,6 +50,11 @@ public abstract class FilterBase : BootstrapModuleComponentBase, IFilterAction
5550
[CascadingParameter]
5651
protected FilterContext? FilterContext { get; set; }
5752

53+
/// <summary>
54+
/// 获得/设置 多个条件逻辑关系符号
55+
/// </summary>
56+
protected FilterLogic Logic { get; set; }
57+
5858
/// <summary>
5959
/// <inheritdoc/>
6060
/// </summary>

src/BootstrapBlazor/Components/Filters/LookupFilter.razor.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,29 @@ public partial class LookupFilter
1818
/// <summary>
1919
/// <inheritdoc/>
2020
/// </summary>
21-
protected override void OnParametersSet()
21+
protected override async Task OnParametersSetAsync()
2222
{
23-
base.OnParametersSet();
23+
await base.OnParametersSetAsync();
2424

2525
if (TableColumnFilter != null)
2626
{
2727
var column = TableColumnFilter.Column;
2828
_isShowSearch = column.ShowSearchWhenSelect;
2929
_type = column.PropertyType;
3030
_lookup = column;
31+
32+
if (string.IsNullOrEmpty(_value))
33+
{
34+
var service = _lookup.LookupService;
35+
if (service != null)
36+
{
37+
var items = await _lookup.GetItemsAsync(service, _lookup.LookupServiceKey, _lookup.LookupServiceData);
38+
if(items != null)
39+
{
40+
_value = items.FirstOrDefault()?.Value;
41+
}
42+
}
43+
}
3144
}
3245
}
3346

src/BootstrapBlazor/Components/Select/Select.razor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public partial class Select<TValue> : ISelect, ILookup
2727
private ILookupService? InjectLookupService { get; set; }
2828

2929
/// <summary>
30-
/// Gets or sets a value indicating whether the "active" state should be used when the associated value is null.
30+
/// 获得/设置 值为 null 时是否使用第一个选项作为默认值
31+
/// <para>Gets or sets a value indicating whether the "active" state should be used when the associated value is null.</para>
3132
/// </summary>
3233
[Parameter]
3334
public bool IsUseActiveWhenValueIsNull { get; set; }

test/UnitTest/Components/TableLookupFilterTest.cs

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@ public async Task FilterAction_Ok()
3131
});
3232
var lookup = cut.FindComponent<LookupFilter>();
3333
var filter = lookup.Instance;
34-
3534
var newConditions = new FilterKeyValueAction()
3635
{
3736
Filters =
3837
[
39-
new FilterKeyValueAction() { FieldValue = "1" },
38+
new FilterKeyValueAction() { FieldValue = "2" },
4039
]
4140
};
4241
await cut.InvokeAsync(() => filter.SetFilterConditionsAsync(newConditions));
4342
var conditions = filter.GetFilterConditions();
4443
Assert.Single(conditions.Filters);
44+
Assert.Equal("2", conditions.Filters[0].FieldValue);
45+
4546
await cut.InvokeAsync(() => filter.Reset());
4647
conditions = filter.GetFilterConditions();
4748
Assert.Empty(conditions.Filters);
@@ -64,6 +65,51 @@ public async Task FilterAction_Ok()
6465
Assert.Empty(conditions.Filters);
6566
}
6667

68+
[Fact]
69+
public async Task LookupService_Ok()
70+
{
71+
var cut = Context.RenderComponent<TableColumnFilter>(pb =>
72+
{
73+
pb.Add(a => a.Table, new MockTable());
74+
pb.Add(a => a.Column, new MockLookupServiceColumn());
75+
});
76+
var lookup = cut.FindComponent<LookupFilter>();
77+
var filter = lookup.Instance;
78+
79+
// 由于 LookupFilter 默认值未设置使用候选项第一个
80+
cut.WaitForAssertion(() => cut.Contains("value=\"LookupService-Test-1-async\""), TimeSpan.FromMilliseconds(100));
81+
var newConditions = new FilterKeyValueAction()
82+
{
83+
Filters =
84+
[
85+
new FilterKeyValueAction() { FieldValue = "v2" },
86+
]
87+
};
88+
await cut.InvokeAsync(() => filter.SetFilterConditionsAsync(newConditions));
89+
var conditions = filter.GetFilterConditions();
90+
Assert.Single(conditions.Filters);
91+
Assert.Equal("v2", conditions.Filters[0].FieldValue);
92+
93+
await cut.InvokeAsync(() => filter.Reset());
94+
conditions = filter.GetFilterConditions();
95+
Assert.Empty(conditions.Filters);
96+
}
97+
98+
[Fact]
99+
public async Task LookupService_Empty()
100+
{
101+
var column = new MockEmptyLookupServiceColumn();
102+
var cut = Context.RenderComponent<TableColumnFilter>(pb =>
103+
{
104+
pb.Add(a => a.Table, new MockTable());
105+
pb.Add(a => a.Column, column);
106+
});
107+
var lookup = cut.FindComponent<LookupFilter>();
108+
var filter = lookup.Instance;
109+
110+
await column.Task;
111+
}
112+
67113
class MockTable : ITable
68114
{
69115
public Dictionary<string, IFilterAction> Filters { get; set; } = [];
@@ -89,4 +135,60 @@ public MockColumn()
89135
};
90136
}
91137
}
138+
139+
class MockLookupServiceColumn : TableColumn<Foo, string>
140+
{
141+
public MockLookupServiceColumn()
142+
{
143+
PropertyType = typeof(string);
144+
FieldName = "Lookup";
145+
LookupService = new LookupFilterService();
146+
LookupServiceKey = "LookupKey";
147+
}
148+
}
149+
150+
class MockEmptyLookupServiceColumn : TableColumn<Foo, string>
151+
{
152+
private LookupFilterService _service = new LookupFilterService();
153+
154+
public MockEmptyLookupServiceColumn()
155+
{
156+
PropertyType = typeof(string);
157+
FieldName = "Lookup";
158+
LookupService = _service;
159+
LookupServiceKey = "LookupEmptyKey";
160+
}
161+
162+
public Task Task => _service.Task;
163+
}
164+
165+
class LookupFilterService : LookupServiceBase
166+
{
167+
private TaskCompletionSource _taskCompletionSource = new();
168+
169+
public override IEnumerable<SelectedItem>? GetItemsByKey(string? key, object? data) => null;
170+
171+
public override async Task<IEnumerable<SelectedItem>?> GetItemsByKeyAsync(string? key, object? data)
172+
{
173+
IEnumerable<SelectedItem>? ret = null;
174+
175+
if (key == "LookupKey")
176+
{
177+
await Task.Delay(30);
178+
ret =
179+
[
180+
new SelectedItem("v1", "LookupService-Test-1-async"),
181+
new SelectedItem("v2", "LookupService-Test-2-async")
182+
];
183+
}
184+
else if (key == "LookupEmptyKey")
185+
{
186+
ret = [];
187+
_taskCompletionSource.TrySetResult();
188+
}
189+
return ret;
190+
}
191+
192+
public Task Task => _taskCompletionSource.Task;
193+
}
92194
}

0 commit comments

Comments
 (0)