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
|
|
|
* 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.
|
|
|
|
*
|
2017-02-25 11:05:32 +00:00
|
|
|
* @providesModule TimePickerAndroidExample
|
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',
|
|
|
|
};
|
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>
|
|
|
|
<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>
|
2016-01-26 18:29:47 +00:00
|
|
|
|
2017-05-06 03:50:47 +00:00
|
|
|
<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;
|
|
|
|
|