157 lines
5.7 KiB
Swift
157 lines
5.7 KiB
Swift
//
|
|
// RadioButtonGroup.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 8/11/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
@objc(VDSRadioButtonGroup)
|
|
open class RadioButtonGroup: SelectorGroupSelectedHandlerBase<RadioButtonItem> {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
public override var selectorViews: [RadioButtonItem] {
|
|
willSet {
|
|
mainStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
|
|
}
|
|
|
|
didSet {
|
|
for selector in selectorViews {
|
|
selector.onClick = { [weak self] handler in
|
|
self?.didSelect(handler)
|
|
self?.setNeedsUpdate()
|
|
}
|
|
mainStackView.addArrangedSubview(selector)
|
|
}
|
|
}
|
|
}
|
|
|
|
open var selectorModels: [RadioButtonModel]? {
|
|
didSet {
|
|
if let selectorModels {
|
|
selectorViews = selectorModels.enumerated().map { index, model in
|
|
return RadioButtonItem().with {
|
|
$0.isEnabled = !model.disabled
|
|
$0.surface = model.surface
|
|
$0.inputId = model.inputId
|
|
$0.value = model.value
|
|
$0.accessibilityLabel = model.accessibileText
|
|
$0.accessibilityValue = "item \(index+1) of \(selectorModels.count)"
|
|
$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
|
|
}
|
|
}
|
|
}
|
|
setNeedsUpdate()
|
|
}
|
|
}
|
|
|
|
private var _showError: Bool = false
|
|
open 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 = VDSLayout.Spacing.space6X.value
|
|
}
|
|
}()
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Overrides
|
|
//--------------------------------------------------
|
|
/// Called once when a view is initialized and is used to Setup additional UI or other constants and configurations.
|
|
open override func setup() {
|
|
super.setup()
|
|
|
|
addSubview(mainStackView)
|
|
|
|
mainStackView.pinToSuperView()
|
|
}
|
|
|
|
public override func didSelect(_ selectedControl: RadioButtonItem) {
|
|
if let selectedHandler {
|
|
updateToggle(selectedHandler)
|
|
}
|
|
updateToggle(selectedControl)
|
|
if showError {
|
|
showError = false
|
|
}
|
|
valueChanged()
|
|
}
|
|
|
|
private func updateToggle(_ radioButton: RadioButtonItem) {
|
|
if radioButton.showError && radioButton.isSelected == false {
|
|
radioButton.showError.toggle()
|
|
}
|
|
radioButton.isSelected.toggle()
|
|
}
|
|
}
|
|
|
|
extension RadioButtonGroup {
|
|
public struct RadioButtonModel: Surfaceable, Initable, FormFieldable, Errorable {
|
|
|
|
/// Whether this object is disabled or not
|
|
public var disabled: Bool
|
|
/// Current Surface and this is used to pass down to child objects that implement Surfacable
|
|
public var surface: Surface
|
|
public var inputId: String?
|
|
public var value: AnyHashable?
|
|
public var accessibileText: String?
|
|
public var labelText: String?
|
|
/// Array of LabelAttributeModel objects used in rendering the labelText.
|
|
public var labelTextAttributes: [any LabelAttributeModel]?
|
|
public var childText: String?
|
|
/// Array of LabelAttributeModel objects used in rendering the childText.
|
|
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)
|
|
}
|
|
}
|
|
}
|