bug to fix issue of the timing of the value being set
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
15b7e9be62
commit
557bf7d373
@ -216,9 +216,12 @@ extension InputField {
|
||||
return false
|
||||
}
|
||||
|
||||
// Set the value to the rawNumber, if you don't the onChange will trigger
|
||||
value = rawNumber
|
||||
|
||||
// Set the formatted text
|
||||
textField.text = formattedNumber
|
||||
|
||||
|
||||
// Calculate the new cursor position
|
||||
if let newPosition = textField.cursorPosition(range: range,
|
||||
replacementString: string,
|
||||
@ -227,9 +230,6 @@ extension InputField {
|
||||
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
|
||||
}
|
||||
|
||||
// if all passes, then set the number1
|
||||
value = rawNumber
|
||||
|
||||
// Prevent the default behavior
|
||||
return false
|
||||
}
|
||||
@ -252,11 +252,20 @@ extension InputField {
|
||||
internal func maskCreditCardNumber(_ cardType: CreditCardType, number: String) -> String {
|
||||
// Mask the first 12 characters if the length is 16
|
||||
let rawNumber = number.filter { $0.isNumber }
|
||||
guard rawNumber.count == cardType.maxLength else { return formatCreditCardNumber(cardType, number: number) }
|
||||
let count = rawNumber.count
|
||||
let min = cardType.minLength
|
||||
let max = cardType.maxLength
|
||||
var shouldFormat: Bool = false
|
||||
if min == max {
|
||||
shouldFormat = true
|
||||
} else {
|
||||
shouldFormat = count >= min && count <= max
|
||||
}
|
||||
guard shouldFormat else { return formatCreditCardNumber(cardType, number: number) }
|
||||
let lastFourDigits = rawNumber.suffix(4)
|
||||
let maskedSection = String(repeating: "•", count: number.count - lastFourDigits.count)
|
||||
let formattedMaskSection = String.format(maskedSection, indices: cardType.separatorIndices(rawNumber.count), with: " ")
|
||||
return formattedMaskSection + " " + lastFourDigits
|
||||
let formattedMaskSection = String.format(maskedSection + lastFourDigits, indices: cardType.separatorIndices(rawNumber.count), with: " ")
|
||||
return formattedMaskSection
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user