DatePicker.js
3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'use strict';
import React from 'react';
import ReactNative from 'react-native';
const {
Component,
} = React;
const {
View,
Text,
StyleSheet,
Dimensions,
Platform,
DatePickerAndroid,
TouchableHighlight,
Image,
} = ReactNative;
export default class DatePicker extends Component {
static propTypes = {
containerStyle: View.propTypes.style,
};
constructor(props) {
super(props);
this._showDatePicker = this._showDatePicker.bind(this);
}
render() {
return (
<View style={[styles.container, this.props.containerStyle]}>
<TouchableHighlight underlayColor={'white'}
onPress={() => {this._showDatePicker(this.props.currentStartDate, '2000-1-1', this.props.currentEndDate, true)}}>
<View style={styles.dateContainer}>
<Text style={styles.dateText}>{this.props.currentStartDate}</Text>
<Image source={require('../images/down.png')}/>
</View>
</TouchableHighlight>
<Text style={styles.middleText}>至</Text>
<TouchableHighlight underlayColor={'white'}
onPress={() => {this._showDatePicker(this.props.currentEndDate, this.props.currentStartDate, this.props.maxDate, false)}}>
<View style={styles.dateContainer}>
<Text style={styles.dateText}>{this.props.currentEndDate}</Text>
<Image source={require('../images/down.png')}/>
</View>
</TouchableHighlight>
</View>
);
}
async _showDatePicker(currentDate, minDate, maxDate, ifStartDate) {
let currentDateArr = currentDate.split('-');
let minDateArr = minDate.split('-');
let maxDateArr = maxDate.split('-');
if (Platform.OS === 'android') {
try {
const {action, year, month, day} = await DatePickerAndroid.open({
date: new Date(currentDateArr[0], (currentDateArr[1] - 1), currentDateArr[2]),
minDate: new Date(minDateArr[0], (minDateArr[1] - 1), minDateArr[2]),
maxDate: new Date(maxDateArr[0], (maxDateArr[1] - 1), maxDateArr[2]),
});
if (action !== DatePickerAndroid.dismissedAction) {
let pickedDate = year + '-' + (month + 1) + '-' + day;
this.props.newDatePicked(pickedDate, ifStartDate);
}
} catch ({code, message}) {
}
} else {
this.props.showDatePickerIOS(true, ifStartDate);
}
}
}
let {width, height} = Dimensions.get('window');
let styles = StyleSheet.create({
container: {
width,
height: 45,
backgroundColor: 'white',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
middleText: {
marginLeft: 15,
marginRight: 15,
color: '#444444',
fontSize: 14,
},
dateText: {
color: '#444444',
fontSize: 14,
paddingRight: 5,
},
dateContainer: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
}
});