mirror of
https://github.com/yattee/yattee.git
synced 2026-02-20 09:49:46 +00:00
Yattee v2 rewrite
This commit is contained in:
50
Yattee/Extensions/View+Conditional.swift
Normal file
50
Yattee/Extensions/View+Conditional.swift
Normal file
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// View+Conditional.swift
|
||||
// Yattee
|
||||
//
|
||||
// Conditional view modifier extension.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
extension View {
|
||||
/// Conditionally applies a transformation to the view.
|
||||
@ViewBuilder
|
||||
func `if`<Transform: View>(
|
||||
_ condition: Bool,
|
||||
transform: (Self) -> Transform
|
||||
) -> some View {
|
||||
if condition {
|
||||
transform(self)
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Conditionally applies one of two transformations.
|
||||
@ViewBuilder
|
||||
func `if`<TrueContent: View, FalseContent: View>(
|
||||
_ condition: Bool,
|
||||
then trueTransform: (Self) -> TrueContent,
|
||||
else falseTransform: (Self) -> FalseContent
|
||||
) -> some View {
|
||||
if condition {
|
||||
trueTransform(self)
|
||||
} else {
|
||||
falseTransform(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Conditionally applies a transformation when an optional value is present.
|
||||
@ViewBuilder
|
||||
func ifLet<T, Transform: View>(
|
||||
_ value: T?,
|
||||
transform: (Self, T) -> Transform
|
||||
) -> some View {
|
||||
if let value {
|
||||
transform(self, value)
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user