Skip to content

Commit

Permalink
support Text Input block
Browse files Browse the repository at this point in the history
  • Loading branch information
RyosukeCla committed Dec 18, 2023
1 parent 6ffb449 commit 377b83b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ios/Nativebrik/Nativebrik/block.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func uiblockToUIView(data: UIBlock, context: UIBlockContext) -> UIView {
return ImageView(block: image, context: context)
case .EUITextBlock(let text):
return TextView(block: text, context: context)
case .EUITextInputBlock(let input):
return TextInputView(block: input)
default:
return UIView(frame: .zero)
}
Expand Down
94 changes: 94 additions & 0 deletions ios/Nativebrik/Nativebrik/forms.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// form.swift
// Nativebrik
//
// Created by Ryosuke Suzuki on 2023/12/14.
//

import Foundation
import UIKit
import YogaKit

class TextInputView: UIView, UITextFieldDelegate {
var textInput: UITextField? = nil

required init?(coder: NSCoder) {
super.init(coder: coder)
}

init(block: UITextInputBlock) {
super.init(frame: .zero)

// wrap layout
self.configureLayout { layout in
layout.isEnabled = true
layout.width = YGValueUndefined
layout.width = .init(value: 100.0, unit: .percent)
layout.flexShrink = 1
}
configureBorder(view: self, frame: block.data?.frame)

// toolbar for input
let toolbar = UIToolbar()
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(doneButtonTapped))
toolbar.setItems([space, doneButton], animated: true)
toolbar.sizeToFit()

// input layout
let textInput = UITextField()
self.textInput = textInput
textInput.textAlignment = parseTextAlign(block.data?.textAlign)

textInput.inputAccessoryView = toolbar
textInput.delegate = self
textInput.configureLayout { layout in
layout.isEnabled = true
layout.width = .init(value: 100.0, unit: .percent)
configurePadding(layout: layout, frame: block.data?.frame)
}
if let paddingLeft = block.data?.frame?.paddingLeft {
textInput.leftView = UIView(frame: CGRect(x: 0, y: 0, width: paddingLeft, height: 0))
textInput.leftViewMode = .always
}
if let paddingRight = block.data?.frame?.paddingRight {
textInput.rightView = UIView(frame: CGRect(x: 0, y: 0, width: paddingRight, height: 0))
textInput.rightViewMode = .always
}
if let color = block.data?.color {
textInput.textColor = parseColor(color)
} else {
textInput.textColor = .label
}
textInput.font = parseTextBlockDataToUIFont(block.data?.size, block.data?.weight, block.data?.design)
if let placeholder = block.data?.placeholder {
textInput.placeholder = placeholder
}
if let autocorrect = block.data?.autocorrect {
if autocorrect {
textInput.autocorrectionType = .yes
} else {
textInput.autocorrectionType = .no
}
}
if let secure = block.data?.secure {
textInput.isSecureTextEntry = secure
}
textInput.keyboardType = parserKeyboardType(block.data?.keyboardType)

self.addSubview(textInput)
}

override func layoutSubviews() {
super.layoutSubviews()
}

@objc func doneButtonTapped() {
self.textInput?.resignFirstResponder()
}

// UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
}
}

0 comments on commit 377b83b

Please sign in to comment.