Created
October 30, 2021 10:41
-
-
Save disc0infern0/51b7143b254a349988d897deb6776af9 to your computer and use it in GitHub Desktop.
View extension to mirror focus state between a view and view model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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