Last active
April 10, 2023 20:42
-
-
Save ylem/5013d9b1e3b7e48a53cc69f6c94eb0c2 to your computer and use it in GitHub Desktop.
InputTextView with indicator border
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
@available(iOS 15.0, *) | |
struct InputTextView: View { | |
private let placeholder: String? | |
private let text: Binding<String> | |
private let isError: Binding<Bool> | |
@FocusState private var isFocused: Bool | |
init( | |
placeholder: String? = nil, | |
text: Binding<String>, | |
isError: Binding<Bool> | |
) { | |
self.placeholder = placeholder | |
self.text = text | |
self.isError = isError | |
} | |
private var borderColor: Color { | |
if isFocused { | |
return Color.gray | |
} else if isError.wrappedValue { | |
return Color.red | |
} else { | |
return Color.blue | |
} | |
} | |
var body: some View { | |
TextField("", text: text) | |
.font(.body) | |
.padding(.vertical, 12) | |
.padding(.leading, 8) | |
.focused($isFocused) | |
.overlay(RoundedRectangle(cornerRadius: 4).stroke(borderColor, lineWidth:1)) | |
.padding(.bottom, 16) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment