refactored readOnly to isReadOnly and required to isRequired

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-05-01 17:36:48 -05:00
parent 0ae2dd2706
commit 3f36d8b8c9
4 changed files with 76 additions and 89 deletions

View File

@ -162,7 +162,7 @@ open class DropdownSelect: EntryFieldBase {
updateInlineLabel() updateInlineLabel()
dropdownField.isUserInteractionEnabled = readOnly ? false : true dropdownField.isUserInteractionEnabled = isReadOnly ? false : true
selectedOptionLabel.surface = surface selectedOptionLabel.surface = surface
selectedOptionLabel.isEnabled = isEnabled selectedOptionLabel.isEnabled = isEnabled
} }
@ -190,7 +190,7 @@ open class DropdownSelect: EntryFieldBase {
updatedLabelText = showInlineLabel ? "" : updatedLabelText updatedLabelText = showInlineLabel ? "" : updatedLabelText
if let oldText = updatedLabelText, !required, !oldText.hasSuffix("Optional") { if let oldText = updatedLabelText, !isRequired, !oldText.hasSuffix("Optional") {
let optionColorAttr = ColorLabelAttribute(location: oldText.count + 2, let optionColorAttr = ColorLabelAttribute(location: oldText.count + 2,
length: 8, length: 8,
color: secondaryColorConfiguration.getColor(self)) color: secondaryColorConfiguration.getColor(self))
@ -241,7 +241,7 @@ open class DropdownSelect: EntryFieldBase {
statusIcon.name = .downCaret statusIcon.name = .downCaret
} }
statusIcon.surface = surface statusIcon.surface = surface
statusIcon.isHidden = readOnly ? true : false statusIcon.isHidden = isReadOnly ? true : false
statusIcon.color = iconColorConfiguration.getColor(self) statusIcon.color = iconColorConfiguration.getColor(self)
} }

View File

@ -190,9 +190,9 @@ open class EntryFieldBase: Control, Changeable, FormFieldInternalValidatable {
open var defaultValue: AnyHashable? { didSet { setNeedsUpdate() } } open var defaultValue: AnyHashable? { didSet { setNeedsUpdate() } }
open var required: Bool = false { didSet { validate() } } open var isRequired: Bool = false { didSet { setNeedsUpdate() } }
open var readOnly: Bool = false { didSet { setNeedsUpdate() } } open var isReadOnly: Bool = false { didSet { setNeedsUpdate() } }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Constraints // MARK: - Constraints
@ -283,8 +283,8 @@ open class EntryFieldBase: Control, Changeable, FormFieldInternalValidatable {
width = nil width = nil
inputId = nil inputId = nil
defaultValue = nil defaultValue = nil
required = false isRequired = false
readOnly = false isReadOnly = false
onChange = nil onChange = nil
} }
@ -309,7 +309,7 @@ open class EntryFieldBase: Control, Changeable, FormFieldInternalValidatable {
//-------------------------------------------------- //--------------------------------------------------
private func updateContainerView() { private func updateContainerView() {
containerView.backgroundColor = backgroundColorConfiguration.getColor(self) containerView.backgroundColor = backgroundColorConfiguration.getColor(self)
containerView.layer.borderColor = readOnly ? readOnlyBorderColorConfiguration.getColor(self).cgColor : borderColorConfiguration.getColor(self).cgColor containerView.layer.borderColor = isReadOnly ? readOnlyBorderColorConfiguration.getColor(self).cgColor : borderColorConfiguration.getColor(self).cgColor
containerView.layer.borderWidth = VDSFormControls.borderWidth containerView.layer.borderWidth = VDSFormControls.borderWidth
containerView.layer.cornerRadius = VDSFormControls.borderRadius containerView.layer.cornerRadius = VDSFormControls.borderRadius
} }
@ -329,7 +329,7 @@ open class EntryFieldBase: Control, Changeable, FormFieldInternalValidatable {
internal func updateRules() { internal func updateRules() {
rules.removeAll() rules.removeAll()
if self.required { if self.isRequired {
let rule = RequiredRule() let rule = RequiredRule()
if let errorText, !errorText.isEmpty { if let errorText, !errorText.isEmpty {
rule.errorMessage = errorText rule.errorMessage = errorText
@ -350,7 +350,7 @@ open class EntryFieldBase: Control, Changeable, FormFieldInternalValidatable {
var updatedLabelText = labelText var updatedLabelText = labelText
//dealing with the "Optional" addition to the text //dealing with the "Optional" addition to the text
if let oldText = updatedLabelText, !required, !oldText.hasSuffix("Optional") { if let oldText = updatedLabelText, !isRequired, !oldText.hasSuffix("Optional") {
if isEnabled { if isEnabled {
let optionColorAttr = ColorLabelAttribute(location: oldText.count + 2, let optionColorAttr = ColorLabelAttribute(location: oldText.count + 2,
length: 8, length: 8,

View File

@ -165,7 +165,6 @@ open class InputField: EntryFieldBase {
controlStackView.addArrangedSubview(leftIcon) controlStackView.addArrangedSubview(leftIcon)
controlStackView.addArrangedSubview(textField) controlStackView.addArrangedSubview(textField)
textField.delegate = self
textField.heightAnchor.constraint(equalToConstant: 20).isActive = true textField.heightAnchor.constraint(equalToConstant: 20).isActive = true
textField textField
.textPublisher .textPublisher
@ -176,6 +175,18 @@ open class InputField: EntryFieldBase {
self?.sendActions(for: .valueChanged) self?.sendActions(for: .valueChanged)
}.store(in: &subscribers) }.store(in: &subscribers)
textField
.publisher(for: .editingDidBegin)
.sink { [weak self] _ in
self?.setNeedsUpdate()
}.store(in: &subscribers)
textField
.publisher(for: .editingDidEnd)
.sink { [weak self] _ in
self?.validate()
}.store(in: &subscribers)
stackView.addArrangedSubview(successLabel) stackView.addArrangedSubview(successLabel)
stackView.setCustomSpacing(8, after: successLabel) stackView.setCustomSpacing(8, after: successLabel)
@ -192,7 +203,6 @@ open class InputField: EntryFieldBase {
open override func reset() { open override func reset() {
super.reset() super.reset()
textField.text = "" textField.text = ""
textField.delegate = self
successLabel.reset() successLabel.reset()
successLabel.textStyle = .bodySmall successLabel.textStyle = .bodySmall
@ -272,7 +282,7 @@ open class InputField: EntryFieldBase {
var isSecureTextEntry = false var isSecureTextEntry = false
var rules = [AnyRule<String>]() var rules = [AnyRule<String>]()
if self.required { if self.isRequired {
let rule = RequiredRule() let rule = RequiredRule()
if let errorText { if let errorText {
rule.errorMessage = errorText rule.errorMessage = errorText
@ -395,33 +405,3 @@ open class InputField: EntryFieldBase {
open var hidePasswordButtonText: String = "Hide" { didSet { setNeedsUpdate() } } open var hidePasswordButtonText: String = "Hide" { didSet { setNeedsUpdate() } }
open var showPasswordButtonText: String = "Show" { didSet { setNeedsUpdate() } } open var showPasswordButtonText: String = "Show" { didSet { setNeedsUpdate() } }
} }
extension InputField: UITextFieldDelegate {
public func textFieldDidBeginEditing(_ textField: UITextField) {
setNeedsUpdate()
}
public func textFieldDidEndEditing(_ textField: UITextField) {
setNeedsUpdate()
}
}
public class TextField: UITextField {
open override var isSecureTextEntry: Bool {
didSet {
if isFirstResponder {
_ = becomeFirstResponder()
}
}
}
public override func becomeFirstResponder() -> Bool {
let success = super.becomeFirstResponder()
if isSecureTextEntry, let text {
self.text?.removeAll()
insertText(text)
}
return success
}
}

View File

@ -171,14 +171,34 @@ open class TextArea: EntryFieldBase {
.pinLeading() .pinLeading()
.pinTrailingLessThanOrEqualTo(nil, 0, .defaultHigh) .pinTrailingLessThanOrEqualTo(nil, 0, .defaultHigh)
.pinBottom(0, .defaultHigh) .pinBottom(0, .defaultHigh)
textView.isScrollEnabled = true textView.isScrollEnabled = true
textView.autocorrectionType = .no textView.autocorrectionType = .no
//events
textView
.publisher(for: .editingChanged)
.sink { [weak self] control in
self?.textViewDidChange(control)
}.store(in: &subscribers)
textView
.publisher(for: .editingDidBegin)
.sink { [weak self] _ in
self?.setNeedsUpdate()
}.store(in: &subscribers)
textView
.publisher(for: .editingDidEnd)
.sink { [weak self] _ in
self?.validate()
}.store(in: &subscribers)
textViewHeightConstraint = textView.heightAnchor.constraint(greaterThanOrEqualToConstant: containerSize.height) textViewHeightConstraint = textView.heightAnchor.constraint(greaterThanOrEqualToConstant: containerSize.height)
textViewHeightConstraint?.isActive = true textViewHeightConstraint?.isActive = true
backgroundColorConfiguration.setSurfaceColors(VDSColor.feedbackSuccessBackgroundOnlight, VDSColor.feedbackSuccessBackgroundOndark, forState: .success) backgroundColorConfiguration.setSurfaceColors(VDSColor.feedbackSuccessBackgroundOnlight, VDSColor.feedbackSuccessBackgroundOndark, forState: .success)
borderColorConfiguration.setSurfaceColors(VDSColor.feedbackSuccessOnlight, VDSColor.feedbackSuccessOndark, forState: .success) borderColorConfiguration.setSurfaceColors(VDSColor.feedbackSuccessOnlight, VDSColor.feedbackSuccessOndark, forState: .success)
borderColorConfiguration.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .focused) borderColorConfiguration.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .focused)
textView.delegate = self
characterCounterLabel.textColorConfiguration = primaryColorConfiguration.eraseToAnyColorable() characterCounterLabel.textColorConfiguration = primaryColorConfiguration.eraseToAnyColorable()
bottomContainerStackView.spacing = VDSLayout.space2X bottomContainerStackView.spacing = VDSLayout.space2X
} }
@ -219,8 +239,8 @@ open class TextArea: EntryFieldBase {
characterCounterLabel.text = getCharacterCounterText() characterCounterLabel.text = getCharacterCounterText()
statusIcon.color = iconColorConfiguration.getColor(self) statusIcon.color = iconColorConfiguration.getColor(self)
containerView.layer.borderColor = readOnly ? readOnlyBorderColorConfiguration.getColor(self).cgColor : borderColorConfiguration.getColor(self).cgColor containerView.layer.borderColor = isReadOnly ? readOnlyBorderColorConfiguration.getColor(self).cgColor : borderColorConfiguration.getColor(self).cgColor
textView.isEditable = readOnly ? false : true textView.isEditable = isReadOnly ? false : true
textView.backgroundColor = backgroundColorConfiguration.getColor(self) textView.backgroundColor = backgroundColorConfiguration.getColor(self)
textView.tintColor = iconColorConfiguration.getColor(self) textView.tintColor = iconColorConfiguration.getColor(self)
characterCounterLabel.surface = surface characterCounterLabel.surface = surface
@ -277,41 +297,7 @@ open class TextArea: EntryFieldBase {
} }
} }
open func highlightCharacterOverflow() { func textViewDidChange(_ textView: UITextView) {
let count = textView.text.count
guard let maxLength, maxLength > 0, count > maxLength else {
textView.textAttributes = nil
return
}
var textAttributes = [any LabelAttributeModel]()
let location = maxLength
let length = count - maxLength
textAttributes.append(ColorLabelAttribute(location: location, length: length, color: highlightBackgroundColor.getColor(self), isForegroundColor: false))
textAttributes.append(ColorLabelAttribute(location: location, length: length, color: highlightTextColor.getColor(self), isForegroundColor: true))
textView.textAttributes = textAttributes
}
//--------------------------------------------------
// MARK: - Validation
//--------------------------------------------------
var countRule = CharacterCountRule()
}
extension TextArea: UITextViewDelegate {
public func textViewDidBeginEditing(_ textView: UITextView) {
setNeedsUpdate()
}
public func textViewDidEndEditing(_ textView: UITextView) {
setNeedsUpdate()
}
//--------------------------------------------------
// MARK: - UITextViewDelegate
//--------------------------------------------------
public func textViewDidChange(_ textView: UITextView) {
//dynamic textView Height sizing based on Figma //dynamic textView Height sizing based on Figma
//if you want it to work "as-is" delete this code //if you want it to work "as-is" delete this code
@ -351,4 +337,25 @@ extension TextArea: UITextViewDelegate {
} }
validate() validate()
} }
private func highlightCharacterOverflow() {
let count = textView.text.count
guard let maxLength, maxLength > 0, count > maxLength else {
textView.textAttributes = nil
return
}
var textAttributes = [any LabelAttributeModel]()
let location = maxLength
let length = count - maxLength
textAttributes.append(ColorLabelAttribute(location: location, length: length, color: highlightBackgroundColor.getColor(self), isForegroundColor: false))
textAttributes.append(ColorLabelAttribute(location: location, length: length, color: highlightTextColor.getColor(self), isForegroundColor: true))
textView.textAttributes = textAttributes
}
//--------------------------------------------------
// MARK: - Validation
//--------------------------------------------------
var countRule = CharacterCountRule()
} }