Skip to content

Commit

Permalink
Autofocus relevant fields
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Jul 29, 2024
1 parent f674fcc commit 5dbec08
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
8 changes: 2 additions & 6 deletions Fyreplace/Views/Screens/LoginScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ struct LoginScreen: View {
@SceneStorage("LoginScreen.identifier")
private var identifier = ""

@Namespace
private var mainNamespace

@FocusState
private var focused: Bool

Expand Down Expand Up @@ -37,12 +34,11 @@ struct LoginScreen: View {
.onSubmit(submit)
.accessibilityIdentifier("identifier")
.matchedGeometryEffect(id: "first-field", in: namespace)
#if os(macOS)
.prefersDefaultFocus(in: mainNamespace)
#else
#if !os(macOS)
.labelsHidden()
#endif
}
.onAppear { focused = identifier.isEmpty }

#if !os(macOS)
submitButton
Expand Down
17 changes: 12 additions & 5 deletions Fyreplace/Views/Screens/RegisterScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct RegisterScreen: View {
private var email = ""

@FocusState
private var focusedField: FocusedField?
private var focused: FocusedField?

private var isUsernameValid: Bool { 3 ... 50 ~= username.count }
private var isEmailValid: Bool { 3 ... 254 ~= email.count && email.contains("@") }
Expand Down Expand Up @@ -40,20 +40,27 @@ struct RegisterScreen: View {
TextField("Register.Username", text: $username, prompt: usernamePrompt)
.textContentType(.username)
.autocorrectionDisabled()
.focused($focusedField, equals: .username)
.focused($focused, equals: .username)
.submitLabel(.next)
.onSubmit { focusedField = .email }
.onSubmit { focused = .email }
.accessibilityIdentifier("username")
.matchedGeometryEffect(id: "first-field", in: namespace)

TextField("Register.Email", text: $email, prompt: emailPrompt)
.textContentType(.email)
.autocorrectionDisabled()
.focused($focusedField, equals: .email)
.focused($focused, equals: .email)
.submitLabel(.done)
.onSubmit(submit)
.accessibilityIdentifier("email")
}
.onAppear {
if username.isEmpty {
focused = .username
} else if email.isEmpty {
focused = .email
}
}

#if !os(macOS)
submitButton
Expand All @@ -64,7 +71,7 @@ struct RegisterScreen: View {

private func submit() {
guard canSubmit else { return }
focusedField = nil
focused = nil
}
}

Expand Down

0 comments on commit 5dbec08

Please sign in to comment.