vds_ios/VDS/Components/RadioButton/RadioButtonGroup.swift
Matt Bruce dd6fddbcf4 fixed layout bugs in console
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-11-21 12:45:52 -06:00

85 lines
2.4 KiB
Swift

//
// RadioButtonGroup.swift
// VDS
//
// Created by Matt Bruce on 8/11/22.
//
import Foundation
import UIKit
@objc(VDSRadioButtonGroup)
public class RadioButtonGroup: RadioButtonGroupBase<RadioButton> {
public override func didSelect(_ selectedControl: RadioButton) {
selectedHandler?.toggle()
selectedControl.toggle()
if showError {
showError = false
}
valueChanged()
}
}
public class RadioButtonGroupBase<HandlerType: RadioButtonBase>: SelectorGroupSelectedHandlerBase<HandlerType> {
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
public override var selectorViews: [HandlerType] {
didSet {
for selector in selectorViews {
if !mainStackView.arrangedSubviews.contains(selector) {
selector
.publisher(for: .touchUpInside)
.sink { [weak self] handler in
self?.didSelect(handler)
}.store(in: &subscribers)
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()
}
}