diff --git a/Sources/TCAComposer/Macros/ComposeMacros.swift b/Sources/TCAComposer/Macros/ComposeMacros.swift index 1305704..b9838aa 100644 --- a/Sources/TCAComposer/Macros/ComposeMacros.swift +++ b/Sources/TCAComposer/Macros/ComposeMacros.swift @@ -182,8 +182,25 @@ public macro ComposeBodyActionAlertCase(_ name: String = "") = module: "TCAComposerMacros", type: "ComposeDirectiveMacro" ) +/// Specified the location in the `body` to attach the `.onChange()` modifier. +public enum ComposeBodyOnChangeAttachment { + // Attaches the `.onChange()` modifier to the `BindingReducer` + case binding + + // Attaches the `.onChange()` modifier to the reducer core. + case core + + // Attaches the `.onChange()` modifier to the `Scope` reducer for the specified child. + case scope(String) +} + +/// Adds an `onChange(of: ...)` modifier to the `body` of the Reducer. +/// - Parameters: +/// - of: A `KeyPath` of `State` to use in calling the `.onChange()` modifier +/// - attachment: Specified which Reducer in the `body` to attach the `.onChange()` to. By default it will be attached to the core. +/// @attached(peer) -public macro ComposeBodyOnChange(of keyPath: KeyPath) = +public macro ComposeBodyOnChange(of keyPath: KeyPath, attachment: ComposeBodyOnChangeAttachment = .core) = #externalMacro( module: "TCAComposerMacros", type: "ComposeDirectiveMacro" ) diff --git a/Sources/TCAComposerMacros/Composition.swift b/Sources/TCAComposerMacros/Composition.swift index f14d2b7..05620de 100644 --- a/Sources/TCAComposerMacros/Composition.swift +++ b/Sources/TCAComposerMacros/Composition.swift @@ -5,6 +5,7 @@ import SwiftSyntaxBuilder import SwiftSyntaxMacroExpansion import SwiftSyntaxMacros import XCTestDynamicOverlay +import OrderedCollections class Composition { var options = Set