2016-01-26 18:29:47 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
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
|
2016-01-26 18:29:47 +00:00
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2016-01-26 18:29:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
|
|
|
var ReactNative = require('react-native');
|
2016-01-26 18:29:47 +00:00
|
|
|
var {
|
|
|
|
TimePickerAndroid,
|
|
|
|
StyleSheet,
|
|
|
|
Text,
|
|
|
|
TouchableWithoutFeedback,
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2016-01-26 18:29:47 +00:00
|
|
|
|
2017-05-06 03:50:47 +00:00
|
|
|
var RNTesterBlock = require('./RNTesterBlock');
|
|
|
|
var RNTesterPage = require('./RNTesterPage');
|
2016-01-26 18:29:47 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class TimePickerAndroidExample extends React.Component {
|
|
|
|
static title = 'TimePickerAndroid';
|
|
|
|
static description = 'Standard Android time picker dialog';
|
2016-01-26 18:29:47 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
isoFormatText: 'pick a time (24-hour format)',
|
|
|
|
presetHour: 4,
|
|
|
|
presetMinute: 4,
|
|
|
|
presetText: 'pick a time, default: 4:04AM',
|
|
|
|
simpleText: 'pick a time',
|
2017-10-08 19:28:10 +00:00
|
|
|
clockText: 'pick a time',
|
|
|
|
spinnerText: 'pick a time',
|
|
|
|
defaultText: 'pick a time',
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-01-26 18:29:47 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
showPicker = async (stateKey, options) => {
|
2016-01-26 18:29:47 +00:00
|
|
|
try {
|
|
|
|
const {action, minute, hour} = await TimePickerAndroid.open(options);
|
|
|
|
var newState = {};
|
|
|
|
if (action === TimePickerAndroid.timeSetAction) {
|
|
|
|
newState[stateKey + 'Text'] = _formatTime(hour, minute);
|
|
|
|
newState[stateKey + 'Hour'] = hour;
|
|
|
|
newState[stateKey + 'Minute'] = minute;
|
|
|
|
} else if (action === TimePickerAndroid.dismissedAction) {
|
|
|
|
newState[stateKey + 'Text'] = 'dismissed';
|
|
|
|
}
|
|
|
|
this.setState(newState);
|
|
|
|
} catch ({code, message}) {
|
|
|
|
console.warn(`Error in example '${stateKey}': `, message);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2016-01-26 18:29:47 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterPage title="TimePickerAndroid">
|
|
|
|
<RNTesterBlock title="Simple time picker">
|
2016-01-26 18:29:47 +00:00
|
|
|
<TouchableWithoutFeedback
|
2016-09-16 07:30:46 +00:00
|
|
|
onPress={this.showPicker.bind(this, 'simple', {})}>
|
2016-01-26 18:29:47 +00:00
|
|
|
<Text style={styles.text}>{this.state.simpleText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
2017-10-08 19:28:10 +00:00
|
|
|
<RNTesterBlock title="Simple clock time picker">
|
|
|
|
<TouchableWithoutFeedback
|
|
|
|
onPress={this.showPicker.bind(this, 'clock', {mode: 'clock'})}>
|
|
|
|
<Text style={styles.text}>{this.state.clockText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Simple spinner time picker">
|
|
|
|
<TouchableWithoutFeedback
|
|
|
|
onPress={this.showPicker.bind(this, 'spinner', {mode: 'spinner'})}>
|
|
|
|
<Text style={styles.text}>{this.state.spinnerText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Simple default time picker">
|
|
|
|
<TouchableWithoutFeedback
|
|
|
|
onPress={this.showPicker.bind(this, 'default', {mode: 'default'})}>
|
|
|
|
<Text style={styles.text}>{this.state.defaultText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
</RNTesterBlock>
|
2017-05-06 03:50:47 +00:00
|
|
|
<RNTesterBlock title="Time picker with pre-set time">
|
2016-01-26 18:29:47 +00:00
|
|
|
<TouchableWithoutFeedback
|
|
|
|
onPress={this.showPicker.bind(this, 'preset', {
|
|
|
|
hour: this.state.presetHour,
|
|
|
|
minute: this.state.presetMinute,
|
|
|
|
})}>
|
|
|
|
<Text style={styles.text}>{this.state.presetText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Time picker with 24-hour time format">
|
2016-01-26 18:29:47 +00:00
|
|
|
<TouchableWithoutFeedback
|
|
|
|
onPress={this.showPicker.bind(this, 'isoFormat', {
|
|
|
|
hour: this.state.isoFormatHour,
|
|
|
|
minute: this.state.isoFormatMinute,
|
|
|
|
is24Hour: true,
|
|
|
|
})}>
|
|
|
|
<Text style={styles.text}>{this.state.isoFormatText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
</RNTesterPage>
|
2016-01-26 18:29:47 +00:00
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-26 18:29:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns e.g. '3:05'.
|
|
|
|
*/
|
|
|
|
function _formatTime(hour, minute) {
|
|
|
|
return hour + ':' + (minute < 10 ? '0' + minute : minute);
|
|
|
|
}
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
text: {
|
|
|
|
color: 'black',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = TimePickerAndroidExample;
|