Skip to content

Commit

Permalink
utility update
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Mar 7, 2024
1 parent 54a03ee commit e7ab6ee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sources/UIComponent/Core/Model/Component/Component+Modifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,24 @@ public extension Component {
func environment<Value>(_ keyType: any EnvironmentKey<Value>.Type, value: Value) -> EnvironmentComponent<Value, Self> {
EnvironmentComponent(keyType: keyType, value: value, content: self)
}

/// Applies an environment value to the component for the given key path.
/// - Parameters:
/// - keyPath: A key path to a specific environment value.
/// - valueBuilder: A closure to be called to generate the value to set for the environment key.
/// - Returns: An `EnvironmentComponent` that provides the environment value to the component.
func environment<Value>(_ keyPath: WritableKeyPath<EnvironmentValues, Value>, valueBuilder: () -> Value) -> EnvironmentComponent<Value, Self> {
EnvironmentComponent(keyPath: keyPath, value: valueBuilder(), content: self)
}

/// Applies an environment value to the component for the given environment key type.
/// - Parameters:
/// - keyType: The type of the environment key.
/// - valueBuilder: A closure to be called to generate the value to set for the environment key.
/// - Returns: An `EnvironmentComponent` that provides the environment value to the component.
func environment<Value>(_ keyType: any EnvironmentKey<Value>.Type, valueBuilder: () -> Value) -> EnvironmentComponent<Value, Self> {
EnvironmentComponent(keyType: keyType, value: valueBuilder(), content: self)
}
}

// MARK: - Type erase modifiers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public struct ComponentArrayBuilder {
public static func buildExpression(_ expression: any Component) -> [any Component] {
[expression]
}
public static func buildExpression(_ expression: (any Component)?) -> [any Component] {
[expression].compactMap { $0 }
}
public static func buildExpression(_ expression: [any Component]) -> [any Component] {
expression
}
Expand Down

0 comments on commit e7ab6ee

Please sign in to comment.