1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import React from 'react'
|
7 | import { DatePickerIOS, StyleSheet, Dimensions } from 'react-native'
|
8 |
|
9 | type Props = {
|
10 | onDateSelected: Date => void,
|
11 | initDate: Date
|
12 | }
|
13 |
|
14 | type State = {
|
15 | chosenDate: Date
|
16 | }
|
17 |
|
18 | export default class DatePicker extends React.Component<Props, State> {
|
19 | constructor(props: Props){
|
20 | super(props)
|
21 | this.state = { chosenDate: props.initDate || new Date() }
|
22 | }
|
23 |
|
24 | setDate = (newDate) => {
|
25 | this.setState({chosenDate: newDate})
|
26 | const { onDateSelected } = this.props
|
27 | if (onDateSelected) onDateSelected(newDate)
|
28 | }
|
29 |
|
30 | render(){
|
31 | return (
|
32 | <DatePickerIOS
|
33 | style={styles.picker}
|
34 | date={this.state.chosenDate}
|
35 | onDateChange={this.setDate}
|
36 | {...this.props}
|
37 | />
|
38 | )
|
39 | }
|
40 | }
|
41 |
|
42 | let styles = StyleSheet.create({
|
43 | picker: {
|
44 | width: Dimensions.get('screen').width
|
45 | },
|
46 | })
|