From bb705d8155e3197eef335b8a7e1b6aa426881014 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 23 Apr 2024 16:39:50 -0500 Subject: [PATCH] refactored value to be readOnly Signed-off-by: Matt Bruce --- .../DropdownSelect/DropdownSelect.swift | 7 +- .../TextFields/EntryFieldBase.swift | 10 +-- .../TextFields/InputField/InputField.swift | 90 +++++++++++-------- .../TextFields/TextArea/TextArea.swift | 14 +-- VDS/Protocols/FormFieldable.swift | 2 +- 5 files changed, 64 insertions(+), 59 deletions(-) diff --git a/VDS/Components/DropdownSelect/DropdownSelect.swift b/VDS/Components/DropdownSelect/DropdownSelect.swift index f1b7fd13..860b4db0 100644 --- a/VDS/Components/DropdownSelect/DropdownSelect.swift +++ b/VDS/Components/DropdownSelect/DropdownSelect.swift @@ -37,6 +37,11 @@ open class DropdownSelect: EntryFieldBase { /// Allows unique ID to be passed to the element. open var selectId: Int? { didSet { setNeedsUpdate() }} + /// Current SelectedItem Value + open override var value: String? { + selectedItem?.value + } + /// Current SelectedItem open var selectedItem: DropdownOptionModel? { guard let selectId else { return nil } @@ -228,7 +233,6 @@ open class DropdownSelect: EntryFieldBase { open func updateSelectedOptionLabel(option: DropdownOptionModel? = nil) { selectedOptionLabel.text = option?.text ?? "" - value = option?.value } open override func updateErrorLabel() { @@ -278,6 +282,7 @@ extension DropdownSelect: UIPickerViewDelegate, UIPickerViewDataSource { guard options.count > row else { return } selectId = row updateSelectedOptionLabel(option: options[row]) + sendActions(for: .valueChanged) self.onItemSelected?(row, options[row]) } } diff --git a/VDS/Components/TextFields/EntryFieldBase.swift b/VDS/Components/TextFields/EntryFieldBase.swift index 7d78cf11..ddbd0016 100644 --- a/VDS/Components/TextFields/EntryFieldBase.swift +++ b/VDS/Components/TextFields/EntryFieldBase.swift @@ -201,15 +201,8 @@ open class EntryFieldBase: Control, Changeable, FormFieldInternalValidatable { open var inputId: String? { didSet { setNeedsUpdate() } } /// The text of this textField. - internal var _value: String? open var value: String? { - get { _value } - set { - if let newValue, newValue != _value { - _value = newValue - sendActions(for: .valueChanged) - } - } + get { fatalError("must be read from subclass")} } open var defaultValue: AnyHashable? { didSet { setNeedsUpdate() } } @@ -306,7 +299,6 @@ open class EntryFieldBase: Control, Changeable, FormFieldInternalValidatable { transparentBackground = false width = nil inputId = nil - value = nil defaultValue = nil required = false readOnly = false diff --git a/VDS/Components/TextFields/InputField/InputField.swift b/VDS/Components/TextFields/InputField/InputField.swift index cb013856..f43fc951 100644 --- a/VDS/Components/TextFields/InputField/InputField.swift +++ b/VDS/Components/TextFields/InputField/InputField.swift @@ -30,15 +30,15 @@ open class InputField: EntryFieldBase, UITextFieldDelegate { public required init?(coder: NSCoder) { super.init(coder: coder) } - + //-------------------------------------------------- // MARK: - Enums //-------------------------------------------------- /// Enum used to describe the input type. public enum FieldType: String, CaseIterable { - case text, number, calendar, inlineAction, password, creditCard, tel, date, securityCode + case text, number, inlineAction, password, creditCard, tel, date, securityCode } - + //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- @@ -50,9 +50,9 @@ open class InputField: EntryFieldBase, UITextFieldDelegate { $0.spacing = 12 } }() - + internal var minWidthConstraint: NSLayoutConstraint? - + //-------------------------------------------------- // MARK: - Public Properties //-------------------------------------------------- @@ -73,39 +73,29 @@ open class InputField: EntryFieldBase, UITextFieldDelegate { $0.setSurfaceColors(VDSColor.interactiveDisabledOnlight, VDSColor.interactiveDisabledOndark, forDisabled: true) $0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forDisabled: false) }.eraseToAnyColorable() - + /// Representing the type of input. open var fieldType: FieldType = .text { didSet { setNeedsUpdate() } } - + open var leftIcon: Icon = Icon().with { $0.size = .medium } open var actionTextLink = TextLink().with { $0.contentEdgeInsets = .top(-2) } open var actionTextLinkModel: TextLinkModel? { didSet { setNeedsUpdate() } } - + /// The text of this TextField. - private var _text: String? open var text: String? { - get { _text } + get { textField.text } set { - if let newValue, newValue != _text { - _text = newValue - textField.text = newValue - value = newValue - } - setNeedsUpdate() + textField.text = newValue } } - /// The value of this textField. + /// Value for the textField open override var value: String? { - didSet { - if text != value { - text = value - } - } + textField.text } - + var _showError: Bool = false /// Whether not to show the error. open override var showError: Bool { @@ -172,10 +162,10 @@ open class InputField: EntryFieldBase, UITextFieldDelegate { textField.heightAnchor.constraint(equalToConstant: 20).isActive = true textField .textPublisher - .sink { [weak self] text in - self?.value = text + .sink { [weak self] newText in + self?.text = newText self?.sendActions(for: .valueChanged) - + if newText.isEmpty { self?.passwordActionType = .show } }.store(in: &subscribers) stackView.addArrangedSubview(successLabel) @@ -259,13 +249,13 @@ open class InputField: EntryFieldBase, UITextFieldDelegate { } } } - + open func updateFieldType() { textField.isSecureTextEntry = false var minWidth: CGFloat = 40.0 - var iconName: Icon.Name? - var actionLinkModel: InputField.TextLinkModel? + var leftIconName: Icon.Name? + var actionModel: InputField.TextLinkModel? var toolTipModel: Tooltip.TooltipModel? switch fieldType { @@ -275,14 +265,22 @@ open class InputField: EntryFieldBase, UITextFieldDelegate { case .number: break - case .calendar: - break - case .inlineAction: minWidth = 102.0 case .password: - textField.isSecureTextEntry = true + let isHide = passwordActionType == .hide + let buttonText = isHide ? hidePasswordButtonText : showPasswordButtonText + let isSecureTextEntry = !isHide + let nextPasswordActionType = passwordActionType.toggle() + if let text, !text.isEmpty { + actionModel = .init(text: buttonText, + onClick: { [weak self] _ in + guard let self else { return } + self.passwordActionType = nextPasswordActionType + }) + textField.isSecureTextEntry = isSecureTextEntry + } minWidth = 62.0 case .creditCard: @@ -302,13 +300,14 @@ open class InputField: EntryFieldBase, UITextFieldDelegate { //leftIcon leftIcon.surface = surface leftIcon.color = iconColorConfiguration.getColor(self) - leftIcon.name = iconName - leftIcon.isHidden = iconName == nil + leftIcon.name = leftIconName + leftIcon.isHidden = leftIconName == nil //actionLink actionTextLink.surface = surface - if let actionTextLinkModel = actionLinkModel { - actionTextLink.text = actionTextLinkModel.text + if let actionModel { + actionTextLink.text = actionModel.text + actionTextLink.onClick = actionModel.onClick actionTextLink.isHidden = false containerStackView.setCustomSpacing(VDSLayout.space2X, after: statusIcon) } else { @@ -330,4 +329,21 @@ open class InputField: EntryFieldBase, UITextFieldDelegate { //tooltip self.tooltipModel = toolTipModel } + + //-------------------------------------------------- + // MARK: - Password + //-------------------------------------------------- + enum PasswordAction { + case show, hide + + func toggle() -> PasswordAction { + self == .hide ? .show : .hide + } + } + + internal var passwordActionType: PasswordAction = .show { didSet { setNeedsUpdate() } } + + open var hidePasswordButtonText: String = "Hide" { didSet { setNeedsUpdate() } } + open var showPasswordButtonText: String = "Show" { didSet { setNeedsUpdate() } } } + diff --git a/VDS/Components/TextFields/TextArea/TextArea.swift b/VDS/Components/TextFields/TextArea/TextArea.swift index 797e7e74..1de4c1aa 100644 --- a/VDS/Components/TextFields/TextArea/TextArea.swift +++ b/VDS/Components/TextFields/TextArea/TextArea.swift @@ -102,24 +102,16 @@ open class TextArea: EntryFieldBase { /// The text of this TextArea. private var _text: String? open var text: String? { - get { _text } + get { textView.text } set { - if let newValue, newValue != _text { - _text = newValue - textView.text = newValue - value = newValue - } + textView.text = newValue setNeedsUpdate() } } /// The value of this textField. open override var value: String? { - didSet { - if text != value { - text = value - } - } + return textView.text } /// UITextView shown in the TextArea. diff --git a/VDS/Protocols/FormFieldable.swift b/VDS/Protocols/FormFieldable.swift index 70a69ee5..83638cf4 100644 --- a/VDS/Protocols/FormFieldable.swift +++ b/VDS/Protocols/FormFieldable.swift @@ -15,7 +15,7 @@ public protocol FormFieldable { var inputId: String? { get set } /// Value for the Form Field. - var value: ValueType? { get set } + var value: ValueType? { get } } /// Protocol for FormFieldable that require internal validation.