vds_ios/VDS/Components/TextFields/TextEntryField/TextEntryField.swift
Matt Bruce ae6c380627 removed old code
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-12-08 19:34:04 -06:00

210 lines
6.1 KiB
Swift

//
// TextEntryField.swift
// VDS
//
// Created by Matt Bruce on 10/3/22.
//
import Foundation
import UIKit
import VDSColorTokens
import VDSFormControlsTokens
import Combine
public enum TextEntryFieldType: String, CaseIterable {
case text, number, calendar, inlineAction, password, creditCard, tel, date, securityCode
}
@objc(VDSTextEntryField)
public class TextEntryField: TextEntryFieldBase{}
open class TextEntryFieldBase: EntryField {
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
required public init() {
super.init(frame: .zero)
}
public override init(frame: CGRect) {
super.init(frame: .zero)
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
}
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
internal var containerStackView: UIStackView = {
return UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.axis = .horizontal
$0.distribution = .fill
$0.spacing = 12
}
}()
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
open var type: TextEntryFieldType = .text { didSet { didChange() }}
var _showError: Bool = false
open override var showError: Bool {
get { _showError }
set {
if !showSuccess && _showError != newValue {
_showError = newValue
didChange()
}
}
}
var _showSuccess: Bool = false
open var showSuccess: Bool {
get { _showSuccess }
set {
if !showError && _showSuccess != newValue {
_showSuccess = newValue
didChange()
}
}
}
open override var state: UIControl.State {
get {
var state = super.state
if showSuccess {
state.insert(.success)
}
return state
}
}
open var successText: String? { didSet { didChange() }}
open var helperTextPlacement: HelperTextPlacement = .bottom { didSet { didChange() }}
private var successLabel = Label().with {
$0.setContentCompressionResistancePriority(.required, for: .vertical)
$0.textPosition = .left
$0.typograpicalStyle = .BodySmall
}
internal var minWidthConstraint: NSLayoutConstraint?
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
open override func setup() {
super.setup()
minWidthConstraint = containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: 0)
minWidthConstraint?.isActive = true
stackView.addArrangedSubview(successLabel)
stackView.setCustomSpacing(8, after: successLabel)
successLabel.textColorConfiguration = primaryColorConfig.eraseToAnyColorable()
backgroundColorConfiguration.setSurfaceColors(VDSColor.feedbackSuccessBackgroundOnlight, VDSColor.feedbackSuccessBackgroundOndark, forState: .success)
borderColorConfiguration.setSurfaceColors(VDSColor.feedbackSuccessOnlight, VDSColor.feedbackSuccessOndark, forState: .success)
}
public override func reset() {
super.reset()
successLabel.reset()
successLabel.textPosition = .left
successLabel.typograpicalStyle = .BodySmall
type = .text
showSuccess = false
successText = nil
helperTextPlacement = .bottom
}
open override func getContainer() -> UIView {
containerStackView.addArrangedSubview(containerView)
return containerStackView
}
//--------------------------------------------------
// MARK: - State
//--------------------------------------------------
open override func updateView() {
super.updateView()
//show error or success
if showError, let _ = errorText {
successLabel.isHidden = true
} else if showSuccess, let successText {
successLabel.text = successText
successLabel.surface = surface
successLabel.disabled = disabled
successLabel.isHidden = false
errorLabel.isHidden = true
} else {
successLabel.isHidden = true
}
//set the width constraints
if let width, width > type.width {
widthConstraint?.constant = width
widthConstraint?.isActive = true
minWidthConstraint?.isActive = false
} else {
minWidthConstraint?.constant = type.width
widthConstraint?.isActive = false
minWidthConstraint?.isActive = true
}
}
open override func updateHelperLabel(){
//remove first
helperLabel.removeFromSuperview()
super.updateHelperLabel()
//set the helper label position
if helperText != nil {
if helperTextPlacement == .right {
containerStackView.spacing = 12
containerStackView.distribution = .fillEqually
containerStackView.addArrangedSubview(helperLabel)
} else {
containerStackView.spacing = 0
containerStackView.distribution = .fill
stackView.addArrangedSubview(helperLabel)
}
}
}
}
extension TextEntryFieldType {
var width: CGFloat {
switch self {
case .inlineAction:
return 102
case .password:
return 62.0
case .creditCard:
return 288.0
case .tel:
return 176.0
case .date:
return 114.0
case .securityCode:
return 88.0
default:
return 40.0
}
}
}