// // TextEntryFieldViewController.swift // VDSSample // // Created by Matt Bruce on 10/3/22. // import Foundation import UIKit import VDS import VDSColorTokens import Combine class InputFieldViewController: BaseViewController { lazy var helperTextPlacementPickerSelectorView = { PickerSelectorView(title: "", picker: self.picker, items: InputField.HelperTextPlacement.allCases) }() var disabledSwitch = Toggle() var requiredSwitch = 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() override func viewDidLoad() { super.viewDidLoad() addContentTopView(view: component) setupPicker() setupModel() } override func setupForm(){ super.setupForm() addFormRow(label: "Disabled", view: disabledSwitch) addFormRow(label: "Required", view: requiredSwitch) addFormRow(label: "Surface", view: surfacePickerSelectorView) addFormRow(label: "Label Text", view: labelTextField) addFormRow(label: "Helper Text Placement", view: helperTextPlacementPickerSelectorView) addFormRow(label: "Helper Text", view: helperTextField) addFormRow(label: "Error", view: showErrorSwitch) addFormRow(label: "Error Text", view: errorTextField) addFormRow(label: "Success", view: showSuccessSwitch) addFormRow(label: "Success Text", view: successTextField) addFormRow(label: "Width", view: widthTextField) addFormRow(label: "ToolTip Title", view: tooltipTitleTextField) addFormRow(label: "ToolTip Content", view: tooltipContentTextField) requiredSwitch.onChange = { [weak self] sender in self?.component.required = 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 } 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?.component.tooltipTitle = text }.store(in: &subscribers) tooltipContentTextField .textPublisher .sink { [weak self] text in self?.component.tooltipContent = text }.store(in: &subscribers) } func setupModel() { component.type = .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.tooltipTitle = "Check the formatting of your address" component.tooltipContent = "House/Building number then street name" component .publisher(for: .valueChanged) .sink { component in if let text = component.value { print("text entry: \(text)") } else { print("text entry: null") } }.store(in: &subscribers) //setup UI surfacePickerSelectorView.text = component.surface.rawValue helperTextPlacementPickerSelectorView.text = component.helperTextPlacement.rawValue disabledSwitch.isOn = !component.isEnabled requiredSwitch.isOn = component.required 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.tooltipTitle tooltipContentTextField.text = component.tooltipContent 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 } } } extension InputFieldViewController: ComponentSampleable { static func makeSample() -> ComponentSample { let component = Self.makeComponent() component.type = .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.tooltipTitle = "Check the formatting of your address" component.tooltipContent = "House/Building number then street name" return ComponentSample(component: component, trailingPinningType: .lessThanOrEqual) } }