@@ -24,26 +24,7 @@ class DatePicker extends Component {
24
24
constructor ( props ) {
25
25
super ( props ) ;
26
26
27
- this . mode = this . props . mode || 'date' ;
28
-
29
- this . format = this . props . format || FORMATS [ this . mode ] ;
30
- // component height: 216(DatePickerIOS) + 1(borderTop) + 42(marginTop), IOS only
31
- this . height = 259 ;
32
- // slide animation duration time, default to 300ms, IOS only
33
- this . duration = this . props . duration || 300 ;
34
-
35
- this . confirmBtnText = this . props . confirmBtnText || '确定' ;
36
- this . cancelBtnText = this . props . cancelBtnText || '取消' ;
37
-
38
- this . iconSource = this . props . iconSource || require ( './date_icon.png' ) ;
39
- this . customStyles = this . props . customStyles || { } ;
40
-
41
- // whether or not show the icon
42
- if ( typeof this . props . showIcon === 'boolean' ) {
43
- this . showIcon = this . props . showIcon ;
44
- } else {
45
- this . showIcon = true ;
46
- }
27
+ this . format = this . props . format || FORMATS [ this . props . mode ] ;
47
28
48
29
this . state = {
49
30
date : this . getDate ( ) ,
@@ -79,8 +60,8 @@ class DatePicker extends Component {
79
60
Animated . timing (
80
61
this . state . animatedHeight ,
81
62
{
82
- toValue : this . height ,
83
- duration : this . duration
63
+ toValue : this . props . height ,
64
+ duration : this . props . duration
84
65
}
85
66
) . start ( ) ;
86
67
} else {
@@ -120,15 +101,15 @@ class DatePicker extends Component {
120
101
this . props . onDateChange ( this . getDateStr ( this . state . date ) , this . state . date ) ;
121
102
}
122
103
}
123
-
104
+
124
105
getTitleElement ( ) {
125
106
const { date, placeholder} = this . props ;
126
107
if ( ! date && placeholder ) {
127
- return ( < Text style = { [ Style . placeholderText , this . customStyles . placeholderText ] } > { placeholder } </ Text > ) ;
108
+ return ( < Text style = { [ Style . placeholderText , this . props . customStyles . placeholderText ] } > { placeholder } </ Text > ) ;
128
109
}
129
- return ( < Text style = { [ Style . dateText , this . customStyles . dateText ] } > { this . getDateStr ( ) } </ Text > ) ;
110
+ return ( < Text style = { [ Style . dateText , this . props . customStyles . dateText ] } > { this . getDateStr ( ) } </ Text > ) ;
130
111
}
131
-
112
+
132
113
onDatePicked ( { action, year, month, day} ) {
133
114
if ( action !== DatePickerAndroid . dismissedAction ) {
134
115
this . setState ( {
@@ -183,13 +164,13 @@ class DatePicker extends Component {
183
164
} else {
184
165
185
166
// 选日期
186
- if ( this . mode === 'date' ) {
167
+ if ( this . props . mode === 'date' ) {
187
168
DatePickerAndroid . open ( {
188
169
date : this . state . date ,
189
170
minDate : this . props . minDate && this . getDate ( this . props . minDate ) ,
190
171
maxDate : this . props . maxDate && this . getDate ( this . props . maxDate )
191
172
} ) . then ( this . onDatePicked ) ;
192
- } else if ( this . mode === 'time' ) {
173
+ } else if ( this . props . mode === 'time' ) {
193
174
// 选时间
194
175
195
176
let timeMoment = Moment ( this . state . date ) ;
@@ -199,7 +180,7 @@ class DatePicker extends Component {
199
180
minute : timeMoment . minutes ( ) ,
200
181
is24Hour : ! this . format . match ( / h | a / )
201
182
} ) . then ( this . onTimePicked ) ;
202
- } else if ( this . mode === 'datetime' ) {
183
+ } else if ( this . props . mode === 'datetime' ) {
203
184
// 选日期和时间
204
185
205
186
DatePickerAndroid . open ( {
@@ -214,20 +195,22 @@ class DatePicker extends Component {
214
195
}
215
196
216
197
render ( ) {
198
+ let customStyles = this . props . customStyles ;
199
+ this . format = this . props . format || FORMATS [ this . props . mode ] ;
217
200
218
201
return (
219
202
< TouchableHighlight
220
203
style = { [ Style . dateTouch , this . props . style ] }
221
204
underlayColor = { 'transparent' }
222
205
onPress = { this . onPressDate }
223
206
>
224
- < View style = { [ Style . dateTouchBody , this . customStyles . dateTouchBody ] } >
225
- < View style = { [ Style . dateInput , this . customStyles . dateInput , this . state . disabled && Style . disabled ] } >
207
+ < View style = { [ Style . dateTouchBody , customStyles . dateTouchBody ] } >
208
+ < View style = { [ Style . dateInput , customStyles . dateInput , this . state . disabled && Style . disabled ] } >
226
209
{ this . getTitleElement ( ) }
227
210
</ View >
228
- { this . showIcon && < Image
229
- style = { [ Style . dateIcon , this . customStyles . dateIcon ] }
230
- source = { this . iconSource }
211
+ { this . props . showIcon && < Image
212
+ style = { [ Style . dateIcon , customStyles . dateIcon ] }
213
+ source = { this . props . iconSource }
231
214
/> }
232
215
{ Platform . OS === 'ios' && < Modal
233
216
transparent = { true }
@@ -245,33 +228,33 @@ class DatePicker extends Component {
245
228
style = { { flex : 1 } }
246
229
>
247
230
< Animated . View
248
- style = { [ Style . datePickerCon , { height : this . state . animatedHeight } , this . customStyles . datePickerCon ] }
231
+ style = { [ Style . datePickerCon , { height : this . state . animatedHeight } , customStyles . datePickerCon ] }
249
232
>
250
233
< DatePickerIOS
251
234
date = { this . state . date }
252
- mode = { this . mode }
235
+ mode = { this . props . mode }
253
236
minimumDate = { this . props . minDate && this . getDate ( this . props . minDate ) }
254
237
maximumDate = { this . props . maxDate && this . getDate ( this . props . maxDate ) }
255
238
onDateChange = { ( date ) => this . setState ( { date : date } ) }
256
- style = { [ Style . datePicker , this . customStyles . datePicker ] }
239
+ style = { [ Style . datePicker , customStyles . datePicker ] }
257
240
/>
258
241
< TouchableHighlight
259
242
underlayColor = { 'transparent' }
260
243
onPress = { this . onPressCancel }
261
- style = { [ Style . btnText , Style . btnCancel , this . customStyles . btnCancel ] }
244
+ style = { [ Style . btnText , Style . btnCancel , customStyles . btnCancel ] }
262
245
>
263
246
< Text
264
- style = { [ Style . btnTextText , Style . btnTextCancel , this . customStyles . btnTextCancel ] }
247
+ style = { [ Style . btnTextText , Style . btnTextCancel , customStyles . btnTextCancel ] }
265
248
>
266
- { this . cancelBtnText }
249
+ { this . props . cancelBtnText }
267
250
</ Text >
268
251
</ TouchableHighlight >
269
252
< TouchableHighlight
270
253
underlayColor = { 'transparent' }
271
254
onPress = { this . onPressConfirm }
272
- style = { [ Style . btnText , Style . btnConfirm , this . customStyles . btnConfirm ] }
255
+ style = { [ Style . btnText , Style . btnConfirm , customStyles . btnConfirm ] }
273
256
>
274
- < Text style = { [ Style . btnTextText , this . customStyles . btnTextConfirm ] } > { this . confirmBtnText } </ Text >
257
+ < Text style = { [ Style . btnTextText , customStyles . btnTextConfirm ] } > { this . props . confirmBtnText } </ Text >
275
258
</ TouchableHighlight >
276
259
</ Animated . View >
277
260
</ TouchableHighlight >
@@ -283,4 +266,42 @@ class DatePicker extends Component {
283
266
}
284
267
}
285
268
269
+ DatePicker . defaultProps = {
270
+ mode : 'date' ,
271
+ date : '' ,
272
+ // component height: 216(DatePickerIOS) + 1(borderTop) + 42(marginTop), IOS only
273
+ height : 259 ,
274
+
275
+ // slide animation duration time, default to 300ms, IOS only
276
+ duration : 300 ,
277
+ confirmBtnText : '确定' ,
278
+ cancelBtnText : '取消' ,
279
+ iconSource : require ( './date_icon.png' ) ,
280
+ customStyles : { } ,
281
+
282
+ // whether or not show the icon
283
+ showIcon : true ,
284
+ disabled : false ,
285
+ placeholder : ''
286
+ } ;
287
+
288
+ DatePicker . propTypes = {
289
+ mode : React . PropTypes . oneOf ( [ 'date' , 'datetime' , 'time' ] ) ,
290
+ date : React . PropTypes . oneOfType ( [ React . PropTypes . string , function ( props , propName ) {
291
+ if ( ! ( props [ propName ] instanceof Date ) ) {
292
+ return new Error ( 'Should be string or Date' ) ;
293
+ }
294
+ } ] ) ,
295
+ height : React . PropTypes . number ,
296
+ duration : React . PropTypes . number ,
297
+ confirmBtnText : React . PropTypes . string ,
298
+ cancelBtnText : React . PropTypes . string ,
299
+ iconSource : React . PropTypes . oneOfType ( [ React . PropTypes . number , React . PropTypes . object ] ) ,
300
+ customStyles : React . PropTypes . object ,
301
+ showIcon : React . PropTypes . bool ,
302
+ disabled : React . PropTypes . bool ,
303
+ onDateChange : React . PropTypes . func ,
304
+ placeholder : React . PropTypes . string
305
+ } ;
306
+
286
307
export default DatePicker ;
0 commit comments