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

Allow chaining into BindingViewState #2334

Merged
merged 1 commit into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/ComposableArchitecture/SwiftUI/Binding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ extension ViewStore where ViewAction: BindableAction, ViewAction.State == ViewSt
/// bindable in SwiftUI views.
///
/// Read <doc:Bindings> for more information.
@dynamicMemberLookup
@propertyWrapper
public struct BindingViewState<Value> {
let binding: Binding<Value>
Expand All @@ -376,6 +377,12 @@ public struct BindingViewState<Value> {
public var projectedValue: Binding<Value> {
self.binding
}

public subscript<Subject>(
dynamicMember keyPath: WritableKeyPath<Value, Subject>
) -> BindingViewState<Subject> {
BindingViewState<Subject>(binding: self.binding[dynamicMember: keyPath])
}
}

extension BindingViewState: Equatable where Value: Equatable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ final class BindingTests: BaseTCATestCase {
XCTAssertEqual(viewStore.state, .init(nested: .init(field: "Hello!")))
}

func testNestedBindingViewState() {
struct ViewState: Equatable {
@BindingViewState var field: String
}

let store = Store(initialState: BindingTest.State()) { BindingTest() }

let viewStore = ViewStore(store, observe: { ViewState(field: $0.$nested.field) })

viewStore.$field.wrappedValue = "Hello"

XCTAssertEqual(store.withState { $0.nested.field }, "Hello!")
}

func testBindingActionUpdatesRespectsPatternMatching() async {
let testStore = TestStore(
initialState: BindingTest.State(nested: BindingTest.State.Nested(field: ""))
Expand Down