I'm trying to implement a sidebar similar to those in native Apple apps like Finder or Notes, where items can be rearranged and renamed.
struct SidebarListView: View {
var items: [Item] // class using SwiftData
var body: some View {
List {
ForEach(self.items) { item in
ItemView(item: item)
}
.onMove(perform: self.moveItem)
}
}
// Additional code...
}
struct ItemView: View {
var item: Item
@State private var newTitle: String = ""
var body: some View {
NavigationLink {
Text(self.item.title)
} label: {
Label {
TextField(text: self.$newTitle) {}
.onSubmit(self.renameItem)
} icon: { Image(systemName: "folder") }
}
}
// Additional code...
}
In my sidebar, each item is a NavigationLink containing a TextField. I've managed to replicate the following behaviors seen in native apps:
When an item is active, clicking on the item name once allows it to be renamed.
When an item is active, pressing the Return key allows it to be renamed.
However, after rearranging the items, the ability to rename an item with the Return key (behavior 2) stops working.
Given that these are fundamental UI behaviors in Mac apps, I believe they should be straightforward to implement in SwiftUI. However, I couldn't find any official documentation on placing a TextField inside a NavigationLink and having it focus with the Return key (even before rearranging items).
Does anyone have any insights or suggestions on this issue?