2016-01-26 18:29:47 +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
|
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 {
|
|
|
|
DatePickerAndroid,
|
|
|
|
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 DatePickerAndroidExample extends React.Component {
|
|
|
|
static title = 'DatePickerAndroid';
|
|
|
|
static description = 'Standard Android date picker dialog';
|
2016-01-26 18:29:47 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
presetDate: new Date(2020, 4, 5),
|
2017-01-02 19:19:04 +00:00
|
|
|
simpleDate: new Date(2020, 4, 5),
|
|
|
|
spinnerDate: new Date(2020, 4, 5),
|
|
|
|
calendarDate: new Date(2020, 4, 5),
|
|
|
|
defaultDate: new Date(2020, 4, 5),
|
2016-07-26 08:00:02 +00:00
|
|
|
allDate: new Date(2020, 4, 5),
|
|
|
|
simpleText: 'pick a date',
|
2016-11-23 12:48:34 +00:00
|
|
|
spinnerText: 'pick a date',
|
|
|
|
calendarText: 'pick a date',
|
|
|
|
defaultText: 'pick a date',
|
2016-07-26 08:00:02 +00:00
|
|
|
minText: 'pick a date, no earlier than today',
|
|
|
|
maxText: 'pick a date, no later than today',
|
|
|
|
presetText: 'pick a date, preset to 2020/5/5',
|
|
|
|
allText: 'pick a date between 2020/5/1 and 2020/5/10',
|
|
|
|
};
|
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 {
|
|
|
|
var newState = {};
|
2016-04-06 16:20:39 +00:00
|
|
|
const {action, year, month, day} = await DatePickerAndroid.open(options);
|
2016-01-26 18:29:47 +00:00
|
|
|
if (action === DatePickerAndroid.dismissedAction) {
|
|
|
|
newState[stateKey + 'Text'] = 'dismissed';
|
|
|
|
} else {
|
|
|
|
var date = new Date(year, month, day);
|
|
|
|
newState[stateKey + 'Text'] = date.toLocaleDateString();
|
|
|
|
newState[stateKey + 'Date'] = date;
|
|
|
|
}
|
|
|
|
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="DatePickerAndroid">
|
|
|
|
<RNTesterBlock title="Simple date picker">
|
2016-01-26 18:29:47 +00:00
|
|
|
<TouchableWithoutFeedback
|
2018-05-11 20:32:37 +00:00
|
|
|
onPress={this.showPicker.bind(this, 'simple', {
|
|
|
|
date: this.state.simpleDate,
|
|
|
|
})}>
|
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="Simple spinner date picker">
|
2016-11-23 12:48:34 +00:00
|
|
|
<TouchableWithoutFeedback
|
2018-05-11 20:32:37 +00:00
|
|
|
onPress={this.showPicker.bind(this, 'spinner', {
|
|
|
|
date: this.state.spinnerDate,
|
|
|
|
mode: 'spinner',
|
|
|
|
})}>
|
2016-11-23 12:48:34 +00:00
|
|
|
<Text style={styles.text}>{this.state.spinnerText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Simple calendar date picker">
|
2016-11-23 12:48:34 +00:00
|
|
|
<TouchableWithoutFeedback
|
2018-05-11 20:32:37 +00:00
|
|
|
onPress={this.showPicker.bind(this, 'calendar', {
|
|
|
|
date: this.state.calendarDate,
|
|
|
|
mode: 'calendar',
|
|
|
|
})}>
|
2016-11-23 12:48:34 +00:00
|
|
|
<Text style={styles.text}>{this.state.calendarText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Simple default date picker">
|
2016-11-23 12:48:34 +00:00
|
|
|
<TouchableWithoutFeedback
|
2018-05-11 20:32:37 +00:00
|
|
|
onPress={this.showPicker.bind(this, 'default', {
|
|
|
|
date: this.state.defaultDate,
|
|
|
|
mode: 'default',
|
|
|
|
})}>
|
2016-11-23 12:48:34 +00:00
|
|
|
<Text style={styles.text}>{this.state.defaultText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Date picker with pre-set date">
|
2016-01-26 18:29:47 +00:00
|
|
|
<TouchableWithoutFeedback
|
2018-05-11 20:32:37 +00:00
|
|
|
onPress={this.showPicker.bind(this, 'preset', {
|
|
|
|
date: this.state.presetDate,
|
|
|
|
})}>
|
2016-01-26 18:29:47 +00:00
|
|
|
<Text style={styles.text}>{this.state.presetText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Date picker with minDate">
|
2016-01-26 18:29:47 +00:00
|
|
|
<TouchableWithoutFeedback
|
|
|
|
onPress={this.showPicker.bind(this, 'min', {
|
|
|
|
date: this.state.minDate,
|
|
|
|
minDate: new Date(),
|
|
|
|
})}>
|
|
|
|
<Text style={styles.text}>{this.state.minText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Date picker with maxDate">
|
2016-01-26 18:29:47 +00:00
|
|
|
<TouchableWithoutFeedback
|
|
|
|
onPress={this.showPicker.bind(this, 'max', {
|
|
|
|
date: this.state.maxDate,
|
|
|
|
maxDate: new Date(),
|
|
|
|
})}>
|
|
|
|
<Text style={styles.text}>{this.state.maxText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
2017-05-06 03:50:47 +00:00
|
|
|
</RNTesterBlock>
|
|
|
|
<RNTesterBlock title="Date picker with all options">
|
2016-01-26 18:29:47 +00:00
|
|
|
<TouchableWithoutFeedback
|
|
|
|
onPress={this.showPicker.bind(this, 'all', {
|
|
|
|
date: this.state.allDate,
|
|
|
|
minDate: new Date(2020, 4, 1),
|
|
|
|
maxDate: new Date(2020, 4, 10),
|
|
|
|
})}>
|
|
|
|
<Text style={styles.text}>{this.state.allText}</Text>
|
|
|
|
</TouchableWithoutFeedback>
|
2018-05-11 20:32:37 +00:00
|
|
|
</RNTesterBlock>
|
2017-05-06 03:50:47 +00:00
|
|
|
</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
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
text: {
|
|
|
|
color: 'black',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = DatePickerAndroidExample;
|