137 lines
5.2 KiB
Swift
137 lines
5.2 KiB
Swift
//
|
|
// CheckboxGroup.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 8/23/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
/// When the choice has multiple options, use a checkbox group. For example, use a checkbox group when
|
|
/// asking a customer which attributes they would like to filter their search by. This uses ``CheckboxItem``
|
|
/// to allow user selection.
|
|
@objc(VDSCheckboxGroup)
|
|
open class CheckboxGroup: SelectorGroupBase<CheckboxItem> {
|
|
//--------------------------------------------------
|
|
// MARK: - Initializers
|
|
//--------------------------------------------------
|
|
required public init() {
|
|
super.init(frame: .zero)
|
|
}
|
|
|
|
public override init(frame: CGRect) {
|
|
super.init(frame: .zero)
|
|
}
|
|
|
|
public required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
/// Array of ``CheckboxItemModel`` that will be used to build the selectorViews of type ``CheckboxItem``.
|
|
open var selectorModels: [CheckboxItemModel]? {
|
|
didSet {
|
|
if let selectorModels {
|
|
items = selectorModels.enumerated().map { index, model in
|
|
return CheckboxItem().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
|
|
|
|
/// Whether not to show the error.
|
|
open var showError: Bool {
|
|
get { _showError }
|
|
set {
|
|
var newShowError = newValue
|
|
let anySelected = items.filter { $0.isSelected == true }.count > 0
|
|
if anySelected && newShowError {
|
|
newShowError = false
|
|
}
|
|
items.forEach { handler in
|
|
handler.showError = newShowError
|
|
}
|
|
_showError = newShowError
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// 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()
|
|
mainStackView.spacing = VDSLayout.Spacing.space6X.value
|
|
}
|
|
|
|
public override func didSelect(_ selectedControl: CheckboxItem) {
|
|
selectedControl.toggle()
|
|
if selectedControl.isSelected, showError{
|
|
showError.toggle()
|
|
}
|
|
valueChanged()
|
|
}
|
|
}
|
|
|
|
extension CheckboxGroup {
|
|
public struct CheckboxItemModel : 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 labeText.
|
|
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)
|
|
}
|
|
}
|
|
|
|
}
|