Popovers icon indicating copy to clipboard operation
Popovers copied to clipboard

Menu not working with ForEach

Open gdetari opened this issue 2 years ago • 2 comments

I have a dynamic number of items, and I want to show menu options for each. To simplify the case, consider the following code:

var body: some View {
  Templates.Menu() {
    ForEach(0..<3) { index in
      Templates.MenuButton(title: "Button \(index)") { print("Button \(index) pressed") }
    }
  }
  label: { fade in
    Text("Tap here")
  }
}

Only the first MenuButton will be clickable.

gdetari avatar Feb 11 '22 15:02 gdetari

Oof. Currently menus don't support ForEach yet since I'm extracting all the views inside Templates.Menu() { ... } into an array of views. The entire ForEach counts as a single view, which is why only the first one is clickable.

https://github.com/aheze/Popovers/blob/94647f2f17886153dd53543a59635d024684995a/Sources/Templates/Menu.swift#L125

        /**
         A built-from-scratch version of the system menu, for SwiftUI.
         This initializer lets you pass in a multiple menu items.
         */
        public init<Contents>(
            present: Binding<Bool> = .constant(false),
            configuration buildConfiguration: @escaping ((inout MenuConfiguration) -> Void) = { _ in },
            @ViewBuilder content: @escaping () -> TupleView<Contents>,
            @ViewBuilder label: @escaping (Bool) -> Label
        ) {
            _overridePresent = present

            var configuration = MenuConfiguration()
            buildConfiguration(&configuration)
            self.configuration = configuration
            self.content = ViewExtractor.getViews(from: content)
            self.label = label
        }

I'll see if I can find a fix.

aheze avatar Feb 12 '22 03:02 aheze

Any update for this issue ?

X901 avatar Mar 21 '23 09:03 X901