Animation for DisclosureGroup open/close

A disclosure group in a list animates the open close when you tap on the > icon. I need to support tapping on the entire title label. This code works below, but the tapGesture on the label does not do any animation like the > button does. Is there some way to have DisclosureGroup use the same animation as it does itself?

Code Block
@State var guestsExpanded: Bool = true
var body: some View
{
List()
{
DisclosureGroup(isExpanded: $guestsExpanded, content:
{
ForEach( model.guests ) { guest in GuestView(guest: guest, selectedGuests: $selectedGuests) }
}, label: { Text("Guests").onTapGesture { guestsExpanded = !guestsExpanded } })
...
}
}

I’ve got a way for you to be able to expand the DisclosureGroup by tapping either on the label or the arrow but not the row as a whole.

Code Block Swift
DisclosureGroup(isExpanded: $guestsExpanded) {
ForEach(model.guests) { ... }
} label: {
Button("Guests") {
withAnimation {
guestsExpanded.toggle()
}
}
.foregroundColor(.primary)
}


I am have also encountered this strange behaviour of having to tap only on the small arrow to toggle the DisclosureGroup.

This works for the whole label, but still can't get the animation:

Code Block
, label:
{
HStack
{
Text("Guests")
.padding(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8))
.font(.headline)
}
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
.onTapGesture { guestsExpanded = !guestsExpanded }
}


This should be fixed in another beta because I’ve seen DisclosureGroups in Apple’s own apps be expandable by tapping anywhere in the row. Hopefully, this isn’t its intended behaviour in SwiftUI.
Hi,

Perhaps wrap it with a withAnimation{} block:

.onTapGesture {
withAnimation { guestsExpanded.toggle() }
}

Thanks
Building on @BabyJ's answer, here's how you can get the entire row to be tappable:

Code Block Swift
DisclosureGroup(isExpanded: $guestsExpanded){
ForEach(model.guests) { ... }
} label: {
HStack {
Button("Guests") {
withAnimation {
guestsExpanded.toggle()
}
}
.foregroundColor(.primary)
Spacer()
}
.contentShape(Rectangle())
}


I can confirm that @lcurry's solution work.
Also, I put the onTapGesture on the DisclosureGroup, not on the Text label.

Code Block swift
@State var isGuestsExpanded: Bool = true
var body: some View {
List() {
DisclosureGroup(isExpanded: $isGuestsExpanded, content: {
ForEach( model.guests ) { guest in GuestView(guest: guest, selectedGuests: $selectedGuests) }
}, label: {
Text("Guests")
}.onTapGesture {
withAnimation { isGuestsExpanded.toggle() }
}
}
}


Animation for DisclosureGroup open/close
 
 
Q