refactored out more code into enums

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-05-09 08:30:19 -05:00
parent 5672df7070
commit f93239b421
2 changed files with 40 additions and 35 deletions

View File

@ -9,10 +9,11 @@ import Foundation
import UIKit import UIKit
extension InputField { extension InputField {
public enum FieldType: String, CaseIterable { public enum FieldType: String, CaseIterable {
case text, number, inlineAction, password, creditCard, tel, date, securityCode case text, number, inlineAction, password, creditCard, tel, date, securityCode
public var keyboardType: UIKeyboardType { internal var keyboardType: UIKeyboardType {
switch self { switch self {
case .number: case .number:
.numberPad .numberPad
@ -28,5 +29,41 @@ extension InputField {
.default .default
} }
} }
internal func appendRules(for textField: InputField) {
switch self {
case .creditCard:
if let text = textField.text, text.count > 0 {
let rule = CharacterCountRule().copyWith {
$0.maxLength = textField.creditCardType.maxLength
$0.compareType = .equals
$0.errorMessage = "Enter a valid credit card."
}
textField.rules.append(.init(rule))
}
case .tel:
if let text = textField.text, text.count > 0 {
let rule = CharacterCountRule().copyWith {
$0.maxLength = "XXX-XXX-XXXX".count
$0.compareType = .equals
$0.errorMessage = "Enter a valid telephone."
}
textField.rules.append(.init(rule))
}
case .date:
if let text = textField.text, text.count > 0 {
let rule = CharacterCountRule().copyWith {
$0.maxLength = textField.dateFormat.maxLength
$0.compareType = .equals
$0.errorMessage = "Enter a valid date."
}
textField.rules.append(.init(rule))
}
default: break
}
}
} }
} }

View File

@ -312,7 +312,7 @@ open class InputField: EntryFieldBase {
//leftIcon //leftIcon
if let leftImageName { if let leftImageName {
leftImageView.image = BundleManager.shared.image(for: creditCardType.imageName)?.withTintColor(iconColorConfiguration.getColor(self)) leftImageView.image = BundleManager.shared.image(for: leftImageName)?.withTintColor(iconColorConfiguration.getColor(self))
} }
leftImageView.isHidden = leftImageName == nil leftImageView.isHidden = leftImageName == nil
@ -349,39 +349,7 @@ open class InputField: EntryFieldBase {
override func updateRules() { override func updateRules() {
super.updateRules() super.updateRules()
fieldType.appendRules(for: self)
switch fieldType {
case .creditCard:
if let text = textField.text, text.count > 0 {
let rule = CharacterCountRule().copyWith {
$0.maxLength = creditCardType.maxLength
$0.compareType = .equals
$0.errorMessage = "Enter a valid credit card."
}
rules.append(.init(rule))
}
case .tel:
if let text = textField.text, text.count > 0 {
let rule = CharacterCountRule().copyWith {
$0.maxLength = "XXX-XXX-XXXX".count
$0.compareType = .equals
$0.errorMessage = "Enter a valid telephone."
}
rules.append(.init(rule))
}
case .date:
if let text = textField.text, text.count > 0 {
let rule = CharacterCountRule().copyWith {
$0.maxLength = dateFormat.maxLength
$0.compareType = .equals
$0.errorMessage = "Enter a valid date."
}
rules.append(.init(rule))
}
default: break
}
} }
/// Used to update any Accessibility properties. /// Used to update any Accessibility properties.