vds_ios/VDS/Components/RadioButton/RadioButtonGroup.swift
Matt Bruce aa233515a5 updated control
and subcontrols

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-03-29 13:47:56 -05:00

89 lines
2.5 KiB
Swift

//
// RadioButtonGroup.swift
// VDS
//
// Created by Matt Bruce on 8/11/22.
//
import Foundation
import UIKit
@objc(VDSRadioButtonGroup)
public class RadioButtonGroup: SelectorGroupSelectedHandlerBase<RadioButton> {
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
public override var selectorViews: [RadioButton] {
didSet {
for selector in selectorViews {
if !mainStackView.arrangedSubviews.contains(selector) {
selector.onClick = { [weak self] handler in
self?.didSelect(handler)
}
mainStackView.addArrangedSubview(selector)
}
}
}
}
private var _showError: Bool = false
public var showError: Bool {
get { _showError }
set {
var newShowError = newValue
if selectedHandler != nil, newShowError {
newShowError = false
}
selectorViews.forEach { handler in
handler.showError = newShowError
}
_showError = newShowError
}
}
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
private var mainStackView: UIStackView = {
return UIStackView().with {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.alignment = .fill
$0.distribution = .fill
$0.axis = .vertical
$0.spacing = 10
}
}()
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
open override func setup() {
super.setup()
isAccessibilityElement = true
accessibilityTraits = .button
addSubview(mainStackView)
mainStackView.pinToSuperView()
}
public override func didSelect(_ selectedControl: RadioButton) {
if let selectedHandler {
updateToggle(selectedHandler)
}
updateToggle(selectedControl)
if showError {
showError = false
}
valueChanged()
}
private func updateToggle(_ radioButton: RadioButton) {
if radioButton.showError && radioButton.isSelected == false {
radioButton.showError.toggle()
}
radioButton.isSelected.toggle()
}
}