Skip to content

Commit

Permalink
Merge pull request #1 from digitaldirk/main
Browse files Browse the repository at this point in the history
Added ForceFallback property on SortableList and demo page
  • Loading branch information
burkeholland authored Jan 29, 2024
2 parents 3f497db + d6494f6 commit 5b9ee8d
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@
<section class="section">
<Filtering></Filtering>
</section>
<section class="section">
<ForceFallback></ForceFallback>
</section>
</div>
2 changes: 2 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ All proerties are optional and have default values.

`Filter` (Default: ""): The CSS class you want to use to identify elements that cannot be sorted or moved.

`ForceFallback`: Boolean (Default: True) - Enable HTML5 drag and drop by setting to `false`.

### Methods

`OnUpdate` (Optional): The method to be called when the list is updated. You can name this method whatever you like, but it expects a `oldIndex` and `newIndex` parameters when the drag and drop occurs.
Expand Down
100 changes: 100 additions & 0 deletions Shared/Demos/ForceFallback.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<div class="container">
<h1 class="title is-size-1 has-text-centered">Force Fallback to JavaScript</h1>
<p class="mb-4">Due to HTML5 drag and drop not being universally supported and the loss of control over the styling forceFallback is set to true by default for this library so the HTML5 drag and drop is not used. If preferred, you can set the 'ForceFallback' to false to get SortableJS's default behavior. The list on the left has 'ForceFallback' set to true, and the list on the right has ForceFallback set to false.</p>
<Tabs CodeContent="@codeContent">
<ExampleContent>
<div class="columns">
<div class="column">
<SortableList Id="forceFallback1" Items="items" OnUpdate="@SortList" Context="item">
<SortableItemTemplate>
<div class="card has-border-light has-background-blazor has-text-white has-cursor-grab">
<p class="is-size-4 p-2 ml-4">@item.Name</p>
</div>
</SortableItemTemplate>
</SortableList>
</div>
<div class="column">
<SortableList Id="forceFallback2" Items="items2" OnUpdate="@SortList" Context="item" ForceFallback="false">
<SortableItemTemplate>
<div class="card has-border-light has-background-blazor has-text-white has-cursor-grab">
<p class="is-size-4 p-2 ml-4">@item.Name</p>
</div>
</SortableItemTemplate>
</SortableList>
</div>
</div>
</ExampleContent>
</Tabs>
</div>

@code {

private string codeContent = $@"
<SortableList Id=""forceFallback1"" Items=""items"" OnUpdate=""@SortList"" Context=""item"">
<SortableItemTemplate>
<div class=""card has-border-light has-background-blazor has-text-white has-cursor-grab"">
<p class=""is-size-4 p-2 ml-4"">@item.Name</p>
</div>
</SortableItemTemplate>
</SortableList>
<SortableList Id=""forceFallback2"" Items=""items2"" OnUpdate=""@SortList"" Context=""item"" ForceFallback=""false"">
<SortableItemTemplate>
<div class=""card has-border-light has-background-blazor has-text-white has-cursor-grab"">
<p class=""is-size-4 p-2 ml-4"">@item.Name</p>
</div>
</SortableItemTemplate>
</SortableList>
@code {{
public List<Item> items = Enumerable.Range(1, 10).Select(i => new Item {{ Id = i, Name = $""Item {{i}}"" }}).ToList();
public List<Item> items2 = Enumerable.Range(1, 10).Select(i => new Item {{ Id = i, Name = $""Item {{i}}"" }}).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<Item> items = Enumerable.Range(1, 10).Select(i => new Item { Id = i, Name = $"Item {i}" }).ToList();

public List<Item> items2 = Enumerable.Range(1, 10).Select(i => new Item { Id = i, Name = $"Item {i}" }).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();
}
}
5 changes: 4 additions & 1 deletion Shared/SortableList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
[Parameter]
public string? Filter { get; set; }

[Parameter]
public bool ForceFallback { get; set; } = true;

private DotNetObjectReference<SortableList<T>>? selfReference;

protected override async Task OnAfterRenderAsync(bool firstRender)
Expand All @@ -58,7 +61,7 @@
{
selfReference = DotNetObjectReference.Create(this);
var module = await JS.InvokeAsync<IJSObjectReference>("import", "./Shared/SortableList.razor.js");
await module.InvokeAsync<string>("init", Id, Group, Pull, Put, Sort, Handle, Filter, selfReference);
await module.InvokeAsync<string>("init", Id, Group, Pull, Put, Sort, Handle, Filter, selfReference, ForceFallback);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Shared/SortableList.razor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function init(id, group, pull, put, sort, handle, filter, component) {
export function init(id, group, pull, put, sort, handle, filter, component, forceFallback) {
var sortable = new Sortable(document.getElementById(id), {
animation: 200,
group: {
Expand All @@ -8,7 +8,7 @@ export function init(id, group, pull, put, sort, handle, filter, component) {
},
filter: filter || undefined,
sort: sort,
forceFallback: true,
forceFallback: forceFallback,
handle: handle || undefined,
onUpdate: (event) => {
// Revert the DOM to match the .NET state
Expand Down

0 comments on commit 5b9ee8d

Please sign in to comment.