Skip to content

feat(dnd): pass draggedKey into getItems #8448

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

reidbarber
Copy link
Member

@reidbarber reidbarber commented Jun 25, 2025

Closes #8162

By passing draggedKey as the second argument of getItems, you can decouple your dragged items from selection.

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

Check test and new story

🧢 Your Project:

@rspbot
Copy link

rspbot commented Jun 25, 2025

@@ -65,7 +65,7 @@ export interface DragAndDropOptions extends Omit<DraggableCollectionProps, 'prev
* A function that returns the items being dragged. If not specified, we assume that the collection is not draggable.
* @default () => []
*/
getItems?: (keys: Set<Key>) => DragItem[],
getItems?: (keys: Set<Key>, draggedKey: Key) => DragItem[],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add to the jsdocs what that second argument is

@rspbot
Copy link

rspbot commented Jun 25, 2025

@@ -63,6 +63,8 @@ export interface DragAndDropHooks {
export interface DragAndDropOptions extends Omit<DraggableCollectionProps, 'preview' | 'getItems'>, Omit<DroppableCollectionProps, 'onMove'> {
/**
* A function that returns the items being dragged. If not specified, we assume that the collection is not draggable.
* @param keys - The set of keys that will be affected by the drag (e.g. the current selection).
Copy link
Member

@snowystinger snowystinger Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the goal is to decouple, then should this say something like

Suggested change
* @param keys - The set of keys that will be affected by the drag (e.g. the current selection).
* @param keys - The set of keys that can be affected by the drag (e.g. the current selection).

or if it's always the selection

   * @param keys - The currently selected keys, or, if no selection, equivalent to the dragged key.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rspbot
Copy link

rspbot commented Jun 25, 2025

@rspbot
Copy link

rspbot commented Jun 25, 2025

## API Changes

react-aria-components

/react-aria-components:DragAndDropOptions

 DragAndDropOptions {
   acceptedDragTypes?: 'all' | Array<string | symbol> = 'all'
   dropTargetDelegate?: DropTargetDelegate
   getAllowedDropOperations?: () => Array<DropOperation>
   getDropOperation?: (DropTarget, DragTypes, Array<DropOperation>) => DropOperation
-  getItems?: (Set<Key>) => Array<DragItem> = () => []
+  getItems?: (Set<Key>, Key) => Array<DragItem> = () => []
   isDisabled?: boolean
   onDragEnd?: (DraggableCollectionEndEvent) => void
   onDragMove?: (DraggableCollectionMoveEvent) => void
   onDragStart?: (DraggableCollectionStartEvent) => void
   onDropActivate?: (DroppableCollectionActivateEvent) => void
   onDropEnter?: (DroppableCollectionEnterEvent) => void
   onDropExit?: (DroppableCollectionExitEvent) => void
   onInsert?: (DroppableCollectionInsertDropEvent) => void
   onItemDrop?: (DroppableCollectionOnItemDropEvent) => void
   onMove?: (DroppableCollectionReorderEvent) => void
   onReorder?: (DroppableCollectionReorderEvent) => void
   onRootDrop?: (DroppableCollectionRootDropEvent) => void
   renderDragPreview?: (Array<DragItem>) => JSX.Element
   renderDropIndicator?: (DropTarget) => JSX.Element
   shouldAcceptItemDrop?: (ItemDropTarget, DragTypes) => boolean
 }

@react-spectrum/dnd

/@react-spectrum/dnd:DragAndDropOptions

 DragAndDropOptions {
   acceptedDragTypes?: 'all' | Array<string | symbol> = 'all'
   getAllowedDropOperations?: () => Array<DropOperation>
   getDropOperation?: (DropTarget, DragTypes, Array<DropOperation>) => DropOperation
-  getItems?: (Set<Key>) => Array<DragItem> = () => []
+  getItems?: (Set<Key>, Key) => Array<DragItem> = () => []
   onDragEnd?: (DraggableCollectionEndEvent) => void
   onDragMove?: (DraggableCollectionMoveEvent) => void
   onDragStart?: (DraggableCollectionStartEvent) => void
   onDrop?: (DroppableCollectionDropEvent) => void
   onDropEnter?: (DroppableCollectionEnterEvent) => void
   onDropExit?: (DroppableCollectionExitEvent) => void
   onInsert?: (DroppableCollectionInsertDropEvent) => void
   onItemDrop?: (DroppableCollectionOnItemDropEvent) => void
   onReorder?: (DroppableCollectionReorderEvent) => void
   onRootDrop?: (DroppableCollectionRootDropEvent) => void
   renderPreview?: (Set<Key>, Key) => JSX.Element
   shouldAcceptItemDrop?: (ItemDropTarget, DragTypes) => boolean
 }

@react-stately/dnd

/@react-stately/dnd:DraggableCollectionStateOptions

 DraggableCollectionStateOptions {
   collection: Collection<Node<unknown>>
   getAllowedDropOperations?: () => Array<DropOperation>
-  getItems: (Set<Key>) => Array<DragItem>
+  getItems: (Set<Key>, Key) => Array<DragItem>
   isDisabled?: boolean
   onDragEnd?: (DraggableCollectionEndEvent) => void
   onDragMove?: (DraggableCollectionMoveEvent) => void
   onDragStart?: (DraggableCollectionStartEvent) => void
   selectionManager: MultipleSelectionManager
 }

@@ -65,7 +65,12 @@ export interface DragAndDropOptions extends Omit<DraggableCollectionProps, 'prev
* A function that returns the items being dragged. If not specified, we assume that the collection is not draggable.
* @default () => []
*/
getItems?: (keys: Set<Key>) => DragItem[],
getItems?: (
/** The set of keys that can be affected by the drag (e.g. the current selection). */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the individual ones are useful for autofill. But they still don't appear in our docs. Are you seeing them somewhere? maybe I'm just looking in the wrong place
Screenshot 2025-06-26 at 9 34 21 am
https://reactspectrum.blob.core.windows.net/reactspectrum/a00898e3b00bd2f2ad3954a86d8e8df764b7623b/docs/react-aria/useDraggableCollection.html

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just in VSCode, not seeing them in the docs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we should probably add it to the main description body for the prop as well. I think leave in the more granular ones for VSCode though as well

Copy link
Member

@LFDanLu LFDanLu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for mouse/touch drag, but it seems like keyboard DnD still moves all the selected items. I guess that might be expected? Feels a bit inconsistent though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom getKeysForDrag bug - Drag and drop selected items seperatly with GridList
4 participants