BootstrapBlazor icon indicating copy to clipboard operation
BootstrapBlazor copied to clipboard

feat(TreeView): support Darg and Drop function

Open syminomega opened this issue 8 months ago • 3 comments

Issues

#6216

New Feature

Add Draggable TreeView

Summary By Copilot

Regression?

  • [ ] Yes
  • [x] No

Risk

  • [ ] High
  • [ ] Medium
  • [x] Low

Verification

  • [x] Manual (required)
  • [ ] Automated

Packaging changes reviewed?

  • [ ] Yes
  • [ ] No
  • [x] N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • [ ] Doc is updated/provided or not needed
  • [ ] Demo is updated/provided or not needed
  • [ ] Merge the latest code from the main branch

Summary by Sourcery

Implement drag-and-drop support for TreeView nodes with customizable drop behaviors and visual previews.

New Features:

  • Add ItemDraggable, Draggable, and PreviewDrop parameters to enable node dragging and drop preview.
  • Introduce OnItemDragStart, OnItemDragEnd, OnItemDrop callbacks and an OnDrop handler for drop event customization.
  • Define TreeDropEventArgs class and TreeDropType enum to represent drop operations.

Enhancements:

  • Add SCSS styling and CSS classes for drop zones and preview indicators on tree nodes.

Documentation:

  • Update sample TreeViews component to demonstrate draggable TreeView usage and OnDrop event logging.

syminomega avatar May 30 '25 09:05 syminomega

Thanks for your PR, @syminomega. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

bb-auto[bot] avatar May 30 '25 09:05 bb-auto[bot]

Reviewer's Guide

This PR adds full drag-and-drop reordering support to the TreeView component by introducing draggable parameters and callbacks, rendering drop zones with preview indicators, and updating the component logic to handle item reordering on drop.

Sequence Diagram for TreeView Item Drag-and-Drop

sequenceDiagram
    actor User
    participant SourceRow as TreeViewRow (Source)
    participant TargetRow as TreeViewRow (Target)
    participant TreeViewComp as TreeView

    User->>SourceRow: Initiates drag (ondragstart)
    SourceRow->>SourceRow: DragStart(eventArgs)
    SourceRow->>TreeViewComp: OnItemDragStart(SourceItem)
    TreeViewComp->>TreeViewComp: OnItemDragStart(item): sets _draggingItem, _previewDrop, StateHasChanged()

    User->>TargetRow: Drags over drop zone (ondragenter)
    TargetRow->>TargetRow: DragEnterChildInside() / DragEnterChildBelow()
    TargetRow->>TargetRow: Sets preview flags (e.g., _previewChildLast), Renders drop preview

    User->>TargetRow: Drops item (ondrop)
    TargetRow->>TargetRow: DropChildInside() / DropChildBelow()
    TargetRow->>TreeViewComp: OnItemDrop(TreeDropEventArgs)
    TreeViewComp->>TreeViewComp: OnItemDrop(args): sets args.Source
    TreeViewComp->>TreeViewComp: Invokes user OnDrop(args) callback
    alt User OnDrop returns true
        TreeViewComp->>TreeViewComp: Reorders items (updates TreeViewItem parents/collections)
        TreeViewComp->>TreeViewComp: Resets _draggingItem, _previewDrop, StateHasChanged()
    end

    User->>SourceRow: Drag operation ends (ondragend)
    SourceRow->>SourceRow: DragEnd(eventArgs)
    SourceRow->>TreeViewComp: OnItemDragEnd()
    TreeViewComp->>TreeViewComp: OnItemDragEnd(): resets _previewDrop, _draggingItem, StateHasChanged()

File-Level Changes

Change Details Files
Implement draggable behavior in TreeViewRow
  • Add Draggable and PreviewDrop parameters
  • Define OnItemDragStart, OnItemDragEnd and OnItemDrop callbacks
  • Implement drag event handlers (DragStart, DragEnd, DragEnter/Leave, DropChildInside, DropChildBelow)
  • Update markup to render draggable attribute and drop zones with preview elements
  • Add CSS class for drop-pass state
Components/TreeView/TreeViewRow.razor.cs
Components/TreeView/TreeViewRow.razor
Add drop preview and styling
  • Define CSS variable for preview color
  • Add drop-zone layout and preview classes (.tree-drop-zone, .tree-preview-*)
  • Disable user-select and pointer events during drag
  • Add .tree-drop-pass class to suppress pointer events
Components/TreeView/TreeView.razor.scss
Extend TreeView component to handle drag-and-drop
  • Introduce ItemDraggable and OnDrop parameters
  • Maintain preview state and current dragging item
  • Implement OnItemDragStart, OnItemDragEnd, OnItemDrop to reorder items based on drop type
  • Pass draggable settings and callbacks into each TreeViewRow
Components/TreeView/TreeView.razor.cs
Components/TreeView/TreeView.razor
Define drop event arguments and types
  • Add TreeDropEventArgs with Source, Target, DropType and ExpandAfterDrop
  • Introduce TreeDropType enum (AsFirstChild, AsLastChild, AsSiblingBelow)
Components/TreeView/TreeView.razor.cs
Enums/TreeDropType.cs
Update demo to showcase draggable TreeView
  • Enable ItemDraggable and bind OnDrop in sample markup
  • Implement OnDrop handler in sample code to log moves
Server/Components/Samples/TreeViews.razor
Server/Components/Samples/TreeViews.razor.cs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an issue from a review comment by replying to it. You can also reply to a review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull request title to generate a title at any time. You can also comment @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment @sourcery-ai summary on the pull request to (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar May 30 '25 09:05 sourcery-ai[bot]

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 100.00%. Comparing base (1ae8579) to head (0cd9ec8).

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #6121      +/-   ##
===========================================
+ Coverage   99.98%   100.00%   +0.01%     
===========================================
  Files         718       718              
  Lines       31686     31679       -7     
  Branches     4471      4472       +1     
===========================================
- Hits        31681     31679       -2     
+ Misses          4         0       -4     
+ Partials        1         0       -1     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar May 30 '25 09:05 codecov[bot]

DraggableTree examples WIP

syminomega avatar Jul 08 '25 10:07 syminomega