Last active
February 8, 2026 19:33
-
-
Save StewartLynch/2f5dc9459478346dfc2830716ca03206 to your computer and use it in GitHub Desktop.
PersonForm Starter View for Video on SQLiteData
This file contains hidden or 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
| import SwiftUI | |
| struct PersonForm: View { | |
| @State private var name = "" | |
| @State private var birthDate: Date? | |
| @State private var notes = "" | |
| private var dateBinding: Binding<Date> { | |
| Binding { | |
| birthDate ?? Date.now | |
| } set: { setDate in | |
| birthDate = setDate | |
| } | |
| } | |
| @Environment(\.dismiss) var dismiss | |
| var body: some View { | |
| NavigationStack { | |
| Form { | |
| TextField("Name", text: $name) | |
| if birthDate != nil { | |
| HStack { | |
| DatePicker("Birthdate", selection: dateBinding, displayedComponents: .date) | |
| Button { | |
| birthDate = nil | |
| } label: { | |
| Image(systemName: "xmark.circle.fill") | |
| } | |
| } | |
| } else { | |
| HStack { | |
| Text("Birthdate") | |
| Spacer() | |
| Button("Add Birthdate") { | |
| birthDate = Date.now | |
| } | |
| .buttonStyle(.borderedProminent) | |
| } | |
| } | |
| TextField("Notes", text: $notes, axis: .vertical) | |
| } | |
| .navigationTitle("Person") | |
| .navigationBarTitleDisplayMode(.inline) | |
| .toolbar { | |
| ToolbarItem(placement: .confirmationAction){ | |
| Button(role: .confirm) { | |
| dismiss() | |
| } | |
| } | |
| ToolbarItem(placement: .cancellationAction) { | |
| Button(role: .close) { | |
| dismiss() | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| #Preview { | |
| PersonForm() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment