Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Fix #451 no flag change for different regionCode (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxZheleznyy authored Aug 18, 2022
1 parent 104a53d commit d8c0069
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions PhoneNumberKit/UI/PhoneNumberTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ open class PhoneNumberTextField: UITextField, UITextFieldDelegate {

open func updateFlag() {
guard self.withFlag else { return }
if let phoneNumber = phoneNumber, let regionCode = phoneNumberKit.getRegionCode(of: phoneNumber) {
_defaultRegion = regionCode
partialFormatter.defaultRegion = regionCode
}

let flagBase = UnicodeScalar("🇦").value - UnicodeScalar("A").value

let flag = self.currentRegion
Expand Down
21 changes: 21 additions & 0 deletions PhoneNumberKitTests/PhoneNumberTextFieldTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ class PhoneNumberTextFieldTests: XCTestCase {
tf.text = ""
XCTAssertNil(tf.phoneNumber)
}


func testUSPhoneNumberWithFlag() {
let pnk = PhoneNumberKit()
let tf = PhoneNumberTextField(withPhoneNumberKit: pnk)
tf.partialFormatter.defaultRegion = "US"
tf.withFlag = true
tf.text = "4125551212"
XCTAssertNotNil(tf.flagButton)
XCTAssertEqual(tf.flagButton.titleLabel?.text, "🇺🇸 ")
}

func testNonUSPhoneNumberWithFlag() {
let pnk = PhoneNumberKit()
let tf = PhoneNumberTextField(withPhoneNumberKit: pnk)
tf.partialFormatter.defaultRegion = "US"
tf.withFlag = true
tf.text = "5872170177"
XCTAssertNotNil(tf.flagButton)
XCTAssertEqual(tf.flagButton.titleLabel?.text, "🇨🇦 ")
}
}

#endif

0 comments on commit d8c0069

Please sign in to comment.