From a734346619bb73c9288af0b66c6491254a640197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rard=20Frantz?= Date: Sat, 9 Mar 2024 09:49:03 +0100 Subject: [PATCH] Adding the Class parameter to SortableList Updating the samples --- Pages/Index.razor | 3 + Shared/Demos/SimpleListWithClass.razor | 85 ++++++++++++++++++++++++++ Shared/SortableList.razor | 5 +- wwwroot/index.html | 2 + 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 Shared/Demos/SimpleListWithClass.razor diff --git a/Pages/Index.razor b/Pages/Index.razor index 191bc9d..d5bdfc2 100644 --- a/Pages/Index.razor +++ b/Pages/Index.razor @@ -40,4 +40,7 @@
+
+ +
\ No newline at end of file diff --git a/Shared/Demos/SimpleListWithClass.razor b/Shared/Demos/SimpleListWithClass.razor new file mode 100644 index 0000000..18f2e07 --- /dev/null +++ b/Shared/Demos/SimpleListWithClass.razor @@ -0,0 +1,85 @@ +
+

Simple List

+ + +
+
+ + +
+

@item.Name

+
+
+
+
+
+
+
+
+ +@code { + + private string codeContent = $@" + + +
+

@item.Name

+
+
+
+ + @code {{ + public class Item + {{ + public int Id {{ get; set; }} + public string Name {{ get; set; }} + }} + + public List items = Enumerable.Range(1, 10).Select(i => new Item {{ Id = i, Name = $""Item {{i:00}}"" }}).ToList(); + + private void SortList((int oldIndex, int newIndex) indices) + {{ + // deconstruct the tuple + var (oldIndex, newIndex) = indices; + + var items = this.items; + var itemToMove = items[oldIndex]; + items.RemoveAt(oldIndex); + + if (newIndex < items.Count) + {{ + items.Insert(newIndex, itemToMove); + }} + else + {{ + items.Add(itemToMove); + }} + + StateHasChanged(); + }} + }}"; + + public List items = Enumerable.Range(1, 10).Select(i => new Item { Id = i, Name = $"Item {i:00}" }).ToList(); + + + private void SortList((int oldIndex, int newIndex) indices) + { + // deconstruct the tuple + var (oldIndex, newIndex) = indices; + + var items = this.items; + var itemToMove = items[oldIndex]; + items.RemoveAt(oldIndex); + + if (newIndex < items.Count) + { + items.Insert(newIndex, itemToMove); + } + else + { + items.Add(itemToMove); + } + + StateHasChanged(); + } +} diff --git a/Shared/SortableList.razor b/Shared/SortableList.razor index 1f0e298..5472321 100644 --- a/Shared/SortableList.razor +++ b/Shared/SortableList.razor @@ -5,7 +5,7 @@ @typeparam T -
+
@foreach (var item in Items) { @if (SortableItemTemplate is not null) @@ -53,6 +53,9 @@ [Parameter] public bool ForceFallback { get; set; } = true; + [Parameter] + public string? Class { get; set; } + private DotNetObjectReference>? selfReference; protected override async Task OnAfterRenderAsync(bool firstRender) diff --git a/wwwroot/index.html b/wwwroot/index.html index 3942904..8a263cc 100644 --- a/wwwroot/index.html +++ b/wwwroot/index.html @@ -10,6 +10,7 @@ + @@ -23,6 +24,7 @@
+ \ No newline at end of file