generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dramatically simplfy validation code by not relying on focus state type
- Loading branch information
Showing
19 changed files
with
306 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// SwiftUIView.swift | ||
// | ||
// | ||
// Created by Andreas Bauer on 03.11.23. | ||
// | ||
|
||
import SwiftUI | ||
/* | ||
struct ChildView: View { | ||
@State var text: String = "" | ||
@State var isInFocus: Bool = false | ||
@FocusState var focusState: String? | ||
|
||
var body: some View { | ||
TextField("Input A", text: text) | ||
} | ||
} | ||
|
||
struct FooView: View { | ||
var body: some View { | ||
ChildView() | ||
} | ||
} | ||
*/ | ||
struct ChildView: View { | ||
private let identifier: String | ||
|
||
@State var text: String = "" | ||
@FocusState var isInFocus: Bool | ||
@FocusState.Binding var focusState: String? | ||
|
||
var body: some View { | ||
Text("Has Focus: \(isInFocus ? "Yes": "No")") | ||
if let focusState { | ||
Text("Focus State: \(focusState)") | ||
} | ||
TextField("Input A", text: $text) | ||
.focused($focusState, equals: identifier) | ||
.focused($isInFocus) | ||
} | ||
|
||
|
||
init(id: String, focus: FocusState<String?>.Binding) { | ||
self.identifier = id | ||
self._focusState = focus | ||
} | ||
} | ||
|
||
|
||
struct FocusStateTest: View { | ||
@FocusState private var state: String? | ||
|
||
var body: some View { | ||
List { | ||
Section { | ||
ChildView(id: "A", focus: $state) | ||
} | ||
|
||
Section { | ||
ChildView(id: "B", focus: $state) | ||
} | ||
|
||
Section("Set Focus") { | ||
Button("A") { | ||
state = "A" | ||
} | ||
Button("B") { | ||
state = "B" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
FocusStateTest() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.