I am trying to make a textfield that can do its own validation for input using the new Regex APIs. For example, if I pass in the email regex, it can have a green border when its a valid email and red border otherwise. So far I have this:
struct MyTextField: View {
private var placeholder: String
@Binding private var text: String
private var regex: (any RegexComponent)?
var body: some View {
TextField(placeholder, text: $text)
.onChange(of: $text) { newValue in
guard let regex = regex else {
return
}
if text.matches(of: regex).isEmpty {
// show invalid state
} else {
// show valid state
}
}
}
However I get Command SwiftCompile failed with a nonzero exit code due to a type checking error. What is the correct type to use here?