/
DateField.swift
93 lines (86 loc) · 3.34 KB
/
DateField.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// Copyright © 2023 Alexander Romanov
// DateField.swift, created on 26.02.2023
//
import SwiftUI
#if os(iOS)
@available(iOS 15.0, *)
@available(macOS, unavailable)
@available(watchOS, unavailable)
@available(tvOS, unavailable)
public struct DateField: View {
@Environment(\.theme) private var theme: ThemeSettings
@Environment(\.fieldLabelPosition) private var fieldPlaceholderPosition: FieldLabelPosition
@Binding private var selection: Date
@Binding private var optionalSelection: Date?
private let label: String
@State private var showModal = false
let isOptionalSelection: Bool
public init(
_ sheetTitle: String = "Date",
selection: Binding<Date>
) {
label = sheetTitle
_selection = selection
_optionalSelection = .constant(nil)
isOptionalSelection = false
}
public init(
_ label: String = "Date",
selection: Binding<Date?>
) {
self.label = label
_selection = .constant(Date())
_optionalSelection = selection
isOptionalSelection = true
}
public var body: some View {
VStack(alignment: .leading, spacing: .xSmall) {
if fieldPlaceholderPosition == .adjacent {
HStack {
Text(label)
.subheadline(.medium)
.foregroundColor(.onSurfaceHighEmphasis)
Spacer()
}
}
Button {
showModal.toggle()
} label: {
VStack(alignment: .leading, spacing: .xxxSmall) {
if fieldPlaceholderPosition == .overInput {
Text(label)
.font(.subheadline)
.fontWeight(.semibold)
.onSurfaceDisabledForegroundColor()
}
HStack {
if isOptionalSelection, let optionalSelection {
Text(optionalSelection.formatted(date: .long, time: .shortened))
} else if isOptionalSelection {
Text(label)
} else {
Text(selection.formatted(date: .long, time: .shortened))
}
Spacer()
Icons.Base.calendar.outline
// Icon(.calendar, color: .onSurfaceHighEmphasis)
}
}
}
.buttonStyle(.field)
}
.sheet(isPresented: $showModal) {
if isOptionalSelection {
DatePickerSheet(title: label, selection: $optionalSelection)
.presentationDetents([.height(500)])
.presentationDragIndicator(.hidden)
} else {
DatePickerSheet(title: label, selection: $selection)
.presentationDetents([.height(500)])
.presentationDragIndicator(.hidden)
}
}
}
}
#endif