added selectable flag to match how android validates

This commit is contained in:
Pfeil, Scott Robert 2021-03-01 14:42:58 -05:00
parent 9b9b775070
commit 7aab88c5ec

View File

@ -22,7 +22,6 @@ import UIKit
public var backgroundColor: Color?
public var molecules: [MoleculeModelProtocol & CarouselItemModelProtocol]
public var index: Int = 0
public var selectedIndex: Int?
public var spacing: CGFloat?
public var border: Bool?
public var loop: Bool?
@ -39,17 +38,25 @@ import UIKit
public var fieldKey: String?
public var groupName: String = FormValidator.defaultGroupName
public var selectable = false
public var selectedIndex: Int?
public init(molecules: [MoleculeModelProtocol & CarouselItemModelProtocol]) {
self.molecules = molecules
}
public func formFieldValue() -> AnyHashable? {
let indexForForm = selectedIndex ?? index
let item = molecules[indexForForm]
guard let value = item.formFieldValue() else {
return indexForForm
guard selectable else {
// Use visible item value, else index
if let fieldValue = molecules[index].formFieldValue() {
return fieldValue
}
return index
}
return value
// Use selected item value, else index
guard let selectedIndex = selectedIndex else { return nil }
guard let fieldValue = molecules[selectedIndex].formFieldValue() else { return selectedIndex }
return fieldValue
}
//--------------------------------------------------
@ -61,7 +68,6 @@ import UIKit
case backgroundColor
case molecules
case index
case selectedIndex
case spacing
case border
case loop
@ -76,6 +82,8 @@ import UIKit
case accessibilityText
case groupName
case fieldKey
case selectable
case selectedIndex
}
//--------------------------------------------------
@ -86,6 +94,7 @@ import UIKit
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
molecules = try typeContainer.decodeModels(codingKey: .molecules)
index = try typeContainer.decodeIfPresent(Int.self, forKey: .index) ?? 0
selectable = try typeContainer.decodeIfPresent(Bool.self, forKey: .selectable) ?? false
selectedIndex = try typeContainer.decodeIfPresent(Int.self, forKey: .selectedIndex)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
spacing = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .spacing)
@ -108,13 +117,7 @@ import UIKit
if let groupName = try typeContainer.decodeIfPresent(String.self, forKey: .groupName) {
self.groupName = groupName
}
if let value = formFieldValue() {
baseValue = value
} else if let value = selectedIndex {
baseValue = value
} else {
baseValue = index
}
baseValue = formFieldValue()
}
public func encode(to encoder: Encoder) throws {
@ -136,5 +139,8 @@ import UIKit
try container.encodeIfPresent(accessibilityText, forKey: .accessibilityText)
try container.encodeIfPresent(fieldKey, forKey: .fieldKey)
try container.encode(groupName, forKey: .groupName)
try container.encode(index, forKey: .index)
try container.encode(selectable, forKey: .selectable)
try container.encode(selectedIndex, forKey: .selectedIndex)
}
}