Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add separated code and number bindings #91

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/iPhoneNumberField/iPhoneNumberField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public struct iPhoneNumberField: UIViewRepresentable {
/// The formatted phone number `String`.
/// This variable writes to the binding provided in the initializer.
@Binding public var text: String
@Binding public var code: String
@Binding public var number: String
@State private var displayedText: String

/// Whether or not the phone number field is editing.
Expand Down Expand Up @@ -134,6 +136,8 @@ public struct iPhoneNumberField: UIViewRepresentable {

public init(_ title: String? = nil,
text: Binding<String>,
code: Binding<String>,
number: Binding<String>,
isEditing: Binding<Bool>? = nil,
formatted: Bool = true,
configuration: @escaping (UIViewType) -> () = { _ in } ) {
Expand All @@ -142,6 +146,8 @@ public struct iPhoneNumberField: UIViewRepresentable {
self.externalIsFirstResponder = isEditing
self.formatted = formatted
self._text = text
self._code = code
self._number = number
self._displayedText = State(initialValue: text.wrappedValue)
self.configuration = configuration
}
Expand Down Expand Up @@ -269,6 +275,8 @@ public struct iPhoneNumberField: UIViewRepresentable {
let country = String(number.countryCode)
let nationalNumber = String(number.nationalNumber)
text.wrappedValue = "+" + country + nationalNumber
code.wrappedValue = country
number.wrappedValue = number
} else {
// Otherwise, maintain an empty string
text.wrappedValue = ""
Expand Down