From d3f4f21b02dc3ace1deadc13cfdedb2c34406941 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 11 Jan 2022 15:51:57 -0600 Subject: [PATCH] refactored StateLabel to FormLabel add errorLabel removed all constraints and replaced with UIStackView show/hide errorLabel within showError Signed-off-by: Matt Bruce --- .../FormFields/TextFields/EntryField.swift | 114 +++++++++--------- 1 file changed, 54 insertions(+), 60 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/FormFields/TextFields/EntryField.swift b/MVMCoreUI/Atomic/Atoms/FormFields/TextFields/EntryField.swift index 6271c44a..1a64a0a5 100644 --- a/MVMCoreUI/Atomic/Atoms/FormFields/TextFields/EntryField.swift +++ b/MVMCoreUI/Atomic/Atoms/FormFields/TextFields/EntryField.swift @@ -17,8 +17,8 @@ import UIKit //-------------------------------------------------- // MARK: - Outlets //-------------------------------------------------- - public private(set) var titleLabel: StateLabel = { - let label = StateLabel() + public private(set) var titleLabel: FormLabel = { + let label = FormLabel() label.setContentCompressionResistancePriority(.required, for: .vertical) return label }() @@ -26,12 +26,33 @@ import UIKit public private(set) var entryFieldContainer = EntryFieldContainer() /// Provides contextual information on the TextField. - public private(set) var feedbackLabel: StateLabel = { - let label = StateLabel() + public private(set) var feedbackLabel: FormLabel = { + let label = FormLabel() label.setContentCompressionResistancePriority(.required, for: .vertical) return label }() - + + public private(set) var errorLabel: Label = { + let label = Label() + label.setFontStyle(.RegularMicro) + label.textColor = .mvmBlack + label.setContentCompressionResistancePriority(.required, for: .vertical) + return label + }() + + public lazy var stack: UIStackView = { + errorLabel.isHidden = true + let s = UIStackView(arrangedSubviews: [titleLabel, entryFieldContainer, errorLabel, feedbackLabel]) + s.axis = .vertical + s.alignment = .fill + s.distribution = .fill + s.setCustomSpacing(Padding.One, after: titleLabel) + s.setCustomSpacing(Padding.Two, after: entryFieldContainer) + s.setCustomSpacing(Padding.One, after: errorLabel) + s.translatesAutoresizingMaskIntoConstraints = false + return s + }() + //-------------------------------------------------- // MARK: - Delegate //-------------------------------------------------- @@ -59,13 +80,17 @@ import UIKit feedbackLabel.isEnabled = enabled entryFieldContainer.isEnabled = enabled entryFieldModel?.enabled = enabled - if !enabled { - self.text = defaultText - } } } } + /// Toggles enabled (original) or disabled UI. + public var isRequired: Bool = true { + didSet{ + titleLabel.isRequired = isRequired + } + } + /// Toggles readOnly (original) or disabled UI. public var isReadOnly: Bool { get { entryFieldContainer.isReadOnly } @@ -82,9 +107,10 @@ import UIKit get { entryFieldContainer.showError } set (error) { if error { - feedbackLabel.showError(message: self.errorMessage ?? "") + errorLabel.text = self.errorMessage ?? "" + errorLabel.isHidden = false } else { - feedbackLabel.reset() + errorLabel.isHidden = true } entryFieldContainer.showError = error entryFieldModel?.showError = error @@ -141,25 +167,6 @@ import UIKit model as? EntryFieldModel } - ///This is the value of the entryFieldModel.text on set - ///This is used to update the text value on a reset or when enabled is set to false - private var defaultText: String = "" - //-------------------------------------------------- - // MARK: - Constraints - //-------------------------------------------------- - - public var entryFieldContainerLeading: NSLayoutConstraint? - public var entryFieldContainerTrailing: NSLayoutConstraint? - - public var feedbackLabelTrailing: NSLayoutConstraint? - public var feedbackLabelLeading: NSLayoutConstraint? - - public var titleLabelLeading: NSLayoutConstraint? - public var titleLabelTrailing: NSLayoutConstraint? - - public var titleContainerDistance: NSLayoutConstraint? - public var feedbackContainerDistance: NSLayoutConstraint? - //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- @@ -199,37 +206,18 @@ import UIKit isAccessibilityElement = false setContentCompressionResistancePriority(.required, for: .vertical) - accessibilityElements = [titleLabel, feedbackLabel] + accessibilityElements = [titleLabel, errorLabel, feedbackLabel] backgroundColor = .mvmWhite - - addSubview(titleLabel) - - titleLabel.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true - titleLabelLeading = titleLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor) - titleLabelLeading?.isActive = true - titleLabelTrailing = layoutMarginsGuide.trailingAnchor.constraint(equalTo: titleLabel.trailingAnchor) - titleLabelLeading?.isActive = true - - addSubview(entryFieldContainer) + + addSubview(stack) + entryFieldContainer.setContentCompressionResistancePriority(.required, for: .vertical) setupFieldContainerContent(entryFieldContainer) - - titleContainerDistance = entryFieldContainer.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: Padding.One) - titleContainerDistance?.isActive = true - entryFieldContainerLeading = entryFieldContainer.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor) - entryFieldContainerLeading?.isActive = true - entryFieldContainerTrailing = layoutMarginsGuide.trailingAnchor.constraint(equalTo: entryFieldContainer.trailingAnchor) - entryFieldContainerTrailing?.isActive = true - - addSubview(feedbackLabel) - - feedbackContainerDistance = feedbackLabel.topAnchor.constraint(equalTo: entryFieldContainer.bottomAnchor, constant: Padding.Two) - feedbackContainerDistance?.isActive = true - feedbackLabelLeading = feedbackLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor) - feedbackLabelLeading?.isActive = true - feedbackLabelTrailing = layoutMarginsGuide.trailingAnchor.constraint(equalTo: feedbackLabel.trailingAnchor) - feedbackLabelTrailing?.isActive = true - layoutMarginsGuide.bottomAnchor.constraint(equalTo: feedbackLabel.bottomAnchor).isActive = true + + stack.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true + stack.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true + stack.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor).isActive = true + stack.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor).isActive = true } @objc open override func layoutSubviews() { @@ -247,8 +235,10 @@ import UIKit super.updateView(size) titleLabel.updateView(size) + errorLabel.updateView(size) feedbackLabel.updateView(size) entryFieldContainer.updateView(size) + stack.updateView(size) } //-------------------------------------------------- @@ -306,6 +296,9 @@ import UIKit titleLabel.textColor = .mvmBlack feedbackLabel.font = Styler.Font.RegularMicro.getFont() feedbackLabel.textColor = .mvmBlack + errorLabel.font = Styler.Font.RegularMicro.getFont() + errorLabel.textColor = .mvmBlack + errorLabel.text = nil entryFieldContainer.disableAllBorders = false feedbackLabel.text = nil entryFieldContainer.reset() @@ -324,9 +317,9 @@ import UIKit titleLabel.setup(model: model.titleStateLabel, delegateObject, additionalData) feedbackLabel.setup(model: model.feedbackStateLabel, delegateObject, additionalData) - defaultText = model.text ?? "" isEnabled = model.enabled isReadOnly = model.readOnly + isRequired = model.required model.updateUI = { [weak self] in MVMCoreDispatchUtility.performBlock(onMainThread: { guard let self = self else { return } @@ -338,6 +331,7 @@ import UIKit self.showError = false } self.isEnabled = model.enabled + self.text = model.text }) } @@ -388,8 +382,8 @@ extension EntryField { } extension LabelModel { - public convenience init(fontStyle: Styler.Font, textColor: Color) { - self.init(text: "") + public convenience init(text: String = "", fontStyle: Styler.Font, textColor: Color) { + self.init(text: text) self.fontStyle = fontStyle self.textColor = textColor }