Created
July 2, 2020 08:57
-
-
Save Gujci/71eba08ec067e904f6e4b2a00f2af43d to your computer and use it in GitHub Desktop.
SwiftUI wrapper around the STPPaymentCardTextField from Stripe
This file contains 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
struct StripePaymentCardTextField: UIViewRepresentable { | |
@Binding var cardParams: STPPaymentMethodCardParams | |
@Binding var isValid: Bool | |
func makeUIView(context: Context) -> STPPaymentCardTextField { | |
let input = STPPaymentCardTextField() | |
input.borderWidth = 0 | |
input.delegate = context.coordinator | |
return input | |
} | |
func makeCoordinator() -> StripePaymentCardTextField.Coordinator { Coordinator(self) } | |
func updateUIView(_ view: STPPaymentCardTextField, context: Context) { } | |
class Coordinator: NSObject, STPPaymentCardTextFieldDelegate { | |
var parent: StripePaymentCardTextField | |
init(_ textField: StripePaymentCardTextField) { | |
parent = textField | |
} | |
func paymentCardTextFieldDidChange(_ textField: STPPaymentCardTextField) { | |
parent.cardParams = textField.cardParams | |
parent.isValid = textField.isValid | |
} | |
} | |
} | |
// MARK: - Preview | |
struct StripePaymentCardTextField_Previews: PreviewProvider { | |
static var previews: some View { | |
Group { | |
VStack { | |
StripePaymentCardTextField(cardParams: .constant(STPPaymentMethodCardParams()), isValid: .constant(true)) | |
} | |
VStack { | |
StripePaymentCardTextField(cardParams: .constant(STPPaymentMethodCardParams()), isValid: .constant(true)) | |
} | |
.environment(\.colorScheme, .dark) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment