Skip to content

Commit

Permalink
Support foregroundColor for TextField (#453)
Browse files Browse the repository at this point in the history
* Support adjusting TextField foregroundColor in TokamakDOM

* Simplify implementation of foregroundColor
  • Loading branch information
carson-katri committed May 28, 2022
1 parent c0c4534 commit 355c880
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 8 additions & 2 deletions Sources/TokamakCore/Views/Text/TextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public struct TextField<Label>: _PrimitiveView where Label: View {
let textBinding: Binding<String>
let onEditingChanged: (Bool) -> ()
let onCommit: () -> ()
@Environment(\.textFieldStyle) var style
@Environment(\.self) var environment
}

public extension TextField where Label == Text {
Expand Down Expand Up @@ -80,5 +80,11 @@ public struct _TextFieldProxy<Label: View> {
public var textBinding: Binding<String> { subject.textBinding }
public var onCommit: () -> () { subject.onCommit }
public var onEditingChanged: (Bool) -> () { subject.onEditingChanged }
public var textFieldStyle: _AnyTextFieldStyle { subject.style }
public var textFieldStyle: _AnyTextFieldStyle { subject.environment.textFieldStyle }
public var foregroundColor: AnyColorBox.ResolvedValue? {
guard let foregroundColor = subject.environment.foregroundColor else {
return nil
}
return _ColorProxy(foregroundColor).resolve(in: subject.environment)
}
}
5 changes: 4 additions & 1 deletion Sources/TokamakDOM/Views/Text/TextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ extension TextField: DOMPrimitive where Label == Text {
"type": proxy.textFieldStyle is RoundedBorderTextFieldStyle ? "search" : "text",
.value: proxy.textBinding.wrappedValue,
"placeholder": _TextProxy(proxy.label).rawText,
"style": css(for: proxy.textFieldStyle),
"style": """
\(css(for: proxy.textFieldStyle)) \
\(proxy.foregroundColor.map { "color: \($0.cssValue);" } ?? "")
""",
"class": className(for: proxy.textFieldStyle),
], listeners: [
"focus": { _ in proxy.onEditingChanged(true) },
Expand Down

0 comments on commit 355c880

Please sign in to comment.