Skip to content

Commit

Permalink
map (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Jul 13, 2024
1 parent c9dbabb commit 37c48a3
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions Sources/SwiftUISupport/Components/Conditional.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import SwiftUI

extension View {

/// Applies the given transform if the given condition evaluates to `true`.
/// @see Referenced from: https://www.avanderlee.com/swiftui/conditional-view-modifier/
/// - Parameters:
/// - condition: The condition to evaluate.
/// - transform: The transform to apply to the source `View`.
/// - Returns: Either the original `View` or the modified `View` if the condition is `true`.
@ViewBuilder public func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View {
@ViewBuilder public consuming func `if`<Content: View>(
_ condition: Bool,
transform: (Self) -> Content
) -> some View {
if condition {
transform(self)
} else {
Expand All @@ -16,9 +20,29 @@ extension View {
}

@ViewBuilder
public func `do`<Content: View>(
public consuming func `do`<Content: View>(
@ViewBuilder transform: (Self) -> Content
) -> some View {
transform(self)
}

/**
```
Text("...")
.map { view in
if condition {
return view
} else {
return view
.padding()
}
}
```
*/
public consuming func map<Content: View>(
@ViewBuilder transform: (consuming Self) -> Content
) -> some View {
transform(self)
}

}

0 comments on commit 37c48a3

Please sign in to comment.