190 lines
6.5 KiB
Swift
190 lines
6.5 KiB
Swift
//
|
|
// TextAreaViewController.swift
|
|
// VDSSample
|
|
//
|
|
// Created by Matt Bruce on 1/10/23.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDS
|
|
import VDSTokens
|
|
import Combine
|
|
|
|
class TextAreaViewController: BaseViewController<TextArea> {
|
|
|
|
var disabledSwitch = Toggle()
|
|
var requiredSwitch = Toggle()
|
|
var labelTextField = TextField()
|
|
var errorTextField = TextField()
|
|
var helperTextField = TextField()
|
|
var widthTextField = NumericField()
|
|
var showErrorSwitch = Toggle()
|
|
var tooltipTitleTextField = TextField()
|
|
var tooltipContentTextField = TextField()
|
|
var maxLengthTextField = NumericField()
|
|
var readOnlySwitch = Toggle()
|
|
|
|
lazy var heightPickerSelectorView = {
|
|
PickerSelectorView(title: "2X",
|
|
picker: self.picker,
|
|
items: TextArea.Height.allCases)
|
|
}()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
addContentTopView(view: component)
|
|
component.text = "Starting Text"
|
|
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", view: helperTextField)
|
|
addFormRow(label: "Read Only", view: readOnlySwitch)
|
|
addFormRow(label: "Error", view: showErrorSwitch)
|
|
addFormRow(label: "Error Text", view: errorTextField)
|
|
addFormRow(label: "ToolTip Title", view: tooltipTitleTextField)
|
|
addFormRow(label: "ToolTip Content", view: tooltipContentTextField)
|
|
addFormRow(label: "Character Count", view: maxLengthTextField)
|
|
addFormRow(label: "Min Height", view: heightPickerSelectorView)
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
disabledSwitch.onChange = { [weak self] sender in
|
|
self?.component.isEnabled = !sender.isOn
|
|
}
|
|
|
|
readOnlySwitch.onChange = { [weak self] sender in
|
|
self?.component.readOnly = 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
|
|
guard let number else {
|
|
self?.component.width = nil
|
|
return
|
|
}
|
|
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)
|
|
|
|
maxLengthTextField
|
|
.numberPublisher
|
|
.sink { [weak self] number in
|
|
guard let number else {
|
|
self?.component.maxLength = nil
|
|
return
|
|
}
|
|
self?.component.maxLength = number.intValue
|
|
}.store(in: &subscribers)
|
|
}
|
|
|
|
func setupModel() {
|
|
component.width = 328
|
|
component.labelText = "Street Address"
|
|
component.helperText = "For example: 123 Verizon St"
|
|
component.errorText = "Enter a valid address."
|
|
component.tooltipModel = .init(title: "Check the formatting of your address", content:"House/Building number then street name")
|
|
|
|
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
|
|
disabledSwitch.isOn = !component.isEnabled
|
|
requiredSwitch.isOn = component.required
|
|
labelTextField.text = component.labelText
|
|
helperTextField.text = component.helperText
|
|
showErrorSwitch.isOn = component.showError
|
|
errorTextField.text = component.errorText
|
|
tooltipTitleTextField.text = component.tooltipModel?.title
|
|
tooltipContentTextField.text = component.tooltipModel?.content
|
|
}
|
|
|
|
//Picker
|
|
func setupPicker(){
|
|
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.component.surface = item
|
|
self?.contentTopView.backgroundColor = item.color
|
|
}
|
|
|
|
heightPickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.component.minHeight = item
|
|
}
|
|
}
|
|
|
|
func updateTooltip() {
|
|
let title = tooltipTitleTextField.text ?? ""
|
|
let content = tooltipContentTextField.text ?? ""
|
|
|
|
component.tooltipModel = !title.isEmpty || !content.isEmpty ? .init(title: title,
|
|
content: content) : nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extension TextAreaViewController: ComponentSampleable {
|
|
static func makeSample() -> ComponentSample {
|
|
let component = Self.makeComponent()
|
|
component.width = 328
|
|
component.labelText = "Street Address"
|
|
component.helperText = "For example: 123 Verizon St"
|
|
component.errorText = "Enter 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)
|
|
}
|
|
}
|