151 lines
5.1 KiB
Swift
151 lines
5.1 KiB
Swift
//
|
|
// RadioButtonGroup.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 8/11/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
@objc(VDSRadioButtonGroup)
|
|
open class RadioButtonGroup: SelectorGroupSelectedHandlerBase<RadioButton> {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
public override var selectorViews: [RadioButton] {
|
|
willSet {
|
|
mainStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
|
|
}
|
|
|
|
didSet {
|
|
for selector in selectorViews {
|
|
selector.onClick = { [weak self] handler in
|
|
self?.didSelect(handler)
|
|
}
|
|
mainStackView.addArrangedSubview(selector)
|
|
}
|
|
}
|
|
}
|
|
|
|
public var selectorModels: [RadioButtonModel]? {
|
|
didSet {
|
|
if let selectorModels {
|
|
selectorViews = selectorModels.map { model in
|
|
return RadioButton().with {
|
|
$0.disabled = model.disabled
|
|
$0.surface = model.surface
|
|
$0.inputId = model.inputId
|
|
$0.value = model.value
|
|
$0.accessibilityLabel = model.accessibileText
|
|
$0.labelText = model.labelText
|
|
$0.labelTextAttributes = model.labelTextAttributes
|
|
$0.childText = model.childText
|
|
$0.childTextAttributes = model.childTextAttributes
|
|
$0.isSelected = model.selected
|
|
$0.errorText = model.errorText
|
|
$0.showError = model.showError
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|
|
|
|
extension RadioButtonGroup {
|
|
public struct RadioButtonModel: Surfaceable, Disabling, Initable, FormFieldable, Errorable {
|
|
|
|
public var disabled: Bool
|
|
public var surface: Surface
|
|
public var inputId: String?
|
|
public var value: AnyHashable?
|
|
public var accessibileText: String?
|
|
public var labelText: String?
|
|
public var labelTextAttributes: [any LabelAttributeModel]?
|
|
public var childText: String?
|
|
public var childTextAttributes: [any LabelAttributeModel]?
|
|
public var selected: Bool
|
|
public var showError: Bool
|
|
public var errorText: String?
|
|
|
|
public init(disabled: Bool, surface: Surface = .light, inputId: String? = nil, value: AnyHashable? = nil, accessibileText: String? = nil, labelText: String? = nil, labelTextAttributes: [any LabelAttributeModel]? = nil, childText: String? = nil, childTextAttributes: [any LabelAttributeModel]? = nil, selected: Bool = false, showError: Bool = false, errorText: String? = nil) {
|
|
self.disabled = disabled
|
|
self.surface = surface
|
|
self.inputId = inputId
|
|
self.value = value
|
|
self.accessibileText = accessibileText
|
|
self.labelText = labelText
|
|
self.labelTextAttributes = labelTextAttributes
|
|
self.childText = childText
|
|
self.childTextAttributes = childTextAttributes
|
|
self.selected = selected
|
|
self.showError = showError
|
|
self.errorText = errorText
|
|
}
|
|
|
|
public init() {
|
|
self.init(disabled: false)
|
|
}
|
|
}
|
|
}
|