Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix(Select): revert the changes that using Virtualize for rendering the list #2039

Merged
merged 3 commits into from
Jul 16, 2024
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
11 changes: 10 additions & 1 deletion src/Masa.Blazor.JS/src/interop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ export function getMenuableDimensions(hasActivator, activatorSelector, isDefault

var dimensions = {
activator: {} as any,
content: {},
content: {} as any,
relativeYOffset: 0,
offsetParentLeft: 0
};
Expand Down Expand Up @@ -1077,6 +1077,8 @@ export function getMenuableDimensions(hasActivator, activatorSelector, isDefault
}

dimensions.content = measure(contentElement, isDefaultAttach)
dimensions.content.offsetLeft = contentElement.offsetLeft
dimensions.content.offsetTop = contentElement.offsetTop
}
}, contentElement);

Expand Down Expand Up @@ -1626,4 +1628,11 @@ export function updateTabSlider(
if (vertical) {
sliderWrapper.style.top = `${top}px`;
}
}

export function isScrollNearBottom(element: HTMLElement, threshold: number = 200) {
if (!element) {
return false;
}
return element.scrollHeight - (element.scrollTop + element.clientHeight) < threshold;
}
Empty file.
Empty file.
1 change: 1 addition & 0 deletions src/Masa.Blazor/Components/Menu/MMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
Style="@style"
Class="@css"
id="@_contentId"
onscroll="@OnScroll"
ReferenceCaptureAction="el => ContentElement = el">
@ChildContent
</ShowTransitionElement>
Expand Down
2 changes: 2 additions & 0 deletions src/Masa.Blazor/Components/Menu/MMenu.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public bool CloseOnContentClick

[Parameter] [MasaApiParameter("auto")] public StringNumber MaxHeight { get; set; } = "auto";

[Parameter] public EventCallback<WheelEventArgs> OnScroll { get; set; }

[Parameter] public EventCallback<MouseEventArgs> OnOutsideClick { get; set; }

[Parameter] public string? Origin { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion src/Masa.Blazor/Components/Select/MSelect.razor
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
Right="@ComputedMenuProps.Right"
Top="@ComputedMenuProps.Top"
Transition="@ComputedMenuProps.Transition"
OnScroll="@OnMenuScroll"
@ref="@MMenu">
@GenSelectList()
</MMenu>
Expand All @@ -191,7 +192,7 @@
Color="@ItemColor"
Dense="@Dense"
HideSelected="@HideSelected"
Items="@ComputedItems"
Items="@VirtualizedItems"
ItemDisabled="@ItemDisabled"
ItemText="@ItemText"
ItemValue="@ItemValue"
Expand Down
36 changes: 35 additions & 1 deletion src/Masa.Blazor/Components/Select/MSelect.razor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq.Expressions;
using Masa.Blazor.Components.Input;
using Masa.Blazor.Mixins;
using StyleBuilder = Masa.Blazor.Core.StyleBuilder;

namespace Masa.Blazor;
Expand Down Expand Up @@ -155,6 +154,8 @@
protected List<TItem> AllItems => FilterDuplicates(CachedItems.Concat(Items)).ToList();

protected virtual List<TItem> ComputedItems => AllItems;

private List<TItem> VirtualizedItems => MMenu?.Auto is true ? ComputedItems : ComputedItems.Take(lastItem).ToList();

protected List<TItem> ComputedItemsIfHideSelected =>
HideSelected ? ComputedItems.Where(item => !SelectedItems.Contains(item)).ToList() : ComputedItems;
Expand Down Expand Up @@ -398,7 +399,7 @@
IsMenuActive = true;
}

private bool IndependentTheme => (IsDirtyParameter(nameof(Dark)) && Dark) || (IsDirtyParameter(nameof(Light)) && Light);

Check warning on line 402 in src/Masa.Blazor/Components/Select/MSelect.razor.cs

View workflow job for this annotation

GitHub Actions / pr

'MSelect<TItem, TItemValue, TValue>.IndependentTheme' hides inherited member 'MInput<TValue>.IndependentTheme'. Use the new keyword if hiding was intended.

#if NET8_0_OR_GREATER

Expand Down Expand Up @@ -734,6 +735,39 @@
}
}

private int lastItem = 20;
private CancellationTokenSource? _scrollCts;

private async Task OnMenuScroll(WheelEventArgs args)
{
try
{
if (!IsMenuActive || lastItem > ComputedItems.Count)
{
return;
}

_scrollCts?.Cancel();
_scrollCts = new CancellationTokenSource();
await Task.Delay(16, _scrollCts.Token);

var content = MMenu?.Dimensions.Content;
if (content is not null)
{
var showMoreItems = await Js.InvokeAsync<bool>(JsInteropConstants.IsScrollNearBottom, MMenu!.ContentElement, 200);
if (showMoreItems)
{
lastItem += 20;
StateHasChanged();
}
}
}
catch (TaskCanceledException)
{
// ignored
}
}

public override async Task HandleOnBlurAsync(FocusEventArgs args)
{
if (OnBlur.HasDelegate)
Expand Down
8 changes: 4 additions & 4 deletions src/Masa.Blazor/Components/Select/MSelectList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@typeparam TItem
@typeparam TItemValue
@typeparam TValue
@using Microsoft.AspNetCore.Components.Web.Virtualization

<MList Class="m-select-list"
Dense="Dense"
Expand All @@ -15,9 +14,10 @@
@PrependItemContent
if (computedItems.Count > 0)
{
<Virtualize Items="@computedItems"
ItemContent="@VirtualizeItemContent">
</Virtualize>
@foreach(var item in computedItems)
{
@VirtualizeItemContent(item)
}
}
else
{
Expand Down
8 changes: 7 additions & 1 deletion src/Masa.Blazor/JSInterop/JsInteropConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ public static class JsInteropConstants
internal static string RegisterTableScrollEvent => $"{JsInteropFuncNamePrefix}registerTableScrollEvent";

internal static string UnregisterTableScrollEvent => $"{JsInteropFuncNamePrefix}unregisterTableScrollEvent";

internal static string UpdateTabSlider => $"{JsInteropFuncNamePrefix}updateTabSlider";

/// <summary>
/// Check if the scroll is near the bottom of the element.
/// Arguments: element, threshold
/// </summary>
public static string IsScrollNearBottom => $"{JsInteropFuncNamePrefix}isScrollNearBottom";
}
1 change: 0 additions & 1 deletion src/Masa.Blazor/Mixins/Menuable/MMenuable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ protected double ComputedTop
{
OffsetTop = PositionY ?? AbsoluteY,
OffsetLeft = PositionX ?? AbsoluteX,
ScrollHeight = 0,
Top = PositionY ?? AbsoluteY,
Bottom = PositionY ?? AbsoluteY,
Left = PositionX ?? AbsoluteX,
Expand Down
1 change: 0 additions & 1 deletion src/Masa.Blazor/Mixins/Menuable/MenuablePosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ public class MenuablePosition : BoundingClientRect
{
public double OffsetTop { get; set; }
public double OffsetLeft { get; set; }
public double ScrollHeight { get; set; }

public MenuablePosition()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Masa.Blazor/wwwroot/js/masa-blazor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Masa.Blazor/wwwroot/js/masa-blazor.js.map

Large diffs are not rendered by default.

Loading