A cross-platform datetime picker for React Native that support both Android and iOS. Uses DatePickerIOS component for iOS and DatePickerAndroid API for Android.
npm install react-native-datetime-picker --save
- not need install, on ios use js write
...
include ':react-native-datetime-picker'
project(':react-native-datetime-picker').projectDir = new File(rootProject.projectDir, '../node_modules/@remobile/react-native-datetime-picker/android')
- In
android/app/build.gradle
...
dependencies {
...
compile project(':react-native-datetime-picker')
}
- register module (in MainActivity.java)
import com.remobile.datetimepicker.*; // <--- import
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new RCTDateTimePickerPackage()) // <------ add here
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "ExampleRN", null);
setContentView(mReactRootView);
}
......
}
- ios
![image](https://github.com/remobile/react-native-datetime-picker/blob/master/screencasts/ios/1.png) ![image](https://github.com/remobile/react-native-datetime-picker/blob/master/screencasts/ios/2.png) ![image](https://github.com/remobile/react-native-datetime-picker/blob/master/screencasts/ios/3.png)
* android
![image](https://github.com/remobile/react-native-datetime-picker/blob/master/screencasts/android/1.png) ![image](https://github.com/remobile/react-native-datetime-picker/blob/master/screencasts/android/2.png)
use as follows:
<DateTimePicker ref={(picker)=>{this.picker=picker}}/>
...
this.picker.showDatePicker(...)
this.picker.showTimePicker(...)
- on ios, make sure must on topest view
'use strict';
var React = require('react-native');
var {
StyleSheet,
TouchableOpacity,
View,
Text,
} = React;
var DateTimePicker = require('@remobile/react-native-datetime-picker');
var Button = require('@remobile/react-native-simple-button');
module.exports = React.createClass({
getInitialState() {
return {
date: new Date(),
}
},
showDatePicker() {
var date = this.state.date;
this.picker.showDatePicker(date, (d)=>{
this.setState({date:d});
});
},
showTimePicker() {
var date = this.state.date;
this.picker.showTimePicker(date, (d)=>{
this.setState({date:d});
});
},
render() {
return (
<View style={styles.container}>
<Text style={{textAlign: 'center'}}>
{this.state.date.toString()}
</Text>
<View style={{height:40}} />
<Button onPress={this.showDatePicker}>showDatePicker</Button>
<View style={{height:40}} />
<Button onPress={this.showTimePicker}>showTimePicker</Button>
<DateTimePicker ref={(picker)=>{this.picker=picker}}/>
</View>
);
},
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop:20,
},
});
- showDatePicker(date, callback(date))
- showTimePicker(date, callback(date))
- don't need set any props for