2015-03-09 23:18:15 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
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.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2015-03-23 17:06:16 +00:00
|
|
|
* @flow
|
2015-03-09 23:18:15 +00:00
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2015-03-09 23:18:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-10-30 21:26:13 +00:00
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
|
|
|
const {DatePickerIOS, StyleSheet, Text, TextInput, View} = ReactNative;
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2018-12-31 12:30:58 +00:00
|
|
|
type State = {|
|
|
|
|
date: Date,
|
|
|
|
timeZoneOffsetInHours: number,
|
|
|
|
|};
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2018-12-31 12:30:58 +00:00
|
|
|
type Props = {|
|
|
|
|
children: (State, (Date) => void) => React.Node,
|
|
|
|
|};
|
|
|
|
|
|
|
|
class WithDatePickerData extends React.Component<Props, State> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
2018-12-31 12:30:58 +00:00
|
|
|
date: new Date(),
|
|
|
|
timeZoneOffsetInHours: (-1 * new Date().getTimezoneOffset()) / 60,
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
onDateChange = date => {
|
2015-03-09 23:18:15 +00:00
|
|
|
this.setState({date: date});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
onTimezoneChange = event => {
|
2018-10-30 21:26:13 +00:00
|
|
|
const offset = parseInt(event.nativeEvent.text, 10);
|
2015-03-09 23:18:15 +00:00
|
|
|
if (isNaN(offset)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({timeZoneOffsetInHours: offset});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
render() {
|
2015-03-09 23:18:15 +00:00
|
|
|
// Ideally, the timezone input would be a picker rather than a
|
|
|
|
// text input, but we don't have any pickers yet :(
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<WithLabel label="Value:">
|
2018-12-07 22:52:06 +00:00
|
|
|
<Text testID="date-and-time-indicator">
|
2018-05-11 20:32:37 +00:00
|
|
|
{this.state.date.toLocaleDateString() +
|
|
|
|
' ' +
|
2018-12-07 22:52:06 +00:00
|
|
|
this.state.date.toLocaleTimeString([], {
|
|
|
|
hour: '2-digit',
|
|
|
|
minute: '2-digit',
|
|
|
|
})}
|
2018-05-11 20:32:37 +00:00
|
|
|
</Text>
|
2015-03-09 23:18:15 +00:00
|
|
|
</WithLabel>
|
|
|
|
<WithLabel label="Timezone:">
|
|
|
|
<TextInput
|
|
|
|
onChange={this.onTimezoneChange}
|
|
|
|
style={styles.textinput}
|
|
|
|
value={this.state.timeZoneOffsetInHours.toString()}
|
|
|
|
/>
|
|
|
|
<Text> hours from UTC</Text>
|
|
|
|
</WithLabel>
|
2018-12-31 12:30:58 +00:00
|
|
|
{this.props.children(this.state, this.onDateChange)}
|
2015-03-09 23:18:15 +00:00
|
|
|
</View>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2018-12-31 12:30:58 +00:00
|
|
|
type LabelProps = {|
|
|
|
|
label: string,
|
|
|
|
children: React.Node,
|
|
|
|
|};
|
|
|
|
|
|
|
|
class WithLabel extends React.Component<LabelProps> {
|
2016-07-26 08:00:02 +00:00
|
|
|
render() {
|
2015-03-09 23:18:15 +00:00
|
|
|
return (
|
|
|
|
<View style={styles.labelContainer}>
|
|
|
|
<View style={styles.labelView}>
|
2018-05-11 20:32:37 +00:00
|
|
|
<Text style={styles.label}>{this.props.label}</Text>
|
2015-03-09 23:18:15 +00:00
|
|
|
</View>
|
|
|
|
{this.props.children}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2018-10-30 21:26:13 +00:00
|
|
|
const styles = StyleSheet.create({
|
2015-03-09 23:18:15 +00:00
|
|
|
textinput: {
|
|
|
|
height: 26,
|
|
|
|
width: 50,
|
|
|
|
borderWidth: 0.5,
|
|
|
|
borderColor: '#0f0f0f',
|
|
|
|
padding: 4,
|
|
|
|
fontSize: 13,
|
|
|
|
},
|
|
|
|
labelContainer: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
marginVertical: 2,
|
|
|
|
},
|
|
|
|
labelView: {
|
|
|
|
marginRight: 10,
|
|
|
|
paddingVertical: 2,
|
|
|
|
},
|
|
|
|
label: {
|
2015-03-26 01:19:28 +00:00
|
|
|
fontWeight: '500',
|
2015-03-09 23:18:15 +00:00
|
|
|
},
|
2018-12-31 12:30:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
exports.title = '<DatePickerIOS>';
|
|
|
|
exports.description = 'Select dates and times using the native UIDatePicker.';
|
|
|
|
exports.examples = [
|
|
|
|
{
|
|
|
|
title: 'Date and time picker',
|
|
|
|
render: function(): React.Element<any> {
|
|
|
|
return (
|
|
|
|
<WithDatePickerData>
|
|
|
|
{(state, onDateChange) => (
|
|
|
|
<DatePickerIOS
|
|
|
|
testID="date-and-time"
|
|
|
|
date={state.date}
|
|
|
|
mode="datetime"
|
|
|
|
timeZoneOffsetInMinutes={state.timeZoneOffsetInHours * 60}
|
|
|
|
onDateChange={onDateChange}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</WithDatePickerData>
|
|
|
|
);
|
|
|
|
},
|
2015-03-09 23:18:15 +00:00
|
|
|
},
|
2018-12-31 12:30:58 +00:00
|
|
|
{
|
|
|
|
title: 'Date only picker',
|
|
|
|
render: function(): React.Element<any> {
|
|
|
|
return (
|
|
|
|
<WithDatePickerData>
|
|
|
|
{(state, onDateChange) => (
|
|
|
|
<DatePickerIOS
|
|
|
|
testID="date-only"
|
|
|
|
date={state.date}
|
|
|
|
mode="date"
|
|
|
|
timeZoneOffsetInMinutes={state.timeZoneOffsetInHours * 60}
|
|
|
|
onDateChange={onDateChange}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</WithDatePickerData>
|
|
|
|
);
|
|
|
|
},
|
2015-03-09 23:18:15 +00:00
|
|
|
},
|
2018-12-31 12:30:58 +00:00
|
|
|
{
|
|
|
|
title: 'Time only picker, 10-minute interval',
|
|
|
|
render: function(): React.Element<any> {
|
|
|
|
return (
|
|
|
|
<WithDatePickerData>
|
|
|
|
{(state, onDateChange) => (
|
|
|
|
<DatePickerIOS
|
|
|
|
testID="time-only"
|
|
|
|
date={state.date}
|
|
|
|
mode="time"
|
|
|
|
timeZoneOffsetInMinutes={state.timeZoneOffsetInHours * 60}
|
|
|
|
onDateChange={onDateChange}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</WithDatePickerData>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|