onDelete confirmation for onDelete
derech1e opened this issue · 1 comments
derech1e commented
Can you add a feature, that you can confirm a delete action?
Thank you.
chris-swift-dev commented
Hey @derech1e ,
on iOS < 15 you have to use
@State private var deletedIndexSet: IndexSet?
@State private var isDeleteConfirmationDialogPresented = false
yourAdvancedList
.onDelete { indexSet in
deletedIndexSet = indexSet
isDeleteConfirmationDialogPresented = true
}
.alert(isPresented: $isDeleteConfirmationDialogPresented) {
Alert(
title: Text("Delete"),
message: Text("Are you sure?"),
primaryButton: .destructive(Text("Yes"), action: {
guard let indexSet = deletedIndexSet else {
return
}
yourItems.remove(atOffsets: indexSet)
}),
secondaryButton: .cancel()
)
}
For iOS 15 I recommend the following
@State private var deletedIndexSet: IndexSet?
@State private var isDeleteConfirmationDialogPresented = false
AdvancedList(yourItems, content: { item in
view(for: item)
.swipeActions {
Button(role: .destructive) {
guard let index = yourItems.firstIndex(where: { $0.id == item.id }) else {
return
}
deletedIndexSet = .init(integer: index)
isDeleteConfirmationDialogPresented = true
} label: {
Text("Delete")
}
}
}, listState: yourListState, emptyStateView: {
...
}, errorStateView: { error in
...
}, loadingStateView: {
...
})
.confirmationDialog(Text("Are you sure?"), isPresented: $isDeleteConfirmationDialogPresented) {
Button(role: .destructive) {
guard let indexSet = deletedIndexSet else {
return
}
yourItems.remove(atOffsets: indexSet)
} label: {
Text("Delete")
}
}