Skip to content

Instantly share code, notes, and snippets.

@StewartLynch
Last active February 8, 2026 19:33
Show Gist options
  • Select an option

  • Save StewartLynch/2f5dc9459478346dfc2830716ca03206 to your computer and use it in GitHub Desktop.

Select an option

Save StewartLynch/2f5dc9459478346dfc2830716ca03206 to your computer and use it in GitHub Desktop.
PersonForm Starter View for Video on SQLiteData
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