@@ -24,8 +24,6 @@ class DatePicker extends Component {
24
24
constructor ( props ) {
25
25
super ( props ) ;
26
26
27
- this . format = this . props . format || FORMATS [ this . props . mode ] ;
28
-
29
27
this . state = {
30
28
date : this . getDate ( ) ,
31
29
modalVisible : false ,
@@ -52,15 +50,17 @@ class DatePicker extends Component {
52
50
}
53
51
54
52
setModalVisible ( visible ) {
53
+ const { height, duration} = this . props ;
54
+
55
55
this . setState ( { modalVisible : visible } ) ;
56
56
57
57
// slide animation
58
58
if ( visible ) {
59
59
Animated . timing (
60
60
this . state . animatedHeight ,
61
61
{
62
- toValue : this . props . height ,
63
- duration : this . props . duration
62
+ toValue : height ,
63
+ duration : duration
64
64
}
65
65
) . start ( ) ;
66
66
} else {
@@ -88,22 +88,24 @@ class DatePicker extends Component {
88
88
}
89
89
90
90
getDate ( date = this . props . date ) {
91
+ const { mode, minDate, maxDate, format = FORMATS [ mode ] } = this . props ;
92
+
91
93
// date默认值
92
94
if ( ! date ) {
93
95
let now = new Date ( ) ;
94
- if ( this . props . minDate ) {
95
- let minDate = this . getDate ( this . props . minDate ) ;
96
+ if ( minDate ) {
97
+ let _minDate = this . getDate ( minDate ) ;
96
98
97
- if ( now < minDate ) {
98
- return minDate ;
99
+ if ( now < _minDate ) {
100
+ return _minDate ;
99
101
}
100
102
}
101
103
102
- if ( this . props . maxDate ) {
103
- let maxDate = this . getDate ( this . props . maxDate ) ;
104
+ if ( maxDate ) {
105
+ let _maxDate = this . getDate ( maxDate ) ;
104
106
105
- if ( now > maxDate ) {
106
- return maxDate ;
107
+ if ( now > _maxDate ) {
108
+ return _maxDate ;
107
109
}
108
110
}
109
111
@@ -114,14 +116,16 @@ class DatePicker extends Component {
114
116
return date ;
115
117
}
116
118
117
- return Moment ( date , this . format ) . toDate ( ) ;
119
+ return Moment ( date , format ) . toDate ( ) ;
118
120
}
119
121
120
122
getDateStr ( date = this . props . date ) {
123
+ const { mode, format = FORMATS [ mode ] } = this . props ;
124
+
121
125
if ( date instanceof Date ) {
122
- return Moment ( date ) . format ( this . format ) ;
126
+ return Moment ( date ) . format ( format ) ;
123
127
} else {
124
- return Moment ( this . getDate ( date ) ) . format ( this . format ) ;
128
+ return Moment ( this . getDate ( date ) ) . format ( format ) ;
125
129
}
126
130
}
127
131
@@ -132,11 +136,12 @@ class DatePicker extends Component {
132
136
}
133
137
134
138
getTitleElement ( ) {
135
- const { date, placeholder} = this . props ;
139
+ const { date, placeholder, customStyles} = this . props ;
140
+
136
141
if ( ! date && placeholder ) {
137
- return ( < Text style = { [ Style . placeholderText , this . props . customStyles . placeholderText ] } > { placeholder } </ Text > ) ;
142
+ return ( < Text style = { [ Style . placeholderText , customStyles . placeholderText ] } > { placeholder } </ Text > ) ;
138
143
}
139
- return ( < Text style = { [ Style . dateText , this . props . customStyles . dateText ] } > { this . getDateStr ( ) } </ Text > ) ;
144
+ return ( < Text style = { [ Style . dateText , customStyles . dateText ] } > { this . getDateStr ( ) } </ Text > ) ;
140
145
}
141
146
142
147
onDatePicked ( { action, year, month, day} ) {
@@ -158,8 +163,9 @@ class DatePicker extends Component {
158
163
}
159
164
160
165
onDatetimePicked ( { action, year, month, day} ) {
166
+ const { mode, format = FORMATS [ mode ] , is24Hour = ! format . match ( / h | a / ) } = this . props ;
167
+
161
168
if ( action !== DatePickerAndroid . dismissedAction ) {
162
- const { is24Hour = ! this . format . match ( / h | a / ) } = this . props ;
163
169
let timeMoment = Moment ( this . state . date ) ;
164
170
165
171
TimePickerAndroid . open ( {
@@ -192,16 +198,17 @@ class DatePicker extends Component {
192
198
if ( Platform . OS === 'ios' ) {
193
199
this . setModalVisible ( true ) ;
194
200
} else {
195
- const { is24Hour = ! this . format . match ( / h | a / ) } = this . props ;
201
+
202
+ const { mode, format = FORMATS [ mode ] , minDate, maxDate, is24Hour = ! format . match ( / h | a / ) } = this . props ;
196
203
197
204
// 选日期
198
- if ( this . props . mode === 'date' ) {
205
+ if ( mode === 'date' ) {
199
206
DatePickerAndroid . open ( {
200
207
date : this . state . date ,
201
- minDate : this . props . minDate && this . getDate ( this . props . minDate ) ,
202
- maxDate : this . props . maxDate && this . getDate ( this . props . maxDate )
208
+ minDate : minDate && this . getDate ( minDate ) ,
209
+ maxDate : maxDate && this . getDate ( maxDate )
203
210
} ) . then ( this . onDatePicked ) ;
204
- } else if ( this . props . mode === 'time' ) {
211
+ } else if ( mode === 'time' ) {
205
212
// 选时间
206
213
207
214
let timeMoment = Moment ( this . state . date ) ;
@@ -211,42 +218,53 @@ class DatePicker extends Component {
211
218
minute : timeMoment . minutes ( ) ,
212
219
is24Hour : is24Hour
213
220
} ) . then ( this . onTimePicked ) ;
214
- } else if ( this . props . mode === 'datetime' ) {
221
+ } else if ( mode === 'datetime' ) {
215
222
// 选日期和时间
216
223
217
224
DatePickerAndroid . open ( {
218
225
date : this . state . date ,
219
- minDate : this . props . minDate && this . getDate ( this . props . minDate ) ,
220
- maxDate : this . props . maxDate && this . getDate ( this . props . maxDate )
226
+ minDate : minDate && this . getDate ( minDate ) ,
227
+ maxDate : maxDate && this . getDate ( maxDate )
221
228
} ) . then ( this . onDatetimePicked ) ;
222
- } else {
223
- throw new Error ( 'The specified mode is not supported' ) ;
224
229
}
225
230
}
226
231
}
227
232
228
233
render ( ) {
229
- let customStyles = this . props . customStyles ;
230
- this . format = this . props . format || FORMATS [ this . props . mode ] ;
234
+ const {
235
+ mode,
236
+ style,
237
+ customStyles,
238
+ disabled,
239
+ showIcon,
240
+ iconSource,
241
+ minDate,
242
+ maxDate,
243
+ minuteInterval,
244
+ timeZoneOffsetInMinutes,
245
+ cancelBtnText,
246
+ confirmBtnText
247
+ } = this . props ;
248
+
231
249
const dateInputStyle = [
232
250
Style . dateInput , customStyles . dateInput ,
233
- this . props . disabled && Style . disabled ,
234
- this . props . disabled && customStyles . disabled
251
+ disabled && Style . disabled ,
252
+ disabled && customStyles . disabled
235
253
] ;
236
254
237
255
return (
238
256
< TouchableHighlight
239
- style = { [ Style . dateTouch , this . props . style ] }
257
+ style = { [ Style . dateTouch , style ] }
240
258
underlayColor = { 'transparent' }
241
259
onPress = { this . onPressDate }
242
260
>
243
261
< View style = { [ Style . dateTouchBody , customStyles . dateTouchBody ] } >
244
262
< View style = { dateInputStyle } >
245
263
{ this . getTitleElement ( ) }
246
264
</ View >
247
- { this . props . showIcon && < Image
265
+ { showIcon && < Image
248
266
style = { [ Style . dateIcon , customStyles . dateIcon ] }
249
- source = { this . props . iconSource }
267
+ source = { iconSource }
250
268
/> }
251
269
{ Platform . OS === 'ios' && < Modal
252
270
transparent = { true }
@@ -271,12 +289,12 @@ class DatePicker extends Component {
271
289
>
272
290
< DatePickerIOS
273
291
date = { this . state . date }
274
- mode = { this . props . mode }
275
- minimumDate = { this . props . minDate && this . getDate ( this . props . minDate ) }
276
- maximumDate = { this . props . maxDate && this . getDate ( this . props . maxDate ) }
292
+ mode = { mode }
293
+ minimumDate = { minDate && this . getDate ( minDate ) }
294
+ maximumDate = { maxDate && this . getDate ( maxDate ) }
277
295
onDateChange = { ( date ) => this . setState ( { date : date } ) }
278
- minuteInterval = { this . props . minuteInterval }
279
- timeZoneOffsetInMinutes = { this . props . timeZoneOffsetInMinutes }
296
+ minuteInterval = { minuteInterval }
297
+ timeZoneOffsetInMinutes = { timeZoneOffsetInMinutes }
280
298
style = { [ Style . datePicker , customStyles . datePicker ] }
281
299
/>
282
300
< TouchableHighlight
@@ -287,15 +305,15 @@ class DatePicker extends Component {
287
305
< Text
288
306
style = { [ Style . btnTextText , Style . btnTextCancel , customStyles . btnTextCancel ] }
289
307
>
290
- { this . props . cancelBtnText }
308
+ { cancelBtnText }
291
309
</ Text >
292
310
</ TouchableHighlight >
293
311
< TouchableHighlight
294
312
underlayColor = { 'transparent' }
295
313
onPress = { this . onPressConfirm }
296
314
style = { [ Style . btnText , Style . btnConfirm , customStyles . btnConfirm ] }
297
315
>
298
- < Text style = { [ Style . btnTextText , customStyles . btnTextConfirm ] } > { this . props . confirmBtnText } </ Text >
316
+ < Text style = { [ Style . btnTextText , customStyles . btnTextConfirm ] } > { confirmBtnText } </ Text >
299
317
</ TouchableHighlight >
300
318
</ Animated . View >
301
319
</ TouchableHighlight >
@@ -331,6 +349,7 @@ DatePicker.defaultProps = {
331
349
DatePicker . propTypes = {
332
350
mode : React . PropTypes . oneOf ( [ 'date' , 'datetime' , 'time' ] ) ,
333
351
date : React . PropTypes . oneOfType ( [ React . PropTypes . string , React . PropTypes . instanceOf ( Date ) ] ) ,
352
+ format : React . PropTypes . string ,
334
353
minDate : React . PropTypes . oneOfType ( [ React . PropTypes . string , React . PropTypes . instanceOf ( Date ) ] ) ,
335
354
maxDate : React . PropTypes . oneOfType ( [ React . PropTypes . string , React . PropTypes . instanceOf ( Date ) ] ) ,
336
355
height : React . PropTypes . number ,
0 commit comments