Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS: Fix textField formating and transform #15

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions components/ios/base/PilotTextFieldView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public struct PilotTextFieldView<Label>: View where Label: View {
@ObservedObject private var autoCorrect: StateObservable<KotlinBoolean>
@ObservedObject private var autoCapitalization: StateObservable<PilotKeyboardAutoCapitalization>

@State private var textFieldText: String

public init(_ pilotTextField: PilotTextField, placeholderBuilder: @escaping (String) -> Label) {
self.pilotTextField = pilotTextField
self.placeholderBuilder = placeholderBuilder
Expand All @@ -24,19 +26,12 @@ public struct PilotTextFieldView<Label>: View where Label: View {
_contentType = ObservedObject(wrappedValue: StateObservable(pilotTextField.contentType))
_autoCorrect = ObservedObject(wrappedValue: StateObservable(pilotTextField.autoCorrect))
_autoCapitalization = ObservedObject(wrappedValue: StateObservable(pilotTextField.autoCapitalization))
}

private var textBinding: Binding<String> {
Binding {
pilotTextField.formatText(pilotTextField.transformText(text.value))
} set: { newValue in
let unformattedText = pilotTextField.unformatText(newValue)
pilotTextField.onValueChange(text: unformattedText)
}
_textFieldText = State(initialValue: pilotTextField.formatText(pilotTextField.transformText(pilotTextField.text.value)))
}

public var body: some View {
TextField(text: textBinding) {
TextField(text: $textFieldText) {
placeholderBuilder(placeholder.value)
}
.onSubmit {
Expand All @@ -48,6 +43,14 @@ public struct PilotTextFieldView<Label>: View where Label: View {
.disableAutocorrection(!autoCorrect.value.boolValue)
.autocapitalization(autoCapitalization.value.uiTextAutocapitalizationType)
.textFieldStyle(ExtendedTapAreaTextFieldStyle())
.onChange(of: text.value) { newValue in
textFieldText = pilotTextField.formatText(newValue)
}
.onChange(of: textFieldText) { newValue in
let unformattedText = pilotTextField.unformatText(newValue)
textFieldText = pilotTextField.formatText(pilotTextField.transformText(unformattedText))
pilotTextField.onValueChange(text: unformattedText)
}
}
}

Expand Down
Loading