Skip to content

Commit

Permalink
šŸ› fix(Select): revert the changes that using Virtualize for renderingā€¦
Browse files Browse the repository at this point in the history
ā€¦ the list (#2039)

* šŸ› fix(Select): revert the changes that using Virtualize for rendering the list

* merge from develop and resolve conflicts
  • Loading branch information
capdiem authored Jul 16, 2024
1 parent 575ad41 commit a5cb2cb
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 12 deletions.
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 BMenuProps ComputedMenuProps
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 @@ -734,6 +735,39 @@ protected virtual async Task OnTabDown(KeyboardEventArgs args)
}
}

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.

0 comments on commit a5cb2cb

Please sign in to comment.