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(Sortable): re-sort by Order through js-interop when items changed #2293

Merged
merged 2 commits into from
Dec 23, 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
18 changes: 9 additions & 9 deletions src/Masa.Blazor/Components/Sortable/MSortable.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
@typeparam TItem

<MElement Class="@GetClass()"
Style="@GetStyle()"
Tag="@Tag"
id="@Id"
ReferenceCaptureAction="r => Ref = r"
@attributes="@Attributes">
Style="@GetStyle()"
Tag="@Tag"
id="@Id"
ReferenceCaptureAction="r => Ref = r"
@attributes="@Attributes">
@GenItems()
</MElement>

Expand All @@ -25,10 +25,10 @@
var dataId = ItemKey?.Invoke(item);

<MElement Tag="@ItemTag"
Class="@GetClass(_block.Element("item").Name, ItemClass)"
Style="@ItemStyle"
@key="@(dataId ?? (object?)item)"
data-id="@dataId">
Class="@GetClass(_block.Element("item").Name, ItemClass)"
Style="@ItemStyle"
@key="@(dataId ?? (object?)item)"
data-id="@dataId">
@ChildContent(item)
</MElement>
}
Expand Down
3 changes: 1 addition & 2 deletions src/Masa.Blazor/Components/Sortable/MSortableProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.AddContent(0, sb =>
{
var i = 0;
foreach (var item in Items)
{
Dictionary<string, object?> attrs = new()
{
{ "data-id", ItemKey(item) }
};
var context = new SortableItemContext<TItem>(item, attrs);
sb.AddContent(i++, ItemContent?.Invoke(context));
sb.AddContent(1, ItemContent?.Invoke(context));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems that the sequence in the for loop doesn't need to be incremented, as I checked the compiled source code of @foreach in Razor.

}
});
}
Expand Down
44 changes: 11 additions & 33 deletions src/Masa.Blazor/Components/Sortable/MSortableProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ public bool Disabled
[Parameter] public EventCallback<string> OnRemove { get; set; }

private IEnumerable<TItem>? _prevItems;
private HashSet<string> _prevItemKeys;
private List<string> _internalOrder;
private HashSet<string> _prevItemKeys = [];
private string[] _prevOrder = [];

private DotNetObjectReference<SortableJSInteropHandle>? _sortableJSInteropHandle;
Expand Down Expand Up @@ -224,15 +223,15 @@ protected override void OnParametersSet()
_prevItems = Items;
_prevItemKeys = GetItemKeys();
_renderRateLimiter?.RecordRender();
RefreshOrder();
SortByOrder();
}
else
{
var keys = GetItemKeys();
if (!_prevItemKeys.SetEquals(keys))
{
_prevItemKeys = keys;
SortByInternalOrder();
SortByOrder();
}
}

Expand All @@ -256,12 +255,12 @@ private async Task InitAsync()
{
return;
}

if (_sortableJSInteropHandle is null || ContainerSelector is null)
{
return;
}

_jsObjectReference =
await SortableJSModule.InitAsync(ContainerSelector, GenOptions(), Order, _sortableJSInteropHandle);
}
Expand Down Expand Up @@ -294,53 +293,32 @@ private SortableOptions GenOptions()
};
}

private void UpdateOrderInternal(List<string> order)
public ValueTask UpdateOrder(List<string> order)
{
_internalOrder = order;
_ = OrderChanged.InvokeAsync(order);
}

public async ValueTask UpdateOrder(List<string> order)
{
UpdateOrderInternal(order);
return ValueTask.CompletedTask;
}

public async ValueTask HandleOnAdd(string key, List<string> order)
{
UpdateOrderInternal(order);

_ = OrderChanged.InvokeAsync(order);
await OnAdd.InvokeAsync(key);
}

public async ValueTask HandleOnRemove(string key, List<string> order)
{
UpdateOrderInternal(order);

_ = OrderChanged.InvokeAsync(order);
await OnRemove.InvokeAsync(key);
}

private void RefreshOrder()
{
if (_jsObjectReference is null)
{
return;
}

NextTick(async () =>
{
var order = await _jsObjectReference.ToArrayAsync();
UpdateOrderInternal(order);
});
}

private void SortByInternalOrder()
private void SortByOrder()
{
if (_jsObjectReference is null)
{
return;
}

NextTick(() => _ = _jsObjectReference.SortAsync(_internalOrder, false));
NextTick(() => _ = _jsObjectReference.SortAsync(Order, false).ConfigureAwait(false));
}

protected override async ValueTask DisposeAsyncCore()
Expand Down
Loading