Created
October 30, 2021 10:01
-
-
Save diamantidis/a1b0ef4ad7f7ac0271b32fafbe79ef77 to your computer and use it in GitHub Desktop.
PickerField for medium post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct PickerField: UIViewRepresentable { | |
// MARK: - Public properties | |
@Binding var selectionIndex: Int? | |
// MARK: - Initializers | |
init<S>(_ title: S, data: [String], selectionIndex: Binding<Int?>) where S: StringProtocol { | |
self.placeholder = String(title) | |
self.data = data | |
self._selectionIndex = selectionIndex | |
textField = PickerTextField(data: data, selectionIndex: selectionIndex) | |
} | |
// MARK: - Public methods | |
func makeUIView(context: UIViewRepresentableContext<PickerField>) -> UITextField { | |
textField.placeholder = placeholder | |
return textField | |
} | |
func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<PickerField>) { | |
if let index = selectionIndex { | |
uiView.text = data[index] | |
} else { | |
uiView.text = "" | |
} | |
} | |
// MARK: - Private properties | |
private var placeholder: String | |
private var data: [String] | |
private let textField: PickerTextField | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment