343 lines
12 KiB
Swift
343 lines
12 KiB
Swift
//
|
|
// TextEntryFieldViewController.swift
|
|
// VDSSample
|
|
//
|
|
// Created by Matt Bruce on 10/3/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDS
|
|
import VDSTokens
|
|
import Combine
|
|
|
|
class InputFieldViewController: BaseViewController<InputField> {
|
|
|
|
lazy var helperTextPlacementPickerSelectorView = {
|
|
PickerSelectorView(title: "",
|
|
picker: self.picker,
|
|
items: InputField.HelperTextPlacement.allCases)
|
|
}()
|
|
|
|
lazy var inputTypePickerSelectorView = {
|
|
PickerSelectorView(title: "",
|
|
picker: self.picker,
|
|
items: InputField.FieldType.allCases)
|
|
}()
|
|
|
|
var disabledSwitch = Toggle()
|
|
var requiredSwitch = Toggle()
|
|
var readonlySwitch = Toggle()
|
|
var transparentBgSwitch = Toggle()
|
|
var labelTextField = TextField()
|
|
var errorTextField = TextField()
|
|
var successTextField = TextField()
|
|
var helperTextField = TextField()
|
|
var widthTextField = NumericField()
|
|
var showErrorSwitch = Toggle()
|
|
var showSuccessSwitch = Toggle()
|
|
var tooltipTitleTextField = TextField()
|
|
var tooltipContentTextField = TextField()
|
|
|
|
//FieldType sections
|
|
|
|
//password
|
|
var hidePasswordButtonTextField = TextField()
|
|
var showPasswordButtonTextField = TextField()
|
|
lazy var passwordSection = FormSection().with {
|
|
$0.title = "Password Settings"
|
|
$0.addFormRow(label: "Hide Button", view: hidePasswordButtonTextField)
|
|
$0.addFormRow(label: "Show Button", view: showPasswordButtonTextField)
|
|
$0.isHidden = true
|
|
}
|
|
|
|
//date
|
|
lazy var dateFormatPickerSelectorView = {
|
|
PickerSelectorView(title: "",
|
|
picker: self.picker,
|
|
items: InputField.DateFormat.allCases)
|
|
}()
|
|
|
|
lazy var dateSection = FormSection().with {
|
|
$0.title = "Date Settings"
|
|
$0.addFormRow(label: "Date Format", view: dateFormatPickerSelectorView)
|
|
$0.isHidden = true
|
|
}
|
|
|
|
//inlineAction
|
|
var inlineActionTextField = TextField()
|
|
lazy var inlineActionSection = FormSection().with {
|
|
$0.title = "inlineAction Settings"
|
|
$0.addFormRow(label: "Action Text", view: inlineActionTextField)
|
|
$0.isHidden = true
|
|
}
|
|
|
|
//securityCode
|
|
lazy var cardTypePickerSelectorView = {
|
|
PickerSelectorView(title: "",
|
|
picker: self.picker,
|
|
items: InputField.CreditCardType.allCases)
|
|
}()
|
|
|
|
lazy var securityCodeSection = FormSection().with {
|
|
$0.title = "Security Code Settings"
|
|
$0.addFormRow(label: "Card Type", view: cardTypePickerSelectorView)
|
|
$0.isHidden = true
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
addContentTopView(view: component)
|
|
setupPicker()
|
|
setupModel()
|
|
}
|
|
|
|
override func setupForm(){
|
|
super.setupForm()
|
|
|
|
|
|
let fieldType = FormSection().with {
|
|
$0.title = "Field Type Settings"
|
|
$0.addFormRow(label: "Field Type", view: inputTypePickerSelectorView)
|
|
}
|
|
|
|
let general = FormSection().with {
|
|
$0.title = "\n\nGeneral Settings"
|
|
}
|
|
|
|
general.addFormRow(label: "Surface", view: surfacePickerSelectorView)
|
|
general.addFormRow(label: "Disabled", view: disabledSwitch)
|
|
general.addFormRow(label: "Read Only", view: readonlySwitch)
|
|
general.addFormRow(label: "Required", view: requiredSwitch)
|
|
general.addFormRow(label: "Transparent Background", view: transparentBgSwitch)
|
|
general.addFormRow(label: "Label Text", view: labelTextField)
|
|
general.addFormRow(label: "Helper Text Placement", view: helperTextPlacementPickerSelectorView)
|
|
general.addFormRow(label: "Helper Text", view: helperTextField)
|
|
general.addFormRow(label: "Error", view: showErrorSwitch)
|
|
general.addFormRow(label: "Error Text", view: errorTextField)
|
|
general.addFormRow(label: "Success", view: showSuccessSwitch)
|
|
general.addFormRow(label: "Success Text", view: successTextField)
|
|
general.addFormRow(label: "Width", view: widthTextField)
|
|
general.addFormRow(label: "ToolTip Title", view: tooltipTitleTextField)
|
|
general.addFormRow(label: "ToolTip Content", view: tooltipContentTextField)
|
|
|
|
append(section: fieldType)
|
|
append(section: passwordSection)
|
|
append(section: dateSection)
|
|
append(section: inlineActionSection)
|
|
append(section: securityCodeSection)
|
|
append(section: general)
|
|
|
|
requiredSwitch.onChange = { [weak self] sender in
|
|
self?.component.isRequired = sender.isOn
|
|
}
|
|
|
|
showErrorSwitch.onChange = { [weak self] sender in
|
|
guard let self else { return }
|
|
self.component.showError = sender.isOn
|
|
if self.component.showError != sender.isOn {
|
|
self.showErrorSwitch.isOn = self.component.showError
|
|
}
|
|
}
|
|
|
|
showSuccessSwitch.onChange = { [weak self] sender in
|
|
guard let self else { return }
|
|
self.component.showSuccess = sender.isOn
|
|
if self.component.showSuccess != sender.isOn {
|
|
self.showSuccessSwitch.isOn = self.component.showSuccess
|
|
}
|
|
}
|
|
|
|
disabledSwitch.onChange = { [weak self] sender in
|
|
self?.component.isEnabled = !sender.isOn
|
|
}
|
|
|
|
readonlySwitch.onChange = { [weak self] sender in
|
|
self?.component.isReadOnly = sender.isOn
|
|
}
|
|
|
|
transparentBgSwitch.onChange = { [weak self] sender in
|
|
self?.component.transparentBackground = sender.isOn
|
|
}
|
|
|
|
labelTextField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
self?.component.labelText = text
|
|
}.store(in: &subscribers)
|
|
|
|
helperTextField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
self?.component.helperText = text
|
|
}.store(in: &subscribers)
|
|
|
|
errorTextField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
self?.component.errorText = text
|
|
}.store(in: &subscribers)
|
|
|
|
widthTextField
|
|
.numberPublisher
|
|
.sink { [weak self] number in
|
|
self?.component.width = number?.cgFloatValue
|
|
}.store(in: &subscribers)
|
|
|
|
tooltipTitleTextField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
self?.updateTooltip()
|
|
}.store(in: &subscribers)
|
|
|
|
tooltipContentTextField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
self?.updateTooltip()
|
|
}.store(in: &subscribers)
|
|
|
|
//field types
|
|
//password
|
|
hidePasswordButtonTextField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
self?.component.hidePasswordButtonText = text
|
|
}.store(in: &subscribers)
|
|
|
|
showPasswordButtonTextField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
self?.component.showPasswordButtonText = text
|
|
}.store(in: &subscribers)
|
|
|
|
//inlineAction
|
|
inlineActionTextField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
if !text.isEmpty {
|
|
self?.component.actionTextLinkModel = .init(text: text, onClick: { inputField in
|
|
var value = inputField.value ?? ""
|
|
value = !value.isEmpty ? value : "nil"
|
|
self?.present(UIAlertController(title: "inlineAction", message: "Clicked and you get the value: \(value)", preferredStyle: .alert).with{ $0.addAction(.init(title: "OK", style: .default)) }, animated: true)
|
|
})
|
|
} else {
|
|
self?.component.actionTextLinkModel = nil
|
|
}
|
|
}.store(in: &subscribers)
|
|
}
|
|
|
|
func setupModel() {
|
|
component.fieldType = .text
|
|
component.labelText = "Street Address"
|
|
component.helperText = "For example: 123 Verizon St"
|
|
component.errorText = "Enter a valid address."
|
|
component.successText = "Good job entering a valid address!"
|
|
|
|
component.onChange = { component in
|
|
if let text = component.value {
|
|
print("text entry: \(text)")
|
|
} else {
|
|
print("text entry: null")
|
|
}
|
|
}
|
|
|
|
//setup UI
|
|
surfacePickerSelectorView.text = component.surface.rawValue
|
|
helperTextPlacementPickerSelectorView.text = component.helperTextPlacement.rawValue
|
|
dateFormatPickerSelectorView.text = component.dateFormat.rawValue
|
|
inputTypePickerSelectorView.text = component.fieldType.rawValue
|
|
disabledSwitch.isOn = !component.isEnabled
|
|
requiredSwitch.isOn = component.isRequired
|
|
labelTextField.text = component.labelText
|
|
helperTextField.text = component.helperText
|
|
showErrorSwitch.isOn = component.showError
|
|
errorTextField.text = component.errorText
|
|
showSuccessSwitch.isOn = component.showSuccess
|
|
successTextField.text = component.successText
|
|
tooltipTitleTextField.text = component.tooltipModel?.title
|
|
tooltipContentTextField.text = component.tooltipModel?.content
|
|
if let width = component.width {
|
|
widthTextField.text = String(describing: width)
|
|
}
|
|
}
|
|
|
|
//Picker
|
|
func setupPicker(){
|
|
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.component.surface = item
|
|
self?.contentTopView.backgroundColor = item.color
|
|
}
|
|
|
|
helperTextPlacementPickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.component.helperTextPlacement = item
|
|
}
|
|
|
|
inputTypePickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.component.fieldType = item
|
|
self?.component.text = ""
|
|
self?.updateFormSections()
|
|
}
|
|
|
|
dateFormatPickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.component.dateFormat = item
|
|
self?.updateFormSections()
|
|
}
|
|
|
|
cardTypePickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.component.cardType = item
|
|
}
|
|
}
|
|
|
|
func updateTooltip() {
|
|
let title = tooltipTitleTextField.text ?? ""
|
|
let content = tooltipContentTextField.text ?? ""
|
|
|
|
component.tooltipModel = !title.isEmpty || !content.isEmpty ? .init(title: title,
|
|
content: content) : nil
|
|
}
|
|
|
|
func updateFormSections() {
|
|
[passwordSection, dateSection, inlineActionSection, securityCodeSection].forEach { $0.isHidden = true }
|
|
//reset other fields
|
|
component.actionTextLinkModel = nil
|
|
component.tooltipModel = nil
|
|
component.cardType = .generic
|
|
tooltipTitleTextField.text = nil
|
|
tooltipContentTextField.text = nil
|
|
dateFormatPickerSelectorView.text = component.dateFormat.rawValue
|
|
cardTypePickerSelectorView.text = component.cardType.rawValue
|
|
switch component.fieldType {
|
|
case .inlineAction:
|
|
inlineActionTextField.text = nil
|
|
inlineActionSection.isHidden = false
|
|
|
|
case .password:
|
|
passwordSection.isHidden = false
|
|
|
|
case .date:
|
|
dateSection.isHidden = false
|
|
|
|
case .securityCode:
|
|
securityCodeSection.isHidden = false
|
|
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
extension InputFieldViewController: ComponentSampleable {
|
|
static func makeSample() -> ComponentSample {
|
|
let component = Self.makeComponent()
|
|
component.fieldType = .text
|
|
component.width = 328
|
|
component.labelText = "Street Address"
|
|
component.helperText = "For example: 123 Verizon St"
|
|
component.errorText = "Enter a valid address."
|
|
component.successText = "Good job entering a valid address!"
|
|
component.tooltipModel = .init(title: "Check the formatting of your address", content: "House/Building number then street name")
|
|
return ComponentSample(component: component, trailingPinningType: .lessThanOrEqual)
|
|
}
|
|
}
|