2015-03-07 01:13:18 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-07 01:13:18 +00:00
|
|
|
*
|
|
|
|
*
|
2015-03-17 10:08:46 +00:00
|
|
|
* This is a controlled component version of RCTDatePickerIOS
|
2018-05-11 02:06:46 +00:00
|
|
|
*
|
|
|
|
* @format
|
|
|
|
* @flow
|
2015-03-07 01:13:18 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-03-07 01:13:18 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-01 13:48:29 +00:00
|
|
|
const React = require('React');
|
2018-02-23 18:41:30 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2016-06-01 13:48:29 +00:00
|
|
|
const StyleSheet = require('StyleSheet');
|
|
|
|
const View = require('View');
|
2017-03-24 07:22:57 +00:00
|
|
|
|
2016-06-01 13:48:29 +00:00
|
|
|
const requireNativeComponent = require('requireNativeComponent');
|
2015-03-07 01:13:18 +00:00
|
|
|
|
2018-05-12 17:25:14 +00:00
|
|
|
import type {ViewProps} from 'ViewPropTypes';
|
2015-03-25 19:55:10 +00:00
|
|
|
|
2018-06-01 19:37:22 +00:00
|
|
|
const RCTDatePickerIOS = requireNativeComponent('RCTDatePicker');
|
|
|
|
|
2015-03-25 19:55:10 +00:00
|
|
|
type Event = Object;
|
|
|
|
|
2018-05-12 17:25:14 +00:00
|
|
|
type Props = $ReadOnly<{|
|
|
|
|
...ViewProps,
|
2018-06-01 17:11:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The currently selected date.
|
|
|
|
*/
|
2018-05-12 17:25:14 +00:00
|
|
|
date?: ?Date,
|
2018-06-01 17:11:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides an initial value that will change when the user starts selecting
|
|
|
|
* a date. It is useful for simple use-cases where you do not want to deal
|
|
|
|
* with listening to events and updating the date prop to keep the
|
|
|
|
* controlled state in sync. The controlled state has known bugs which
|
|
|
|
* causes it to go out of sync with native. The initialDate prop is intended
|
|
|
|
* to allow you to have native be source of truth.
|
|
|
|
*/
|
2018-05-12 17:25:14 +00:00
|
|
|
initialDate?: ?Date,
|
2018-06-01 17:11:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The date picker locale.
|
|
|
|
*/
|
2018-05-12 17:25:14 +00:00
|
|
|
locale?: ?string,
|
2018-06-01 17:11:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum date.
|
|
|
|
*
|
|
|
|
* Restricts the range of possible date/time values.
|
|
|
|
*/
|
2018-05-12 17:25:14 +00:00
|
|
|
maximumDate?: ?Date,
|
2018-06-01 17:11:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Minimum date.
|
|
|
|
*
|
|
|
|
* Restricts the range of possible date/time values.
|
|
|
|
*/
|
2018-05-12 17:25:14 +00:00
|
|
|
minimumDate?: ?Date,
|
2018-06-01 17:11:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The interval at which minutes can be selected.
|
|
|
|
*/
|
2018-05-12 17:25:14 +00:00
|
|
|
minuteInterval?: ?(1 | 2 | 3 | 4 | 5 | 6 | 10 | 12 | 15 | 20 | 30),
|
2018-06-01 17:11:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The date picker mode.
|
|
|
|
*/
|
2018-05-12 17:25:14 +00:00
|
|
|
mode?: ?('date' | 'time' | 'datetime'),
|
2018-06-01 17:11:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Date change handler.
|
|
|
|
*
|
|
|
|
* This is called when the user changes the date or time in the UI.
|
|
|
|
* The first and only argument is an Event. For getting the date the picker
|
|
|
|
* was changed to, use onDateChange instead.
|
|
|
|
*/
|
2018-06-01 17:11:28 +00:00
|
|
|
onChange?: ?(event: Event) => void,
|
2018-06-01 17:11:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-05-12 17:25:14 +00:00
|
|
|
onDateChange: (date: Date) => void,
|
2018-06-01 17:11:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Timezone offset in minutes.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-05-12 17:25:14 +00:00
|
|
|
timeZoneOffsetInMinutes?: ?number,
|
|
|
|
|}>;
|
|
|
|
|
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.
|
|
|
|
*/
|
2018-06-01 17:11:35 +00:00
|
|
|
class DatePickerIOS extends React.Component<Props> {
|
|
|
|
static DefaultProps = {
|
|
|
|
mode: 'datetime',
|
|
|
|
};
|
2015-03-07 01:13:18 +00:00
|
|
|
|
2018-06-01 17:11:35 +00:00
|
|
|
// $FlowFixMe How to type a native component to be able to call setNativeProps
|
|
|
|
_picker: ?React.ElementRef<typeof RCTDatePickerIOS> = null;
|
2015-03-07 01:13:18 +00:00
|
|
|
|
2018-06-01 17:11:35 +00:00
|
|
|
componentDidUpdate() {
|
2018-02-23 18:41:30 +00:00
|
|
|
if (this.props.date) {
|
|
|
|
const propsTimeStamp = this.props.date.getTime();
|
|
|
|
if (this._picker) {
|
|
|
|
this._picker.setNativeProps({
|
|
|
|
date: propsTimeStamp,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-06-01 17:11:35 +00:00
|
|
|
}
|
2018-02-23 18:41:30 +00:00
|
|
|
|
2018-06-01 17:11:35 +00:00
|
|
|
_onChange = (event: Event) => {
|
2016-06-01 13:48:29 +00:00
|
|
|
const nativeTimeStamp = event.nativeEvent.timestamp;
|
2018-05-11 02:06:46 +00:00
|
|
|
this.props.onDateChange &&
|
|
|
|
this.props.onDateChange(new Date(nativeTimeStamp));
|
2015-03-07 01:13:18 +00:00
|
|
|
this.props.onChange && this.props.onChange(event);
|
2018-06-01 17:11:35 +00:00
|
|
|
};
|
2015-03-07 01:13:18 +00:00
|
|
|
|
2018-06-01 17:11:35 +00:00
|
|
|
render() {
|
2016-06-01 13:48:29 +00:00
|
|
|
const props = this.props;
|
2018-02-23 18:41:30 +00:00
|
|
|
invariant(
|
|
|
|
props.date || props.initialDate,
|
|
|
|
'A selected date or initial date should be specified.',
|
|
|
|
);
|
2015-03-07 01:13:18 +00:00
|
|
|
return (
|
|
|
|
<View style={props.style}>
|
2015-03-17 10:08:46 +00:00
|
|
|
<RCTDatePickerIOS
|
2018-05-11 02:06:46 +00:00
|
|
|
ref={picker => {
|
|
|
|
this._picker = picker;
|
|
|
|
}}
|
2015-06-05 15:46:17 +00:00
|
|
|
style={styles.datePickerIOS}
|
2018-05-11 02:06:46 +00:00
|
|
|
date={
|
|
|
|
props.date
|
|
|
|
? props.date.getTime()
|
|
|
|
: props.initialDate
|
|
|
|
? props.initialDate.getTime()
|
|
|
|
: undefined
|
|
|
|
}
|
2017-11-04 21:32:07 +00:00
|
|
|
locale={props.locale ? props.locale : undefined}
|
2015-03-07 01:13:18 +00:00
|
|
|
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>
|
|
|
|
);
|
2018-06-01 17:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-07 01:13:18 +00:00
|
|
|
|
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
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = DatePickerIOS;
|