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

Feat: Improve focus/first responder handling in SwiftUI #85

Merged
merged 19 commits into from
Mar 24, 2022

Conversation

marinofelipe
Copy link
Owner

Why?

To provide a better API for making CurrencyTextField the first responder in SwiftUI.

Context

CurrencyTextField is currently bridged from UIKit to SwiftUI, so iOS 15's focus API doesn't work for it.
The only way to make a CurrencyTextField the first responder programmatically is as indicated by YeungKC on #81:

textFieldConfiguration: { uiTextField in
    DispatchQueue.main.async {
        if !becomeFirstResponderCalled {
            uiTextField.becomeFirstResponder()
        }
        becomeFirstResponderCalled = true
    }
}

which is error prone and ugly.

Changes

This PR adds a new property to CurrencyTextFieldConfiguration called hasFocus, an OptionalBinding to drive an listen to the first responder state of a CurrencyTextField wrapped UITextField.

The usage is as follows:

struct SwiftUIExampleView2: View {
    @State private var hasFocus: Bool? = false // or this initially as true
    @State private var text = ""

    var body: some View {
        CurrencyTextField(
            configuration: .init(
                placeholder: "type here...",
                text: $text,
                hasFocus: $hasFocus,
                formatter: .default,
                textFieldConfiguration: { uiTextField in
                    uiTextField.borderStyle = .roundedRect
                    uiTextField.font = UIFont.preferredFont(forTextStyle: .body)
                },
                onEditingChanged: { isEditing in
                    print("onEditingChanged: \(isEditing)")
                },
                onCommit: {
                    print("onCommit")
                }
            )
        )
        .onAppear {
            hasFocus = true
        }
    }
}

Tests

Tests were updated and CurrencyTextFieldConfigurationTests was extended.

Related issue: #81

@marinofelipe marinofelipe self-assigned this Mar 20, 2022
@marinofelipe marinofelipe added the work in progress Currently being worked on label Mar 20, 2022
@codecov-commenter
Copy link

Codecov Report

❗ No coverage uploaded for pull request base (main@1d9a6aa). Click here to learn what that means.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main      #85   +/-   ##
=======================================
  Coverage        ?   95.65%           
=======================================
  Files           ?       22           
  Lines           ?     1104           
  Branches        ?        0           
=======================================
  Hits            ?     1056           
  Misses          ?       48           
  Partials        ?        0           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1d9a6aa...7444dfc. Read the comment docs.

@marinofelipe marinofelipe removed the work in progress Currently being worked on label Mar 24, 2022
@marinofelipe marinofelipe merged commit 41ab9ad into main Mar 24, 2022
@marinofelipe marinofelipe deleted the feat/swift-ui-focus branch March 24, 2022 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants