2015-03-07 01:13:18 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-03-07 01:13:18 +00:00
|
|
|
*
|
|
|
|
* @providesModule DatePickerIOS
|
2015-03-25 19:55:10 +00:00
|
|
|
* @flow
|
2015-03-07 01:13:18 +00:00
|
|
|
*
|
2015-03-17 10:08:46 +00:00
|
|
|
* This is a controlled component version of RCTDatePickerIOS
|
2015-03-07 01:13:18 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-11-04 12:40:26 +00:00
|
|
|
const NativeMethodsMixin = require('NativeMethodsMixin');
|
2016-06-01 13:48:29 +00:00
|
|
|
const React = require('React');
|
2017-04-12 23:09:48 +00:00
|
|
|
const PropTypes = require('prop-types');
|
2016-06-01 13:48:29 +00:00
|
|
|
const StyleSheet = require('StyleSheet');
|
|
|
|
const View = require('View');
|
2017-03-24 07:22:57 +00:00
|
|
|
const ViewPropTypes = require('ViewPropTypes');
|
|
|
|
|
2017-07-07 21:24:25 +00:00
|
|
|
const createReactClass = require('create-react-class');
|
2016-06-01 13:48:29 +00:00
|
|
|
const requireNativeComponent = require('requireNativeComponent');
|
2015-03-07 01:13:18 +00:00
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
type DefaultProps = {
|
2016-08-09 13:32:41 +00:00
|
|
|
mode: 'date' | 'time' | 'datetime',
|
2015-03-25 19:55:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type Event = Object;
|
|
|
|
|
2015-03-07 01:13:18 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2017-07-07 21:24:25 +00:00
|
|
|
const DatePickerIOS = createReactClass({
|
|
|
|
displayName: 'DatePickerIOS',
|
2015-12-04 19:52:05 +00:00
|
|
|
// TOOD: Put a better type for _picker
|
|
|
|
_picker: (undefined: ?$FlowFixMe),
|
|
|
|
|
2015-03-07 01:13:18 +00:00
|
|
|
mixins: [NativeMethodsMixin],
|
|
|
|
|
|
|
|
propTypes: {
|
2017-03-24 07:22:57 +00:00
|
|
|
...ViewPropTypes,
|
2015-03-07 01:13:18 +00:00
|
|
|
/**
|
|
|
|
* The currently selected date.
|
|
|
|
*/
|
|
|
|
date: PropTypes.instanceOf(Date).isRequired,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Date change handler.
|
|
|
|
*
|
|
|
|
* This is called when the user changes the date or time in the UI.
|
|
|
|
* The first and only argument is a Date object representing the new
|
|
|
|
* date and time.
|
|
|
|
*/
|
|
|
|
onDateChange: PropTypes.func.isRequired,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum date.
|
|
|
|
*
|
|
|
|
* Restricts the range of possible date/time values.
|
|
|
|
*/
|
|
|
|
maximumDate: PropTypes.instanceOf(Date),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Minimum date.
|
|
|
|
*
|
|
|
|
* Restricts the range of possible date/time values.
|
|
|
|
*/
|
|
|
|
minimumDate: PropTypes.instanceOf(Date),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The date picker mode.
|
|
|
|
*/
|
2015-03-18 04:40:17 +00:00
|
|
|
mode: PropTypes.oneOf(['date', 'time', 'datetime']),
|
2015-03-07 01:13:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The interval at which minutes can be selected.
|
|
|
|
*/
|
|
|
|
minuteInterval: PropTypes.oneOf([1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30]),
|
|
|
|
|
|
|
|
/**
|
2015-03-18 04:40:17 +00:00
|
|
|
* Timezone offset in minutes.
|
2015-03-07 01:13:18 +00:00
|
|
|
*
|
|
|
|
* By default, the date picker will use the device's timezone. With this
|
|
|
|
* parameter, it is possible to force a certain timezone offset. For
|
|
|
|
* instance, to show times in Pacific Standard Time, pass -7 * 60.
|
|
|
|
*/
|
|
|
|
timeZoneOffsetInMinutes: PropTypes.number,
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
getDefaultProps: function(): DefaultProps {
|
2015-03-07 01:13:18 +00:00
|
|
|
return {
|
|
|
|
mode: 'datetime',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
_onChange: function(event: Event) {
|
2016-06-01 13:48:29 +00:00
|
|
|
const nativeTimeStamp = event.nativeEvent.timestamp;
|
2015-03-07 01:13:18 +00:00
|
|
|
this.props.onDateChange && this.props.onDateChange(
|
|
|
|
new Date(nativeTimeStamp)
|
|
|
|
);
|
2017-03-05 03:29:24 +00:00
|
|
|
// $FlowFixMe(>=0.41.0)
|
2015-03-07 01:13:18 +00:00
|
|
|
this.props.onChange && this.props.onChange(event);
|
|
|
|
|
|
|
|
// We expect the onChange* handlers to be in charge of updating our `date`
|
|
|
|
// prop. That way they can also disallow/undo/mutate the selection of
|
|
|
|
// certain values. In other words, the embedder of this component should
|
|
|
|
// be the source of truth, not the native component.
|
2016-06-01 13:48:29 +00:00
|
|
|
const propsTimeStamp = this.props.date.getTime();
|
2015-12-04 14:56:41 +00:00
|
|
|
if (this._picker && nativeTimeStamp !== propsTimeStamp) {
|
|
|
|
this._picker.setNativeProps({
|
2015-03-07 01:13:18 +00:00
|
|
|
date: propsTimeStamp,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2016-06-01 13:48:29 +00:00
|
|
|
const props = this.props;
|
2015-03-07 01:13:18 +00:00
|
|
|
return (
|
|
|
|
<View style={props.style}>
|
2015-03-17 10:08:46 +00:00
|
|
|
<RCTDatePickerIOS
|
2016-06-01 13:48:29 +00:00
|
|
|
ref={ picker => { this._picker = picker; } }
|
2015-06-05 15:46:17 +00:00
|
|
|
style={styles.datePickerIOS}
|
2015-03-07 01:13:18 +00:00
|
|
|
date={props.date.getTime()}
|
|
|
|
maximumDate={
|
|
|
|
props.maximumDate ? props.maximumDate.getTime() : undefined
|
|
|
|
}
|
|
|
|
minimumDate={
|
|
|
|
props.minimumDate ? props.minimumDate.getTime() : undefined
|
|
|
|
}
|
2015-06-05 15:46:17 +00:00
|
|
|
mode={props.mode}
|
2015-03-07 01:13:18 +00:00
|
|
|
minuteInterval={props.minuteInterval}
|
|
|
|
timeZoneOffsetInMinutes={props.timeZoneOffsetInMinutes}
|
|
|
|
onChange={this._onChange}
|
2016-11-08 02:01:09 +00:00
|
|
|
onStartShouldSetResponder={() => true}
|
|
|
|
onResponderTerminationRequest={() => false}
|
2015-03-07 01:13:18 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-01 13:48:29 +00:00
|
|
|
const styles = StyleSheet.create({
|
2015-06-05 15:46:17 +00:00
|
|
|
datePickerIOS: {
|
2016-04-26 11:20:56 +00:00
|
|
|
height: 216,
|
2015-03-07 01:13:18 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2016-06-01 15:44:03 +00:00
|
|
|
const RCTDatePickerIOS = requireNativeComponent('RCTDatePicker', {
|
|
|
|
propTypes: {
|
|
|
|
...DatePickerIOS.propTypes,
|
|
|
|
date: PropTypes.number,
|
|
|
|
minimumDate: PropTypes.number,
|
|
|
|
maximumDate: PropTypes.number,
|
|
|
|
onDateChange: () => null,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
}
|
2015-09-10 17:05:08 +00:00
|
|
|
});
|
2015-03-07 01:13:18 +00:00
|
|
|
|
|
|
|
module.exports = DatePickerIOS;
|