Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using TCACoordinators within Lists #43

Open
silverfoxlabs opened this issue Jan 22, 2023 · 1 comment
Open

Using TCACoordinators within Lists #43

silverfoxlabs opened this issue Jan 22, 2023 · 1 comment

Comments

@silverfoxlabs
Copy link

Hi, thank you for taking the time to create this library. I have been reading through the documentation however, it's not clear to me how to achieve an example of using a List view type that has tappable rows (that aren't buttons). Normally, using a NavigationLink inside of a List type affords some extra functionality such as highlighting the row, selection and having the chevron icon to indicate that the row is tappable and will push the next screen.

Looking at your examples, there are Button view types nested in a List to achieve this functionality. I might be missing something if you can clarify this use case for me.

@johnpatrickmorgan
Copy link
Owner

Hi @silverfoxlabs, thanks for raising this issue. Sadly NavigationLink doesn't offer a way to override its action, and Button doesn't offer a way to style it like a NavigationLink, so the best I can offer are workarounds.

The following gets you most of the styling, but the row background selection state doesn't persist:

public struct NavigationButton<Label: View>: View {
  let action: () -> Void
  let label: Label
  
  public init(action: @escaping () -> Void, @ViewBuilder label: () -> Label) {
    self.action = action
    self.label = label()
  }
  
  public var body: some View {
    ZStack {
      NavigationLink(isActive: .constant(false), destination: { EmptyView() }, label: { label })
      Button(action: action, label: { EmptyView() })
    }
  }
}

public extension NavigationButton where Label == Text {
  init(_ titleKey: LocalizedStringKey, action: @escaping () -> Void) {
    self.init(action: action, label: { Text(titleKey) })
  }
  init<S>(_ title: S, action: @escaping () -> Void) where S : StringProtocol {
    self.init(action: action, label: { Text(title) })
  }
}

There's some interesting discussion of these shortcomings and possible workarounds in this TCA discussion, including this gist from @tgrapperon which offers a more faithful recreation of NavigationLink's list style.

I'd be interested to hear how you find these workarounds and if you think I should add something like that to this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants