@@ -91,12 +91,12 @@ class DateTimePicker extends BaseComponent {
91
91
constructor ( props ) {
92
92
super ( props ) ;
93
93
94
- const initialValue = props . value || new Date ( ) ;
94
+ const initialValue = props . value ;
95
95
this . chosenDate = initialValue ;
96
96
97
97
this . state = {
98
98
showExpandableOverlay : false ,
99
- chosenDate : initialValue
99
+ value : initialValue
100
100
} ;
101
101
}
102
102
@@ -115,20 +115,35 @@ class DateTimePicker extends BaseComponent {
115
115
} ;
116
116
117
117
toggleExpandableOverlay = callback => {
118
- this . setState ( { showExpandableOverlay : ! this . state . showExpandableOverlay } ,
119
- ( ) => {
120
- if ( _ . isFunction ( callback ) ) {
121
- callback ( ) ;
122
- }
123
- } ) ;
118
+ this . setState ( { showExpandableOverlay : ! this . state . showExpandableOverlay } , ( ) => {
119
+ if ( _ . isFunction ( callback ) ) {
120
+ callback ( ) ;
121
+ }
122
+ } ) ;
124
123
} ;
125
124
126
125
onDonePressed = ( ) =>
127
126
this . toggleExpandableOverlay ( ( ) => {
128
127
_ . invoke ( this . props , 'onChange' , this . chosenDate ) ;
129
- this . setState ( { chosenDate : this . chosenDate } ) ;
128
+ this . setState ( { value : this . chosenDate } ) ;
130
129
} ) ;
131
130
131
+ getStringValue = ( ) => {
132
+ const { value} = this . state ;
133
+ const { mode, dateFormat, timeFormat} = this . getThemeProps ( ) ;
134
+ if ( value ) {
135
+ const dateString =
136
+ mode === MODES . DATE
137
+ ? dateFormat
138
+ ? moment ( value ) . format ( dateFormat )
139
+ : value . toLocaleDateString ( )
140
+ : timeFormat
141
+ ? moment ( value ) . format ( timeFormat )
142
+ : value . toLocaleTimeString ( ) ;
143
+ return dateString ;
144
+ }
145
+ } ;
146
+
132
147
renderExpandableOverlay = ( ) => {
133
148
const { testID, dialogProps} = this . getThemeProps ( ) ;
134
149
const { showExpandableOverlay} = this . state ;
@@ -144,12 +159,7 @@ class DateTimePicker extends BaseComponent {
144
159
onDismiss = { this . toggleExpandableOverlay }
145
160
containerStyle = { this . styles . dialog }
146
161
testID = { `${ testID } .dialog` }
147
- supportedOrientations = { [
148
- 'portrait' ,
149
- 'landscape' ,
150
- 'landscape-left' ,
151
- 'landscape-right'
152
- ] } // iOS only
162
+ supportedOrientations = { [ 'portrait' , 'landscape' , 'landscape-left' , 'landscape-right' ] } // iOS only
153
163
{ ...dialogProps }
154
164
>
155
165
< View useSafeArea >
@@ -171,25 +181,20 @@ class DateTimePicker extends BaseComponent {
171
181
iconStyle = { { tintColor : Colors . dark10 } }
172
182
onPress = { this . toggleExpandableOverlay }
173
183
/>
174
- < Button
175
- useCustomTheme = { useCustomTheme }
176
- link
177
- iconSource = { Assets . icons . check }
178
- onPress = { this . onDonePressed }
179
- />
184
+ < Button useCustomTheme = { useCustomTheme } link iconSource = { Assets . icons . check } onPress = { this . onDonePressed } />
180
185
</ View >
181
186
) ;
182
187
}
183
188
184
189
renderDateTimePicker ( ) {
185
- const { chosenDate , showExpandableOverlay} = this . state ;
190
+ const { value , showExpandableOverlay} = this . state ;
186
191
const { mode, minimumDate, maximumDate, locale, is24Hour, minuteInterval, timeZoneOffsetInMinutes} = this . props ;
187
192
188
193
if ( showExpandableOverlay ) {
189
194
return (
190
195
< RNDateTimePicker
191
196
mode = { mode }
192
- value = { chosenDate }
197
+ value = { value || new Date ( ) }
193
198
onChange = { this . handleChange }
194
199
minimumDate = { minimumDate }
195
200
maximumDate = { maximumDate }
@@ -203,28 +208,16 @@ class DateTimePicker extends BaseComponent {
203
208
}
204
209
205
210
renderExpandable = ( ) => {
206
- return Constants . isAndroid
207
- ? this . renderDateTimePicker ( )
208
- : this . renderExpandableOverlay ( ) ;
211
+ return Constants . isAndroid ? this . renderDateTimePicker ( ) : this . renderExpandableOverlay ( ) ;
209
212
} ;
210
213
211
214
render ( ) {
212
- const { chosenDate} = this . state ;
213
- const { mode, dateFormat, timeFormat} = this . getThemeProps ( ) ;
214
215
const textInputProps = TextField . extractOwnProps ( this . getThemeProps ( ) ) ;
215
- const dateString =
216
- mode === MODES . DATE
217
- ? dateFormat
218
- ? moment ( chosenDate ) . format ( dateFormat )
219
- : chosenDate . toLocaleDateString ( )
220
- : timeFormat
221
- ? moment ( chosenDate ) . format ( timeFormat )
222
- : chosenDate . toLocaleTimeString ( ) ;
223
216
224
217
return (
225
218
< TextField
226
219
{ ...textInputProps }
227
- value = { dateString }
220
+ value = { this . getStringValue ( ) }
228
221
expandable
229
222
renderExpandable = { this . renderExpandable }
230
223
onToggleExpandableModal = { this . toggleExpandableOverlay }
0 commit comments