Last active
February 21, 2023 13:56
-
-
Save Fiser12/62ef54ba0048e5b62cf2f2a61f279492 to your computer and use it in GitHub Desktop.
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
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() | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made a lot of corrections on this gist because I noticed that the previous version led to memory leaks