forked from henninghall/react-native-date-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatePickerIOS.js
95 lines (86 loc) · 2.7 KB
/
DatePickerIOS.js
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
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* This is a controlled component version of RCTDatePickerIOS
*
*/
'use strict'
import React from 'react'
import { StyleSheet, View, requireNativeComponent } from 'react-native'
import { throwIfInvalidProps } from './propChecker'
const invariant = require('fbjs/lib/invariant')
const RCTDatePickerIOS = requireNativeComponent('RNDatePicker')
/**
* Use `DatePickerIOS` to render a date/time picker (selector) on iOS. This is
* a controlled component, so you must hook in to the `onDateChange` callback
* and update the `date` prop in order for the component to update, otherwise
* the user's change will be reverted immediately to reflect `props.date` as the
* source of truth.
*/
export default class DatePickerIOS extends React.Component {
static DefaultProps = {
mode: 'datetime',
}
// $FlowFixMe How to type a native component to be able to call setNativeProps
_picker = null
componentDidUpdate() {
if (this.props.date) {
const propsTimeStamp = this.props.date.getTime()
if (this._picker) {
this._picker.setNativeProps({
date: propsTimeStamp,
})
}
}
}
_onChange = event => {
const nativeTimeStamp = event.nativeEvent.timestamp
this.props.onDateChange &&
this.props.onDateChange(new Date(nativeTimeStamp))
this.props.onChange && this.props.onChange(event)
}
render() {
const props = this.props
if (__DEV__) throwIfInvalidProps(props)
return (
<RCTDatePickerIOS
key={this.props.textColor} // preventing "Today" string keep old text color when text color changes
ref={picker => {
this._picker = picker
}}
style={[styles.datePickerIOS, props.style]}
date={
props.date
? props.date.getTime()
: props.initialDate
? props.initialDate.getTime()
: undefined
}
locale={props.locale ? props.locale : undefined}
maximumDate={
props.maximumDate ? props.maximumDate.getTime() : undefined
}
minimumDate={
props.minimumDate ? props.minimumDate.getTime() : undefined
}
mode={props.mode}
minuteInterval={props.minuteInterval}
timeZoneOffsetInMinutes={props.timeZoneOffsetInMinutes}
onChange={this._onChange}
onStartShouldSetResponder={() => true}
onResponderTerminationRequest={() => false}
textColor={props.textColor}
/>
)
}
}
const styles = StyleSheet.create({
datePickerIOS: {
height: 216,
width: 310,
},
})