Skip to content

Commit 446ce49

Browse files
Reem Heloufacebook-github-bot
Reem Helou
authored andcommittedFeb 23, 2018
Fix Bug with Date Picker IOS
Reviewed By: sahrens Differential Revision: D7052609 fbshipit-source-id: 740fffa9ad55ccd21347626f9c5dc180fcc4094d
1 parent 87f98bc commit 446ce49

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed
 

‎Libraries/Components/DatePicker/DatePickerIOS.ios.js

+28-13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
const NativeMethodsMixin = require('NativeMethodsMixin');
1515
const React = require('React');
16+
const invariant = require('fbjs/lib/invariant');
1617
const PropTypes = require('prop-types');
1718
const StyleSheet = require('StyleSheet');
1819
const View = require('View');
@@ -46,7 +47,17 @@ const DatePickerIOS = createReactClass({
4647
/**
4748
* The currently selected date.
4849
*/
49-
date: PropTypes.instanceOf(Date).isRequired,
50+
date: PropTypes.instanceOf(Date),
51+
52+
/**
53+
* Provides an initial value that will change when the user starts selecting
54+
* a date. It is useful for simple use-cases where you do not want to deal
55+
* with listening to events and updating the date prop to keep the
56+
* controlled state in sync. The controlled state has known bugs which
57+
* causes it to go out of sync with native. The initialDate prop is intended
58+
* to allow you to have native be source of truth.
59+
*/
60+
initialDate: PropTypes.instanceOf(Date),
5061

5162
/**
5263
* Date change handler.
@@ -102,34 +113,38 @@ const DatePickerIOS = createReactClass({
102113
};
103114
},
104115

116+
componentDidUpdate: function() {
117+
if (this.props.date) {
118+
const propsTimeStamp = this.props.date.getTime();
119+
if (this._picker) {
120+
this._picker.setNativeProps({
121+
date: propsTimeStamp,
122+
});
123+
}
124+
}
125+
},
126+
105127
_onChange: function(event: Event) {
106128
const nativeTimeStamp = event.nativeEvent.timestamp;
107129
this.props.onDateChange && this.props.onDateChange(
108130
new Date(nativeTimeStamp)
109131
);
110132
// $FlowFixMe(>=0.41.0)
111133
this.props.onChange && this.props.onChange(event);
112-
113-
// We expect the onChange* handlers to be in charge of updating our `date`
114-
// prop. That way they can also disallow/undo/mutate the selection of
115-
// certain values. In other words, the embedder of this component should
116-
// be the source of truth, not the native component.
117-
const propsTimeStamp = this.props.date.getTime();
118-
if (this._picker && nativeTimeStamp !== propsTimeStamp) {
119-
this._picker.setNativeProps({
120-
date: propsTimeStamp,
121-
});
122-
}
123134
},
124135

125136
render: function() {
126137
const props = this.props;
138+
invariant(
139+
props.date || props.initialDate,
140+
'A selected date or initial date should be specified.',
141+
);
127142
return (
128143
<View style={props.style}>
129144
<RCTDatePickerIOS
130145
ref={ picker => { this._picker = picker; } }
131146
style={styles.datePickerIOS}
132-
date={props.date.getTime()}
147+
date={props.date ? props.date.getTime() : props.initialDate ? props.initialDate.getTime() : undefined}
133148
locale={props.locale ? props.locale : undefined}
134149
maximumDate={
135150
props.maximumDate ? props.maximumDate.getTime() : undefined

0 commit comments

Comments
 (0)
Please sign in to comment.