Skip to content

Commit

Permalink
readme updated
Browse files Browse the repository at this point in the history
  • Loading branch information
gluonfield committed Sep 2, 2021
1 parent c2c3f26 commit f874246
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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 designed to follow [Apple `@FocusState`](https://developer.apple.com/documentation/swiftui/focusstate) property wrapper. Since most of us cannot update our apps to serve iOS 15 exclusively `Focuser` will provide an easy way to accomplish the same thing.
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`](https://developer.apple.com/documentation/swiftui/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

Expand All @@ -26,11 +26,11 @@ To use Focuser you first need to `import Focuser` and define an `enum` correspon
import Focuser

enum FormFields {
case none, username, password, name
case username, password, name
}
```

`.none` is a special case used to reasign first responder. 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".
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`.

```swift
extension FormFields: FocusStateCompliant {
Expand All @@ -39,13 +39,13 @@ extension FormFields: FocusStateCompliant {
.name
}

var next: FormFields {
var next: FormFields? {
switch self {
case .username:
return .password
case .password:
return .name
default: return .none
default: return nil
}
}
}
Expand All @@ -55,7 +55,7 @@ Finally we can build our form

```swift
struct ContentView: View {
@FocusStateLegacy var focusedField: FormFields = .username
@FocusStateLegacy var focusedField: FormFields?
@State var username = ""
@State var password = ""
@State var name = ""
Expand All @@ -72,7 +72,7 @@ struct ContentView: View {
.focusedLegacy($focusedField, equals: .name)

Button(action: {
focusedField = .password
focusedField = FormFields.password
}) {
Text("Focus Password")
}
Expand All @@ -96,6 +96,15 @@ TextField("Username", text: $username)
.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`

0 comments on commit f874246

Please sign in to comment.