Skip to content

Commit 36c3e56

Browse files
committed
Impl View.if and View.ifLet conditional modifier application modifiers
1 parent bc8f318 commit 36c3e56

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
extension View {
2+
public func `if`<Result: View>(
3+
_ condition: Bool,
4+
apply modifier: (Self) -> Result
5+
) -> some View {
6+
if condition {
7+
EitherView<Self, Result>(modifier(self))
8+
} else {
9+
EitherView<Self, Result>(self)
10+
}
11+
}
12+
13+
public func ifLet<Value, Result: View>(
14+
_ value: Value?,
15+
apply modifier: (Self, Value) -> Result
16+
) -> some View {
17+
if let value {
18+
EitherView<Self, Result>(modifier(self, value))
19+
} else {
20+
EitherView<Self, Result>(self)
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)