Does anyone KNOW if SwiftUI now supports drag and drop in a list? It seems like it was only working in ForEach before the latest version. Now, this tries to work, but hangs.
List(sidebars.sorted{$0.index < $1.index}[0].children!, id:\.id, children: \.children, selection: $selected) { sidebar in
NavigationLink {
SidebarDetailSelectorView(sidebar: sidebar)
} label: {
rowLabel(sidebar: sidebar)
//.background(Color.random())
}
.onChange(of: selected) {
currentSidebar.sidebar = sidebars.first{ $0.id == selected }
currentSidebar.editSidebar = currentSidebar.sidebar
}
.draggable(sidebar)
.dropDestination(for: Sidebar.self) { sidebars, location in
print("\(sidebars[0].title) -> \(sidebar.title)")
return true
}
}
I want a hierarchical sidebar (like finder or mail) to be able to drag and drop within the list to reorder it. I can get it working with DisclosureGroups. The parent items can be dragged or dropped on but are not selectable - which I need. Here is an example of my recursive DisclusureGroup implementation using ForEach loops.
struct MenuItemView: View {
let item: MenuItem
var body: some View {
if let children = item.children, children.isEmpty == false {
DisclosureGroup(item.text) {
ForEach(children, id: \.id) { childItem in
MenuItemView(item: childItem)
.draggable(childItem)
.dropDestination(for: MenuItem.self) { items, location in
print("\(items[0].text) -> \(childItem.text)")
return true
} isTargeted: { isTargeted in
item.isTargeted = isTargeted
}
}
}
} else {
VStack {
NavigationLink(item.text, destination: Text(item.text))
.foregroundColor(item.isTargeted ? .teal : Color(.selectedTextColor ))
.draggable(item)
.dropDestination(for: MenuItem.self) { items, location in
print("\(items[0].text) -> \(item.text)")
return true
} isTargeted: { isTargeted in
item.isTargeted = isTargeted
}
}
}
}
}