Thank you for your response.
For now, I want to confirm that it works on the latest macOS, so I am using NavigationSplitView. Additionally, pressing the Return key plays the Alert Sound (the default macOS setting is Funky).
Here is all the code (I've omitted the item renaming function as it doesn't seem relevant to this behavior).
import SwiftUI
import SwiftData
@Model
final class Item {
let id = UUID()
var title: String
var index: Int
init(title: String, index: Int) {
self.title = title
self.index = index
}
}
@main
struct MyApp: App {
var body: some Scene {
WindowGroup { ContentView() }
.modelContainer(for: Item.self)
}
}
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query(sort: \Item.index, order: .forward) private var items: [Item]
var body: some View {
NavigationSplitView {
List {
ForEach(self.items) { item in
NavigationLink { Text(item.title) } label: { ItemView(item: item) }
}
.onMove(perform: self.moveItem)
}
.navigationSplitViewColumnWidth(min: 180, ideal: 200)
.toolbar {
ToolbarItem {
Button(action: addItem) { Label("Add Item", systemImage: "plus") }
}
}
} detail: {
Text("Select an item")
}
}
private func addItem() {
let newItem = Item(title: "New Title \(self.items.count)", index: self.items.count)
self.modelContext.insert(newItem)
}
private func moveItem(sources: IndexSet, destination: Int) {
var newItems = self.items
newItems.move(fromOffsets: sources, toOffset: destination)
newItems.enumerated().forEach { index, item in item.index = index }
try! self.modelContext.save()
}
}
struct ItemView: View {
@Bindable var item: Item
var body: some View {
NavigationLink {
Text(self.item.id.uuidString)
} label: {
TextField(text: self.$item.title) {}
}
}
}
#Preview {
ContentView().modelContainer(for: Item.self, inMemory: true)
}
Additionally, I'm attaching a video that shows the behavior immediately after clicking on each item to activate it and then pressing the Return key.
https://imgur.com/zeVkeGo