I'm unable to find the right combination modifiers to get drag and drop to work using the new .draggable(containerItemID:)
and dragContainer(for:in:selection:_:)
modifiers. The drag is initiated with the item's ID, the item is requested from the .dragContainer
modifier, but the drop closure is never triggered.
Minimal repro:
struct Item: Identifiable, Codable, Transferable {
var id = UUID()
var value: String
static var transferRepresentation: some TransferRepresentation {
CodableRepresentation(contentType: .tab)
}
}
struct DragDrop: View {
@State var items: [Item] = [
Item(value: "Hello"),
Item(value: "world"),
Item(value: "something"),
Item(value: "else")
]
var body: some View {
List(items) { item in
HStack {
Text(item.value)
Spacer()
}
.contentShape(Rectangle())
.draggable(containerItemID: item.id)
.dropDestination(for: Item.self) { items, session in
print("Drop: \(items)")
}
}
.dragContainer(for: Item.self) { itemID in
print("Drag: \(itemID)")
return items.filter { itemID == $0.id }
}
}
}
#Preview("Simple") {
DragDrop()
}