@@ -64,49 +64,49 @@ import SwiftUI
6464/// }
6565///
6666/// This is a _lot_ of boilerplate for something that should be simple. Luckily, we can dramatically
67- /// eliminate this boilerplate using `FormAction `. First, we can collapse all of these
68- /// field-mutating actions into a single case that holds a `FormAction ` generic over the reducer's
69- /// root `SettingsState`:
67+ /// eliminate this boilerplate using `BindingAction `. First, we can collapse all of these
68+ /// field-mutating actions into a single case that holds a `BindingAction ` generic over the
69+ /// reducer's root `SettingsState`:
7070///
7171/// enum SettingsAction {
72- /// case form(FormAction <SettingsState>)
72+ /// case binding(BindingAction <SettingsState>)
7373/// }
7474///
75- /// And then, we can simplify the settings reducer by allowing the `form ` method to handle these
75+ /// And then, we can simplify the settings reducer by allowing the `binding ` method to handle these
7676/// field mutations for us:
7777///
7878/// let settingsReducer = Reducer<
7979/// SettingsState, SettingsAction, SettingsEnvironment
8080/// > {
8181/// switch action {
82- /// case .form :
82+ /// case .binding :
8383/// return .none
8484/// }
8585/// }
86- /// .form (action: /SettingsAction.form )
86+ /// .binding (action: /SettingsAction.binding )
8787///
88- /// Form actions are constructed and sent to the store by providing a writable key path from root
88+ /// Binding actions are constructed and sent to the store by providing a writable key path from root
8989/// state to the field being mutated. There is even a view store helper that simplifies this work.
90- /// You can derive a binding by specifying the key path and form action case:
90+ /// You can derive a binding by specifying the key path and binding action case:
9191///
9292/// TextField(
9393/// "Display name",
94- /// text: viewStore.binding(keyPath: \.displayName, send: SettingsAction.form )
94+ /// text: viewStore.binding(keyPath: \.displayName, send: SettingsAction.binding )
9595/// )
9696///
97- /// Should you need to layer additional functionality over your form , your reducer can pattern match
98- /// the form action for a given key path:
97+ /// Should you need to layer additional functionality over these bindings , your reducer can pattern
98+ /// match the action for a given key path:
9999///
100- /// case .form (\.displayName):
100+ /// case .binding (\.displayName):
101101/// // Validate display name
102102///
103- /// case .form (\.enableNotifications):
103+ /// case .binding (\.enableNotifications):
104104/// // Return an authorization request effect
105105///
106- /// Form actions can also be tested in much the same way regular actions are tested. Rather than
106+ /// Binding actions can also be tested in much the same way regular actions are tested. Rather than
107107/// send a specific action describing how a binding changed, such as `displayNameChanged("Blob")`,
108- /// you will send a `.form ` action that describes which key path is being set to what value, such
109- /// as `.form (.set(\.displayName, "Blob"))`:
108+ /// you will send a `.binding ` action that describes which key path is being set to what value, such
109+ /// as `.binding (.set(\.displayName, "Blob"))`:
110110///
111111/// let store = TestStore(
112112/// initialState: SettingsState(),
@@ -115,15 +115,15 @@ import SwiftUI
115115/// )
116116///
117117/// store.assert(
118- /// .send(.form (.set(\.displayName, "Blob"))) {
118+ /// .send(.binding (.set(\.displayName, "Blob"))) {
119119/// $0.displayName = "Blob"
120120/// },
121- /// .send(.form (.set(\.protectMyPosts, true))) {
121+ /// .send(.binding (.set(\.protectMyPosts, true))) {
122122/// $0.protectMyPosts = true
123123/// )
124124/// )
125125///
126- public struct FormAction < Root> : Equatable {
126+ public struct BindingAction < Root> : Equatable {
127127 public let keyPath : PartialKeyPath < Root >
128128
129129 fileprivate let set : ( inout Root ) -> Void
@@ -150,12 +150,14 @@ public struct FormAction<Root>: Equatable {
150150 )
151151 }
152152
153- /// Transforms a form action over some root state to some other type of root state given a key
153+ /// Transforms a binding action over some root state to some other type of root state given a key
154154 /// path.
155155 ///
156156 /// - Parameter keyPath: A key path from a new type of root state to the original root state.
157- /// - Returns: A form action over a new type of root state.
158- public func pullback< NewRoot> ( _ keyPath: WritableKeyPath < NewRoot , Root > ) -> FormAction < NewRoot > {
157+ /// - Returns: A binding action over a new type of root state.
158+ public func pullback< NewRoot> (
159+ _ keyPath: WritableKeyPath < NewRoot , Root >
160+ ) -> BindingAction < NewRoot > {
159161 . init(
160162 keyPath: ( keyPath as AnyKeyPath ) . appending ( path: self . keyPath) as! PartialKeyPath < NewRoot > ,
161163 set: { self . set ( & $0[ keyPath: keyPath] ) } ,
@@ -170,38 +172,39 @@ public struct FormAction<Root>: Equatable {
170172
171173 public static func ~= < Value> (
172174 keyPath: WritableKeyPath < Root , Value > ,
173- formAction : FormAction < Root >
175+ bindingAction : Self
174176 ) -> Bool {
175- keyPath == formAction . keyPath
177+ keyPath == bindingAction . keyPath
176178 }
177179}
178180
179181extension Reducer {
180- /// Returns a reducer that applies `FormAction ` mutations to `State` before running this reducer's
181- /// logic.
182+ /// Returns a reducer that applies `BindingAction ` mutations to `State` before running this
183+ /// reducer's logic.
182184 ///
183- /// For example, a settings screen may gather its form actions into a single `FormAction` case:
185+ /// For example, a settings screen may gather its binding actions into a single `BindingAction`
186+ /// case:
184187 ///
185188 /// enum SettingsAction {
186189 /// ...
187- /// case form(FormAction <SettingsState>)
190+ /// case binding(BindingAction <SettingsState>)
188191 /// }
189192 ///
190193 /// The reducer can then be enhanced to automatically handle these mutations for you by tacking on
191- /// the `form ` method:
194+ /// the `binding ` method:
192195 ///
193196 /// let settingsReducer = Reducer<SettingsState, SettingsAction, SettingsEnvironment {
194197 /// ...
195198 /// }
196- /// .form (action: /SettingsAction.form )
199+ /// .binding (action: /SettingsAction.binding )
197200 ///
198- /// - Parameter toFormAction : A case path from this reducer's `Action` type to a `FormAction` over
199- /// this reducer's `State`.
200- /// - Returns: A reducer that applies `FormAction ` mutations to `State` before running this
201+ /// - Parameter toBindingAction : A case path from this reducer's `Action` type to a
202+ /// `BindingAction` over this reducer's `State`.
203+ /// - Returns: A reducer that applies `BindingAction ` mutations to `State` before running this
201204 /// reducer's logic.
202- public func form ( action toFormAction : CasePath < Action , FormAction < State > > ) -> Self {
205+ public func binding ( action toBindingAction : CasePath < Action , BindingAction < State > > ) -> Self {
203206 Self { state, action, environment in
204- toFormAction . extract ( from: action) ? . set ( & state)
207+ toBindingAction . extract ( from: action) ? . set ( & state)
205208 return . none
206209 }
207210 . combined ( with: self )
@@ -210,25 +213,25 @@ extension Reducer {
210213
211214extension ViewStore {
212215 /// Derives a binding from the store that mutates state at the given writable key path by wrapping
213- /// a `FormAction ` with the store's action type.
216+ /// a `BindingAction ` with the store's action type.
214217 ///
215218 /// For example, a text field binding can be created like this:
216219 ///
217220 /// struct State { var text = "" }
218- /// enum Action { case form(FormAction <State>) }
221+ /// enum Action { case binding(BindingAction <State>) }
219222 ///
220223 /// TextField(
221224 /// "Enter text",
222- /// text: viewStore.binding(keyPath: \.text, Action.form )
225+ /// text: viewStore.binding(keyPath: \.text, Action.binding )
223226 /// )
224227 ///
225228 /// - Parameters:
226229 /// - keyPath: A writable key path from the view store's state to a mutable field
227- /// - action: A function that wraps a form action in the view store's action type.
230+ /// - action: A function that wraps a binding action in the view store's action type.
228231 /// - Returns: A binding.
229232 public func binding< LocalState> (
230233 keyPath: WritableKeyPath < State , LocalState > ,
231- send action: @escaping ( FormAction < State > ) -> Action
234+ send action: @escaping ( BindingAction < State > ) -> Action
232235 ) -> Binding < LocalState >
233236 where LocalState: Equatable {
234237 self . binding (
0 commit comments