Digital ACT191 story ONEAPP-6682 Checking character count overflow

This commit is contained in:
vasavk 2024-02-23 16:11:14 +05:30
parent 783143717c
commit e319153ac6

View File

@ -35,7 +35,8 @@ open class TextArea: EntryFieldBase {
//--------------------------------------------------
internal var minWidthConstraint: NSLayoutConstraint?
internal var textViewHeightConstraint: NSLayoutConstraint?
internal var allowCharCount: Int = 0
internal var inputFieldStackView: UIStackView = {
return UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
@ -64,10 +65,6 @@ open class TextArea: EntryFieldBase {
$0.setContentCompressionResistancePriority(.required, for: .vertical)
$0.textStyle = .bodySmall
}
open var maxLengthLabel = Label().with {
$0.setContentCompressionResistancePriority(.required, for: .vertical)
$0.textStyle = .bodySmall
}
//--------------------------------------------------
// MARK: - Public Properties
@ -96,9 +93,6 @@ open class TextArea: EntryFieldBase {
super.setup()
minWidthConstraint = containerView.widthAnchor.constraint(greaterThanOrEqualToConstant: containerSize.width)
minWidthConstraint?.isActive = true
characterCountLabel.text = "0"
maxLengthLabel.text = "/200"
controlContainerView.addSubview(textView)
textView
.pinTop()
@ -118,6 +112,8 @@ open class TextArea: EntryFieldBase {
open override func reset() {
super.reset()
textView.text = ""
characterCountLabel.reset()
characterCountLabel.textStyle = .bodySmall
}
/// Container for the area in which the user interacts.
@ -132,7 +128,6 @@ open class TextArea: EntryFieldBase {
textView.isEditable = isEnabled
textView.textColor = textViewTextColorConfiguration.getColor(self)
//set the width constraints
if let width {
widthConstraint?.constant = width
@ -143,6 +138,12 @@ open class TextArea: EntryFieldBase {
widthConstraint?.isActive = false
minWidthConstraint?.isActive = true
}
// allow - 20% of character limit
let overflowLimit = Double(maxLength ?? 0) * 0.20
allowCharCount = Int(overflowLimit) + (maxLength ?? 0)
characterCountLabel.text = getLimitText()
}
/// Container for the area which shows helper text, error text, character count, max length value.
@ -151,9 +152,18 @@ open class TextArea: EntryFieldBase {
bottomStackView.pinToSuperView()
bottomStackView.addArrangedSubview(bottomContainerView)
bottomStackView.addArrangedSubview(characterCountLabel)
bottomStackView.addArrangedSubview(maxLengthLabel)
return bottomView
}
//--------------------------------------------------
// MARK: - Private Methods
//--------------------------------------------------
private func getLimitText() -> String {
let count = textView.text.count
let countStr = (count > maxLength ?? 0) ? ("-" + "\(count-(maxLength ?? 0))") : "\(count)"
let text = "\(countStr)" + "/" + "\(maxLength ?? 0)"
return text
}
}
extension TextArea: UITextViewDelegate {
@ -177,9 +187,12 @@ extension TextArea: UITextViewDelegate {
textViewHeightConstraint.isActive = true
}
//setting the value and firing control event
value = textView.text
sendActions(for: .valueChanged)
if textView.text.count <= allowCharCount {
//setting the value and firing control event
value = textView.text
sendActions(for: .valueChanged)
} else {
textView.text.removeLast()
}
}
}