@foreach (var item in Items.Select((v, i) => (v, i)))
{
-
await onPress(e, item.v, item.i)">
@ChildContent(item.v)
@@ -36,7 +36,7 @@
{
[Parameter, EditorRequired] public RenderFragment
ChildContent { get; set; } = null!;
[Parameter, EditorRequired] public List Items { get; set; } = null!;
- [Parameter] public bool Copy { get; set; } = false;
+ [Parameter] public Func Copy { get; set; }
[Parameter] public bool WithShadow { get; set; } = true;
[Parameter] public bool Debug { get; set; } = false;
@@ -84,12 +84,14 @@
public async Task onPress(MouseEventArgs m, TItem item, int index)
{
if (itemElem == null) return;
+
shouldRender = false; // Because the method triggers re-render, the click propagation is canceled, if you have a link/or any click event inside it's going to stop working
var ghostElem = itemElem[index];
elemWidth = await rs.getWidth(ghostElem);
elemPosition = await rs.getPosition(ghostElem);
clickPosition = await rs.getPoint(m);
- rs.Set(Items, item, index, clickPosition);
+ TItem? clone = Copy != null ? Copy(item) : default(TItem);
+ rs.Set(Items, clone != null ? clone : item, index, clickPosition, clone != null);
}
[JSInvokable]
@@ -138,7 +140,8 @@
newElemIndex = b;
if (WithShadow)
{
- rs.originItems.RemoveAt(rs.elemIndex);
+ if (!rs.isCopy())
+ rs.originItems.RemoveAt(rs.elemIndex);
Items.Insert(newElemIndex, rs.selected);
rs.elemIndex = newElemIndex;
if (rs.originItems != Items) rs.originItems = Items;
diff --git a/BlazorReorderList/ReorderService.cs b/BlazorReorderList/ReorderService.cs
index c6d7ae0..7c698f0 100644
--- a/BlazorReorderList/ReorderService.cs
+++ b/BlazorReorderList/ReorderService.cs
@@ -12,6 +12,7 @@ public class ReorderService : IAsyncDisposable
public TItem selected = default(TItem);
public point elemClickPosition = new point(0, 0);
public bool isDragging = false;
+ private bool _copy = false;
public ReorderService(IJSRuntime jsRuntime)
{
@@ -19,13 +20,22 @@ public class ReorderService : IAsyncDisposable
"import", "./_content/BlazorReorderList/ReorderJsInterop.js").AsTask());
}
- public void Set(List list, TItem item, int index, point clickPoint)
+ public void Set(List list, TItem item, int index, point clickPoint, bool copy)
{
isDragging = true;
originItems = list;
selected = item;
elemIndex = index;
elemClickPosition = clickPoint;
+ _copy = copy;
+ }
+
+ // whe copy only the first time
+ public bool isCopy()
+ {
+ var tempcopy = _copy;
+ _copy = false;
+ return tempcopy;
}
public void Reset()
@@ -34,6 +44,7 @@ public class ReorderService : IAsyncDisposable
originItems = default(List);
selected = default(TItem);
elemClickPosition = new point(0, 0);
+ _copy = false;
}
public async ValueTask initEvents(DotNetObjectReference> dotNetInstance)