From b528f1c7309e064663404135f1394f4076fcaf5f Mon Sep 17 00:00:00 2001 From: Josh Cockrell Date: Wed, 23 Apr 2025 12:49:04 -0700 Subject: [PATCH] Add focus state binding example for child view --- Sources/Showcase/FocusStatePlayground.swift | 29 +++++++++++++++++++ .../Showcase/Resources/Localizable.xcstrings | 3 ++ 2 files changed, 32 insertions(+) diff --git a/Sources/Showcase/FocusStatePlayground.swift b/Sources/Showcase/FocusStatePlayground.swift index 96117ea..00dfeb2 100644 --- a/Sources/Showcase/FocusStatePlayground.swift +++ b/Sources/Showcase/FocusStatePlayground.swift @@ -4,6 +4,7 @@ import SwiftUI struct FocusStatePlayground: View { @State var textA = "" @State var textB = "" + @State var textC = "" @FocusState var focusBool: Bool @FocusState var focusEnum: FocusField? @@ -18,15 +19,43 @@ struct FocusStatePlayground: View { .focused($focusEnum, equals: .textA) TextField("textB", text: $textB) .focused($focusEnum, equals: .textB) + + ChildTextField( + title: "textC", + value: $textC, + focusedField: $focusEnum, + assignedFocusField: FocusField.textC + ) + Button("Set focus to A via Boolean") { focusBool = true } Button("Set focus to A via Enum") { focusEnum = .textA } Button("Set focus to B via Enum") { focusEnum = .textB } + Button("Set focus to C via Enum") { focusEnum = .textC } Button("Nil Enum") { focusEnum = nil } } } } +struct ChildTextField: View { + + let title: String + @Binding var value: String + + #if !SKIP + @FocusState.Binding var focusedField: FocusField? + #else + @Binding var focusedField: FocusField? + #endif + let assignedFocusField: FocusField + + var body: some View { + TextField(title, text: $value) + .focused($focusedField, equals: assignedFocusField) + } +} + enum FocusField: Int { case textA case textB + case textC } diff --git a/Sources/Showcase/Resources/Localizable.xcstrings b/Sources/Showcase/Resources/Localizable.xcstrings index 235ef07..db4a4da 100644 --- a/Sources/Showcase/Resources/Localizable.xcstrings +++ b/Sources/Showcase/Resources/Localizable.xcstrings @@ -2246,6 +2246,9 @@ }, "Set focus to B via Enum" : { + }, + "Set focus to C via Enum" : { + }, "Settings" : { "localizations" : {