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

[FluentSortableList] New component #1334

Merged
merged 18 commits into from
Jan 22, 2024
Merged

[FluentSortableList] New component #1334

merged 18 commits into from
Jan 22, 2024

Conversation

vnbaaij
Copy link
Collaborator

@vnbaaij vnbaaij commented Jan 15, 2024

Fluent Sortable List

An implementation of the SortableJS library for Blazor Fluent UI. Allows for reordering elements within a list using drag and drop. Based on Burke Holland's article and code. re-used with permission.

How to use it in your own project

  1. Add SortableJS to your index.html/_Layout.cshtml/App.razor file. You can either download from the SortableJS website or use a CDN:
<script src="https://cdn.jsdelivr.net/npm/sortablejs@1.13.0/Sortable.min.js"></script>
  1. Use the FluentSortableList.razor in your page/app.

  2. Add the SortableList component to your page and define the SortableItemTemplate...

     <FluentSortableList Id="sortable" Items="items" OnUpdate="@SortList" Context="item">
         <ItemTemplate>@item.Name</ItemTemplate>
     </FluentSortableList>
    
  • Items: The list of items to be displayed. Can be of any type.
  • OnUpdate: The method to be called when the list is updated.
  • Context: The name of the variable to be used in the template. (Optional. Use @context. when omitted)

The ItemTemplate can contain any markup or components that you want. Each item is automatically wrapped in a div with class "sortable-item". The following CSS classes can be used to split the drag functionality from the content:

  • sortable-grab: the grabable part of the dragable item
  • sortable-item-content: the content part of the draggable item

By using these seperate classes, you can define and style areas for grabbing and displaying.. See the examples on how this can be used.

API

Parameters

Only the component's own parameters are described here , not the parameters inherited from base classes

ItemTemplate: RenderFragment<TItem>? (default: null) - The template to be used to define each sortable item in the list. Use the @context parameter to access the item and its properties.

Items: IEnumerable<TItem> (default: null) - The list of items to be displayed in the list.

Group: string (default: null) - Used for dragging between lists. Set the group to the same value on both lists to enable.

Clone: bool (default: false) - Used when you want to clone elments into a list instead of moving them. Set clone to true.

Drop: bool (default: true) - Set to false to disable dropping from another list onto the current list.

Sort: bool (default: true) - Disable sorting within a list by setting to false.

Handle: bool (default: false) - Set to true to use the .sortable-grab CSS class name you set on an element in the ItemTemplate to function as the drag handle. In the default situation the whole item acts as a drragable.

ItemFilter : Func<TItem, bool>? (default: null) - Sepcify a lambda to indicate which item(s) from the Items parameter should be filtered out of the dragging capability

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 FluentSortableListEventArgs (containing OldIndex and NewIndex) parameter when the drag and drop occurs.

OnRemove (Optional): The method to be called when an item is removed from a list. This method is useful for handling the drop even between multiple lists. Like OnUpdate, it expects a FluentSortableListEventArgs (containing OldIndex and NewIndex) parameter

You will need to handle all events yourself. If you don't handle any events, no sort or move will happen as Blazor needs to make the changes to the underlying data structures so it can re-render the list.

Here is an example of how to reorder your list when the OnUpdate is fired...

private void SortList((int oldIndex, int newIndex) indices)
{
    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();
}

sortablelist

@vnbaaij vnbaaij added this to the V4.4.0 milestone Jan 15, 2024
@vnbaaij vnbaaij marked this pull request as draft January 15, 2024 22:44
Group name default to null
Fix Cloning example sorting
Fix esproj?
Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

@vnbaaij vnbaaij self-assigned this Jan 20, 2024
@vnbaaij vnbaaij added the feature A new feature label Jan 20, 2024
- Simplify needed  template information by icluding classes in the component logic
- Replace Filter with ItemFilter (Func)
- Add css variables
- Update docs page
- Add a first test
Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

@vnbaaij vnbaaij marked this pull request as ready for review January 20, 2024 19:54
Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

@vnbaaij vnbaaij enabled auto-merge (squash) January 20, 2024 20:01
@vnbaaij vnbaaij changed the title Add FluentSortableList component [FluentSortableList] new component Jan 22, 2024
@vnbaaij vnbaaij changed the title [FluentSortableList] new component [FluentSortableList] New component Jan 22, 2024
Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

1 similar comment
Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

Copy link

Azure Static Web Apps: Your stage site is ready! Visit it here: https://black-pebble-0dc79cb03-1334.westeurope.3.azurestaticapps.net

@vnbaaij vnbaaij merged commit b720e74 into dev Jan 22, 2024
4 checks passed
@vnbaaij vnbaaij deleted the vnbaaij/sortable-lists branch January 22, 2024 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants