Skip to content

Instantly share code, notes, and snippets.

@Fiser12
Last active February 21, 2023 13:56
Show Gist options
  • Save Fiser12/62ef54ba0048e5b62cf2f2a61f279492 to your computer and use it in GitHub Desktop.
Save Fiser12/62ef54ba0048e5b62cf2f2a61f279492 to your computer and use it in GitHub Desktop.
import SwiftUI
struct DatePickerNullable: View {
let title: String
@Binding var selected: Date?
let defaultDate: Date
var body: some View {
HStack {
if let date = Binding($selected) {
DatePicker(
title,
selection: date,
displayedComponents: [.date, .hourAndMinute]
)
.datePickerStyle(.field)
Button(action: {
selected = nil
}) {
Image(systemName: "xmark.circle").font(.title2)
}
.padding(.trailing)
} else {
Button(action: {
selected = defaultDate
}) {
HStack {
Text("\(title) is empty")
Image(systemName: "plus.circle")
}
}
.background(Color.clear)
Spacer()
}
}
}
}
@Fiser12
Copy link
Author

Fiser12 commented Feb 21, 2023

I made a lot of corrections on this gist because I noticed that the previous version led to memory leaks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment