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
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.5.6</Version>
<Version>9.5.7-beta01</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
15 changes: 12 additions & 3 deletions src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,23 @@ export function init(id, invoke) {
}
});

Input.composition(input, async v => {
let filterDuration = duration;
if (filterDuration === 0) {
filterDuration = 200;
}
const filterCallback = debounce(async v => {
await invoke.invokeMethodAsync('TriggerFilter', v);
el.classList.remove('is-loading');
}, filterDuration);

Input.composition(input, v => {
if (isPopover === false) {
ac.show();
}

el.classList.add('is-loading');
await invoke.invokeMethodAsync('TriggerFilter', v);
el.classList.remove('is-loading');
filterCallback(v);

});

ac.show = () => {
Expand Down
15 changes: 10 additions & 5 deletions src/BootstrapBlazor/Components/AutoFill/AutoFill.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@
data-bb-auto-dropdown-focus="@ShowDropdownListOnFocusString" data-bb-debounce="@DurationString"
data-bb-skip-esc="@SkipEscString" data-bb-skip-enter="@SkipEnterString"
data-bb-scroll-behavior="@ScrollIntoViewBehaviorString"
value="@_displayText"
@bind="@_displayText"
placeholder="@PlaceHolder" disabled="@Disabled" @ref="FocusElement" />
<span class="form-select-append"><i class="@Icon"></i></span>
<span class="form-select-append ac-loading"><i class="@LoadingIcon"></i></span>
@if (GetClearable())
{
<span class="@ClearClassString" @onclick="OnClearValue"><i class="@ClearIcon"></i></span>
}
<div class="dropdown-menu">
<RenderTemplate @ref="_dropdown">
@RenderDropdown
</RenderTemplate>
</div>

@code {
RenderFragment RenderDropdown =>
@<div class="dropdown-menu">
@if (IsVirtualize)
{
<div class="dropdown-menu-body dropdown-virtual">
Expand Down Expand Up @@ -52,10 +59,8 @@
}
</div>
}
</div>
</div>
</div>;

@code {
RenderFragment<TValue> RenderRow => item =>
@<div @key="@item" class="dropdown-item" @onclick="() => OnClickItem(item)">
@if (ItemTemplate != null)
Expand Down
37 changes: 9 additions & 28 deletions src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ public partial class AutoFill<TValue>
private List<TValue>? _filterItems;

[NotNull]
private Virtualize<TValue>? _virtualizeElement = default;
private Virtualize<TValue>? _virtualizeElement = null;

[NotNull]
private RenderTemplate? _dropdown = null;
Comment on lines +154 to +155
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Avoid marking _dropdown as [NotNull] while initializing it to null.

The [NotNull] attribute suggests the field will never be null, yet it’s explicitly set to null. Consider either providing a non-null default value or removing the annotation to prevent any potential confusion or runtime issues.

Suggested change
[NotNull]
private RenderTemplate? _dropdown = null;
private RenderTemplate? _dropdown = null;


/// <summary>
/// Gets the clear icon class string.
Expand Down Expand Up @@ -181,13 +184,6 @@ protected override void OnParametersSet()
Items ??= [];
}

private bool _render = true;

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
protected override bool ShouldRender() => _render;

private bool IsNullable() => !ValueType.IsValueType || NullableUnderlyingType != null;

Expand Down Expand Up @@ -239,10 +235,7 @@ private async Task OnClickItem(TValue val)

private async ValueTask<ItemsProviderResult<TValue>> LoadItems(ItemsProviderRequest request)
{
_render = false;
var data = await OnQueryAsync(new() { StartIndex = request.StartIndex, Count = request.Count, SearchText = _searchText });
_render = true;

var _totalCount = data.TotalCount;
var items = data.Items ?? [];
return new ItemsProviderResult<TValue>(items, _totalCount);
Expand All @@ -261,7 +254,7 @@ public override async Task TriggerFilter(string val)
{
_searchText = val;
await _virtualizeElement.RefreshDataAsync();
StateHasChanged();
_dropdown.Render();
return;
}

Expand All @@ -276,29 +269,17 @@ public override async Task TriggerFilter(string val)
}
else
{
var comparision = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
var items = IsLikeMatch
? Items.Where(i => OnGetDisplayText?.Invoke(i)?.Contains(val, comparision) ?? false)
: Items.Where(i => OnGetDisplayText?.Invoke(i)?.StartsWith(val, comparision) ?? false);
? Items.Where(i => OnGetDisplayText?.Invoke(i)?.Contains(val, comparison) ?? false)
: Items.Where(i => OnGetDisplayText?.Invoke(i)?.StartsWith(val, comparison) ?? false);
_filterItems = [.. items];
}

if (!IsVirtualize && DisplayCount != null)
{
_filterItems = [.. _filterItems.Take(DisplayCount.Value)];
}
StateHasChanged();
}

/// <summary>
/// Triggers the change method.
/// </summary>
/// <param name="val">The value to change to.</param>
[JSInvokable]
public Task TriggerChange(string val)
{
_displayText = val;
StateHasChanged();
return Task.CompletedTask;
_dropdown.Render();
}
}
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Search/Search.razor
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
data-bb-skip-esc="@SkipEscString" data-bb-skip-enter="@SkipEnterString" data-bb-blur="@TriggerBlurString"
data-bb-scroll-behavior="@ScrollIntoViewBehaviorString"
data-bb-input="@UseInputString"
value="@_displayText"
@bind="@_displayText"
placeholder="@PlaceHolder" disabled="@Disabled" @ref="FocusElement" />
@if (IsClearable)
{
Expand Down
11 changes: 2 additions & 9 deletions src/BootstrapBlazor/Components/Search/Search.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public partial class Search<TValue>
private SearchContext<TValue> _context = default!;

[NotNull]
private RenderTemplate? _dropdown = default;
private RenderTemplate? _dropdown = null;

/// <summary>
/// <inheritdoc/>
Expand Down Expand Up @@ -282,14 +282,7 @@ private async Task OnClickItem(TValue val)
/// </summary>
/// <param name="val"></param>
[JSInvokable]
public override Task TriggerFilter(string val) => TriggerChange(val);

/// <summary>
/// TriggerOnChange 方法
/// </summary>
/// <param name="val"></param>
[JSInvokable]
public async Task TriggerChange(string val)
public override async Task TriggerFilter(string val)
{
_render = false;
_displayText = val;
Expand Down
14 changes: 1 addition & 13 deletions test/UnitTest/Components/AutoFillTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public async Task OnSelectedItemChanged_Ok()
}

[Fact]
public async Task OnGetDisplayText_Ok()
public void OnGetDisplayText_Ok()
{
var cut = Context.RenderComponent<AutoFill<Foo>>(pb =>
{
Expand All @@ -174,18 +174,6 @@ public async Task OnGetDisplayText_Ok()
});
var input = cut.Find("input");
Assert.Equal("张三 1000", input.Attributes["value"]?.Value);

cut.SetParametersAndRender(pb =>
{
pb.Add(a => a.OnGetDisplayText, null!);
});
await cut.InvokeAsync(() => cut.Instance.TriggerChange("t"));

cut.SetParametersAndRender(pb =>
{
pb.Add(a => a.IsLikeMatch, true);
});
await cut.InvokeAsync(() => cut.Instance.TriggerChange("t"));
}

[Fact]
Expand Down
9 changes: 4 additions & 5 deletions test/UnitTest/Components/SearchTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ public async Task OnGetDisplayText_Ok()
});
pb.Add(a => a.OnGetDisplayText, foo => foo?.Name);
});

await cut.InvokeAsync(() => cut.Instance.TriggerChange("t"));
await Task.Delay(20);

await cut.InvokeAsync(() => cut.Instance.TriggerFilter("t"));
Assert.Contains("test1", cut.Markup);
Assert.Contains("test2", cut.Markup);
}
Expand Down Expand Up @@ -143,7 +140,9 @@ public async Task OnSelectedItemChanged_Ok()
return items;
});
});
await cut.InvokeAsync(() => cut.Instance.TriggerChange("t"));

await cut.InvokeAsync(() => cut.Instance.TriggerFilter("t"));
await Task.Delay(20);

var item = cut.Find(".dropdown-item");
await cut.InvokeAsync(() => item.Click());
Expand Down