Skip to content

Instantly share code, notes, and snippets.

@disc0infern0
Created October 30, 2021 10:41
Show Gist options
  • Save disc0infern0/51b7143b254a349988d897deb6776af9 to your computer and use it in GitHub Desktop.
Save disc0infern0/51b7143b254a349988d897deb6776af9 to your computer and use it in GitHub Desktop.
View extension to mirror focus state between a view and view model
extension View {
/// Mirror changes between an @Published variable (typically in your View Model) and an @FocusedState variable in a view
func sync<T: Equatable>(_ field1: Binding<T>, _ field2: FocusState<T>.Binding ) -> some View {
self
.onChange(of: field1.wrappedValue) { field2.wrappedValue = $0 }
.onChange(of: field2.wrappedValue) { field1.wrappedValue = $0 }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment