@@ -7,13 +7,26 @@ import SwiftUI
77///
88/// ## Usage
99/// ```swift
10- /// @State var selection : Date = .init(timeIntervalSince1970: 0.0)
10+ /// @State var customizedDate : Date = .init(timeIntervalSince1970: 0.0)
1111/// @State var isRequired = false
1212/// @State var showsErrorMessage = false
13+ /// @State var customizedPickerVisible = false
14+ /// let customizedDateFormatter: DateFormatter = {
15+ /// let formatter = DateFormatter()
16+ /// formatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
17+ /// return formatter
18+ /// }()
19+ /// func mandatoryFieldIndicator() -> TextOrIcon {
20+ /// var indicator = AttributedString("*")
21+ /// indicator.font = .fiori(forTextStyle: .title3)
22+ /// indicator.foregroundColor = Color.preferredColor(.indigo7)
23+ /// return .text(indicator)
24+ /// }
25+ /// @State var customizedPickerVisible = false
1326///
14- /// DateTimePicker(title: "Default ", isRequired: self.isRequired, selectedDate: self.$selection )
15- /// .informationView(isPresented: self.$showsErrorMessage, description: AttributedString("The Date should be before December. "))
16- /// .informationViewStyle(.informational )
27+ /// DateTimePicker(title: "Customized Date Formatter, locale and calendar ", mandatoryFieldIndicator: self.mandatoryFieldIndicator(), isRequired: self.isRequired, selectedDate: self.$customizedDate, dateFormatter: self.customizedDateFormatter, pickerVisible: self.$customizedPickerVisible )
28+ /// .environment(\.locale, Locale(identifier: "zh-Hans "))
29+ /// .environment(\.calendar, Calendar(identifier: .gregorian) )
1730/// ```
1831public struct DateTimePicker {
1932 let title : any View
@@ -24,6 +37,8 @@ public struct DateTimePicker {
2437 let errorMessage : AttributedString ?
2538 let range : ClosedRange < Date > ?
2639 @Binding var selectedDate : Date
40+ /// The `DateFormatter` to be used to display the selected `Date`. Default formatter will use customized dateStyle and timeStyle.
41+ let dateFormatter : DateFormatter ?
2742 /// The components shown in the date picker, default value shows date and time.
2843 let pickerComponents : DatePicker . Components
2944 /// The custom style for displaying the date. The default value is `.abbreviated`, showing for example, "Oct 21, 2015".
@@ -47,6 +62,7 @@ public struct DateTimePicker {
4762 errorMessage: AttributedString ? = nil ,
4863 range: ClosedRange < Date > ? = nil ,
4964 selectedDate: Binding < Date > ,
65+ dateFormatter: DateFormatter ? = nil ,
5066 pickerComponents: DatePicker . Components = [ . date, . hourAndMinute] ,
5167 dateStyle: Date . FormatStyle . DateStyle = . abbreviated,
5268 timeStyle: Date . FormatStyle . TimeStyle = . shortened,
@@ -60,6 +76,7 @@ public struct DateTimePicker {
6076 self . errorMessage = errorMessage
6177 self . range = range
6278 self . _selectedDate = selectedDate
79+ self . dateFormatter = dateFormatter
6380 self . pickerComponents = pickerComponents
6481 self . dateStyle = dateStyle
6582 self . timeStyle = timeStyle
@@ -82,6 +99,7 @@ public extension DateTimePicker {
8299 errorMessage: AttributedString ? = nil ,
83100 range: ClosedRange < Date > ? = nil ,
84101 selectedDate: Binding < Date > ,
102+ dateFormatter: DateFormatter ? = nil ,
85103 pickerComponents: DatePicker . Components = [ . date, . hourAndMinute] ,
86104 dateStyle: Date . FormatStyle . DateStyle = . abbreviated,
87105 timeStyle: Date . FormatStyle . TimeStyle = . shortened,
@@ -90,7 +108,7 @@ public extension DateTimePicker {
90108 {
91109 self . init ( title: {
92110 TextWithMandatoryFieldIndicator ( text: title, isRequired: isRequired, mandatoryFieldIndicator: mandatoryFieldIndicator)
93- } , valueLabel: { OptionalText ( valueLabel) } , controlState: controlState, errorMessage: errorMessage, range: range, selectedDate: selectedDate, pickerComponents: pickerComponents, dateStyle: dateStyle, timeStyle: timeStyle, noDateSelectedString: noDateSelectedString, pickerVisible: pickerVisible)
111+ } , valueLabel: { OptionalText ( valueLabel) } , controlState: controlState, errorMessage: errorMessage, range: range, selectedDate: selectedDate, dateFormatter : dateFormatter , pickerComponents: pickerComponents, dateStyle: dateStyle, timeStyle: timeStyle, noDateSelectedString: noDateSelectedString, pickerVisible: pickerVisible)
94112 }
95113}
96114
@@ -106,6 +124,7 @@ public extension DateTimePicker {
106124 self . errorMessage = configuration. errorMessage
107125 self . range = configuration. range
108126 self . _selectedDate = configuration. $selectedDate
127+ self . dateFormatter = configuration. dateFormatter
109128 self . pickerComponents = configuration. pickerComponents
110129 self . dateStyle = configuration. dateStyle
111130 self . timeStyle = configuration. timeStyle
@@ -121,7 +140,7 @@ extension DateTimePicker: View {
121140 if self . _shouldApplyDefaultStyle {
122141 self . defaultStyle ( )
123142 } else {
124- self . style. resolve ( configuration: . init( componentIdentifier: self . componentIdentifier, title: . init( self . title) , valueLabel: . init( self . valueLabel) , controlState: self . controlState, errorMessage: self . errorMessage, range: self . range, selectedDate: self . $selectedDate, pickerComponents: self . pickerComponents, dateStyle: self . dateStyle, timeStyle: self . timeStyle, noDateSelectedString: self . noDateSelectedString, pickerVisible: self . $pickerVisible) ) . typeErased
143+ self . style. resolve ( configuration: . init( componentIdentifier: self . componentIdentifier, title: . init( self . title) , valueLabel: . init( self . valueLabel) , controlState: self . controlState, errorMessage: self . errorMessage, range: self . range, selectedDate: self . $selectedDate, dateFormatter : self . dateFormatter , pickerComponents: self . pickerComponents, dateStyle: self . dateStyle, timeStyle: self . timeStyle, noDateSelectedString: self . noDateSelectedString, pickerVisible: self . $pickerVisible) ) . typeErased
125144 . transformEnvironment ( \. dateTimePickerStyleStack) { stack in
126145 if !stack. isEmpty {
127146 stack. removeLast ( )
@@ -139,7 +158,7 @@ private extension DateTimePicker {
139158 }
140159
141160 func defaultStyle( ) -> some View {
142- DateTimePicker ( . init( componentIdentifier: self . componentIdentifier, title: . init( self . title) , valueLabel: . init( self . valueLabel) , controlState: self . controlState, errorMessage: self . errorMessage, range: self . range, selectedDate: self . $selectedDate, pickerComponents: self . pickerComponents, dateStyle: self . dateStyle, timeStyle: self . timeStyle, noDateSelectedString: self . noDateSelectedString, pickerVisible: self . $pickerVisible) )
161+ DateTimePicker ( . init( componentIdentifier: self . componentIdentifier, title: . init( self . title) , valueLabel: . init( self . valueLabel) , controlState: self . controlState, errorMessage: self . errorMessage, range: self . range, selectedDate: self . $selectedDate, dateFormatter : self . dateFormatter , pickerComponents: self . pickerComponents, dateStyle: self . dateStyle, timeStyle: self . timeStyle, noDateSelectedString: self . noDateSelectedString, pickerVisible: self . $pickerVisible) )
143162 . shouldApplyDefaultStyle ( false )
144163 . dateTimePickerStyle ( DateTimePickerFioriStyle . ContentFioriStyle ( ) )
145164 . typeErased
0 commit comments