Git Product home page Git Product logo

swift-focuser's Introduction

Focuser

Focuser allows to focus SwiftUI text fields dynamically and implements ability move go through the form using Keyboard for iOS 13 and iOS 14. Implementation is modeled to follow Apple @FocusState property wrapper however instead of however instead of @FocusState we use @FocusStateLegacy and for .focused(...) we use .focusedLegacy(...). Since most of us cannot update our apps to serve iOS 15 exclusively Focuser will provide an easy way to change first responder and connect to keyboard "next"/"done" buttons.

Preview

Preview gif of Focuser

Install

We are going to maintain Focuser and extend its functionality in the near future. You can use Focuser in your project using SPM

File > Swift Packages > Add Package Dependency and use

[email protected]:art-technologies/swift-focuser.git

Example

Feel free to download full Xcode example project in Example folder.

To use Focuser you first need to import Focuser and define an enum corresponding to text fields.

import Focuser

enum FormFields {
    case username, password, name
}

Since Focuser allows to focus keyboard to the next text fields using keybaord we have to provide additional information of what the next field should be. We provide this using extension on our struct comforming to FocusStateCompliant protocol. In addition of providing computed variable next we also provide last. This indicates to Focuser when it should show "done" keyboard button instead of "next". To resign first responder (hide keyboard) set your focusedField to nil.

extension FormFields: FocusStateCompliant {

    static var last: FormFields {
        .name
    }

    var next: FormFields? {
        switch self {
        case .username:
            return .password
        case .password:
            return .name
        default: return nil
        }
    }
}

Finally we can build our form

struct ContentView: View {
    @FocusStateLegacy var focusedField: FormFields?
    @State var username = ""
    @State var password = ""
    @State var name = ""

    var body: some View {
        VStack{
            TextField("Username", text: $username)
                .focusedLegacy($focusedField, equals: .username)

            TextField("Password", text: $password)
                .focusedLegacy($focusedField, equals: .password)

            TextField("Name", text: $name)
                .focusedLegacy($focusedField, equals: .name)

            Button(action: {
                focusedField = FormFields.password
            }) {
                Text("Focus Password")
            }
        }
        .padding()
    }
}

Here we introduced "Focus Password" button showing how to focus a specific text field dynamically.

Caveats

Make sure to apply .focusedLegacy modifier as the last modifier to TextField. I will make a fix later on to aleviate the order issue.

TextField("Username", text: $username)
   .padding(9)
   .background(Color(.systemGray6))
   .cornerRadius(8)
   .focusedLegacy($focusedField, equals: .username)

Comparison to iOS 15 @FocusState

The API is analogous and our property wrapper has exactly the same definition. If you ever decide to switch to iOS 15 wrapper, all you need to do is replace

@FocusStateLegacy -> @FocusState

.focusedLegacy(...) -> .focused(...)

However, Focuser additionally offers to show different keyboard return button such as "next" or "done" based on where you are in the form.

To do

  • Support for TextEditor

swift-focuser's People

Contributors

augustdev avatar jamesroome avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

swift-focuser's Issues

Requires that 'FormFields' conform to 'FocusStateCompliant'

What's wrong with this code?

enum FormFields {
	case firstName, lastName, email, password
}

struct SignUpView: View {
	@FocusStateLegacy var focusedField: FormFields?
	@State var firstName = ""

	var body: some View {
			TextField(
				"First name",
				text: $firstName
			)
			.focusedLegacy($focusedField, equals: .firstName)
	}
}

The issue is "Instance method 'focusedLegacy(_:equals:)' requires that 'FormFields' conform to 'FocusStateCompliant'"

Softkeyboard Done button cannot trigger submit button

After tapping through any input fields with "next" on the soft keyboard, reaching the last field the "done" button appears which when tapped should trigger the submit button under an input form but has no effect on it. It seems focuser can be applied on a button but has no effect on it.

Keyboard reappears after pressing done

Hello, I have an issue with the library. Whenever I press the done button, the keyboard disappears for a second or two, and then reappears by itself. Whenever that happens, I get a bunch of debug messages. The messages are === AttributeGraph: cycle detected through attribute 804744 === (the attribute number changes on each new message). I also get those messages whenever I press the next button. Any ideas what could be the problem? Thanks in advance!

Softkeyboard appears again when pressing a Toggle on the same view

I've got one TextField in my view which gets focus .onAppear. The Done button closes the Softkeyboard correctly. If I'm interacting with any Buttons, Toggles in the same List the last focused TextField gets focus again and the Softkeyboard appears unexpectedly.

Does this work with SecureField as well?

Sorry but I'm not sure the best place to ask this question since there's not a discussion area. Does swift-focuser work properly with SecureField as well? The example in the readme shows a password field but it's a (clear) TextField. Obviously most login screens need this to be a secure field.

Thanks - hoping the answer is yes as this looks like a great solution to a glaring omission in SwiftUI pre iOS15

Keyboard reopens if there is a geometry reader

Hi, first thanks for package.

There is an issue if Textfield is within Geometry reader, last one keeps keyboard reopening because of view redrawn.

GeometryReader { metrics in
VStack{
TextField("Username", text: $username)
.padding(9)
.background(focusedField == .username ? Color.red : Color(.systemGray6))
.cornerRadius(8)
.focusedLegacy($focusedField, equals: .username)

        TextField("Password", text: $password)
            .padding(9)
            .background(focusedField == .password ? Color.red : Color(.systemGray6))
            .cornerRadius(8)
            .focusedLegacy($focusedField, equals: .password)
        
        TextField("Name", text: $name)
            .padding(9)
            .background(focusedField == .name ? Color.red : Color(.systemGray6))
            .cornerRadius(8)
            .focusedLegacy($focusedField, equals: .name)

}
}

Thanks,
Aram

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.