1
1
import _ from 'lodash' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import moment from 'moment' ;
4
- import React from 'react' ;
4
+ import React , { Component } from 'react' ;
5
5
import { StyleSheet } from 'react-native' ;
6
6
import RNDateTimePicker from '@react-native-community/datetimepicker' ;
7
7
import { Constants } from '../../helpers' ;
8
8
import { Colors } from '../../style' ;
9
9
import Assets from '../../assets' ;
10
- import { BaseComponent } from '../../commons' ;
10
+ import { asBaseComponent } from '../../commons' ;
11
11
import { TextField } from '../inputs' ;
12
12
import Dialog from '../dialog' ;
13
13
import View from '../view' ;
14
14
import Button from '../button' ;
15
15
16
+
16
17
const MODES = {
17
18
DATE : 'date' ,
18
19
TIME : 'time'
@@ -28,8 +29,9 @@ const MODES = {
28
29
*/
29
30
/*eslint-enable*/
30
31
31
- class DateTimePicker extends BaseComponent {
32
+ class DateTimePicker extends Component {
32
33
static displayName = 'DateTimePicker' ;
34
+
33
35
static propTypes = {
34
36
...TextField . propTypes ,
35
37
/**
@@ -98,17 +100,23 @@ class DateTimePicker extends BaseComponent {
98
100
constructor ( props ) {
99
101
super ( props ) ;
100
102
101
- const initialValue = props . value ;
102
- this . chosenDate = initialValue ;
103
+ this . chosenDate = props . value ;
103
104
104
105
this . state = {
105
106
showExpandableOverlay : false ,
106
- value : initialValue
107
+ prevValue : props . value ,
108
+ value : props . value
107
109
} ;
108
110
}
109
111
110
- generateStyles ( ) {
111
- this . styles = createStyles ( this . props ) ;
112
+ static getDerivedStateFromProps ( nextProps , prevState ) {
113
+ if ( nextProps . value !== prevState . prevValue ) {
114
+ return {
115
+ prevValue : prevState . value ,
116
+ value : nextProps . value
117
+ } ;
118
+ }
119
+ return null ;
112
120
}
113
121
114
122
handleChange = ( event = { } , date ) => {
@@ -141,14 +149,14 @@ class DateTimePicker extends BaseComponent {
141
149
// since handleChange() is not called on iOS when there is no actual change
142
150
this . chosenDate = new Date ( ) ;
143
151
}
144
-
152
+
145
153
_ . invoke ( this . props , 'onChange' , this . chosenDate ) ;
146
154
this . setState ( { value : this . chosenDate } ) ;
147
155
} ) ;
148
156
149
157
getStringValue = ( ) => {
150
158
const { value} = this . state ;
151
- const { mode, dateFormat, timeFormat, dateFormatter, timeFormatter} = this . getThemeProps ( ) ;
159
+ const { mode, dateFormat, timeFormat, dateFormatter, timeFormatter} = this . props ;
152
160
if ( value ) {
153
161
switch ( mode ) {
154
162
case MODES . DATE :
@@ -168,7 +176,7 @@ class DateTimePicker extends BaseComponent {
168
176
} ;
169
177
170
178
renderExpandableOverlay = ( ) => {
171
- const { testID, dialogProps} = this . getThemeProps ( ) ;
179
+ const { testID, dialogProps} = this . props ;
172
180
const { showExpandableOverlay} = this . state ;
173
181
174
182
return (
@@ -180,7 +188,7 @@ class DateTimePicker extends BaseComponent {
180
188
bottom
181
189
centerH
182
190
onDismiss = { this . toggleExpandableOverlay }
183
- containerStyle = { this . styles . dialog }
191
+ containerStyle = { styles . dialog }
184
192
testID = { `${ testID } .dialog` }
185
193
supportedOrientations = { [ 'portrait' , 'landscape' , 'landscape-left' , 'landscape-right' ] } // iOS only
186
194
{ ...dialogProps }
@@ -197,7 +205,7 @@ class DateTimePicker extends BaseComponent {
197
205
const { useCustomTheme} = this . props ;
198
206
199
207
return (
200
- < View row spread bg-white paddingH-20 style = { this . styles . header } >
208
+ < View row spread bg-white paddingH-20 style = { styles . header } >
201
209
< Button
202
210
link
203
211
iconSource = { Assets . icons . x }
@@ -235,7 +243,7 @@ class DateTimePicker extends BaseComponent {
235
243
} ;
236
244
237
245
render ( ) {
238
- const textInputProps = TextField . extractOwnProps ( this . getThemeProps ( ) ) ;
246
+ const textInputProps = TextField . extractOwnProps ( this . props ) ;
239
247
240
248
return (
241
249
< TextField
@@ -249,23 +257,19 @@ class DateTimePicker extends BaseComponent {
249
257
}
250
258
}
251
259
252
- export default DateTimePicker ;
260
+ export { DateTimePicker } ; // For tests
261
+ export default asBaseComponent ( DateTimePicker ) ;
253
262
254
- function createStyles ( props ) {
255
- const borderRadius = 12 ;
256
263
257
- const styles = StyleSheet . create ( {
258
- header : {
259
- height : 56 ,
260
- borderBottomWidth : 1 ,
261
- borderBottomColor : Colors . dark80
262
- } ,
263
- dialog : {
264
- backgroundColor : Colors . white ,
265
- borderTopLeftRadius : borderRadius ,
266
- borderTopRightRadius : borderRadius
267
- }
268
- } ) ;
269
-
270
- return styles ;
271
- }
264
+ const styles = StyleSheet . create ( {
265
+ header : {
266
+ height : 56 ,
267
+ borderBottomWidth : 1 ,
268
+ borderBottomColor : Colors . dark80
269
+ } ,
270
+ dialog : {
271
+ backgroundColor : Colors . white ,
272
+ borderTopLeftRadius : 12 ,
273
+ borderTopRightRadius : 12
274
+ }
275
+ } ) ;
0 commit comments