While working with React Native, I always tried to use built-in components instead of "reinventing" the bicycle.
So today the design mockups for my project required me to write the form for a user to pick up date and time. And I went to the React Native docs page and scrolled down the bar looking what they have to offer, which component I may use. That's how I came across DatePickerAndroid and TimePickerAndroid in API's section.
Even thought docs are quite straight forward, it took we a while to figure out how to properly use it.
Let's start with DatePickerAndroid: first thing we need to do is to write "onPress" type of the function. Important step is to declare is as "async" function, otherwise you will get an annoying "await is a reserved word" error message.
Another important step that I forgot about was to import DatePickerAndroid from 'react-native' package.
In this case I have assigned data to directly to the state, if you would like to do the same DO NOT forget to add ".bind(this)", when you are calling your function: "random.datePicker.bind(this)".
You can use this function on any element that supports "onPress" property.
Another big challenge is styling DatePickerAndroid. Currently it cannot be done directly through React Native, but we can mess up a little bit with Android code:
"<project>\android\app\src\main\res\values\styles.xml" file in your React Native project.
Let's change the color scheme of the component!
If you want to use the dark theme simply change Theme.AppCompat.Light.NoActionBar to Theme.AppCompat and Theme.AppCompat.Light.Dialog to Theme.AppCompat.
Final result:
TimePickerAndroid is similar to DatePickerAndroid, so once you figured out how to use one of them, you know how to use both:
As you can see even code and strategy how we are going to use it are the same, timePicker may be used as "onPress" function as well.
Even styling would be similar:
And if you want to have a darker theme you would have to perform all the same modifications as with DatePickerAndroid.
Final result:
So today the design mockups for my project required me to write the form for a user to pick up date and time. And I went to the React Native docs page and scrolled down the bar looking what they have to offer, which component I may use. That's how I came across DatePickerAndroid and TimePickerAndroid in API's section.
Even thought docs are quite straight forward, it took we a while to figure out how to properly use it.
Part 1. DatePickerAndroid
Let's start with DatePickerAndroid: first thing we need to do is to write "onPress" type of the function. Important step is to declare is as "async" function, otherwise you will get an annoying "await is a reserved word" error message.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import { DatePickerAndroid } from 'react-native'; | |
const random = { | |
async datePicker(){ | |
try { | |
var today = new Date(); | |
var afterTomorrow = today.setDate(today.getDate()+2); | |
const {action, year, month, day} = await DatePickerAndroid.open({ | |
date: new Date(), | |
minDate: new Date(), | |
maxDate: afterTomorrow, | |
mode: 'calendar' | |
}); | |
if (action !== DatePickerAndroid.dismissedAction) { | |
// Selected year, month (0-11), day | |
this.setState({ | |
this.state.time: day+"/"+month+"/"+year | |
}); | |
} | |
} catch ({code, message}) { | |
alert('Cannot open date picker: '+message); | |
} | |
} | |
} | |
export default random; |
In this case I have assigned data to directly to the state, if you would like to do the same DO NOT forget to add ".bind(this)", when you are calling your function: "random.datePicker.bind(this)".
You can use this function on any element that supports "onPress" property.
Another big challenge is styling DatePickerAndroid. Currently it cannot be done directly through React Native, but we can mess up a little bit with Android code:
"<project>\android\app\src\main\res\values\styles.xml" file in your React Native project.
Let's change the color scheme of the component!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | |
<item name="android:datePickerDialogTheme">@style/Date.Theme</item> | |
</style> | |
<style name="Date.Theme" parent="Theme.AppCompat.Light.Dialog"> | |
<item name="colorAccent">#379683</item> | |
<item name="android:textColorPrimary">#379683</item> | |
</style> | |
</resources> |
Final result:
Part 2. TimePickerAndroid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import { TimePickerAndroid, DatePickerAndroid } from 'react-native'; | |
const random = { | |
async timePicker(){ | |
const TimePickerModule = require('NativeModules').TimePickerAndroid; | |
try { | |
const {action, hour, minute} = await TimePickerAndroid.open({ | |
hour: 14, | |
minute: 0, | |
is24Hour: false, // Will display '2 PM' | |
}); | |
if (action !== TimePickerAndroid.dismissedAction) { | |
// Selected hour (0-23), minute (0-59) | |
//Applying extra 0 before the hour/minute for better visibility | |
// 9 minutes => 09 minutes | |
var m=(minute<10)?"0"+minute:minute; | |
var h=(hour<10)?"0"+hour:hour; | |
this.setState({ time:h+":"+m}) | |
} | |
} catch ({code, message}) { | |
alert('Cannot open time picker'+message); | |
} | |
} | |
} | |
export default random; |
Even styling would be similar:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | |
<item name="android:timePickerDialogTheme">@style/Time.Theme</item> | |
</style> | |
<style name="Time.Theme" parent="Theme.AppCompat.Light.Dialog"> | |
<item name="colorAccent">#379683</item> | |
<item name="android:textColorPrimary">#379683</item> | |
</style> | |
</resources> |
Final result:
Congratulations! Now you know how to make your form more user-friendly and do less validation!
Great read! If you're looking to elevate your driving experience in Dubai, consider opting for a Ford Mustang rental Dubai. The Ford Mustang offers an exciting blend of power, style, and performance, making it the perfect choice for exploring the city's vibrant streets and scenic routes. Whether you're cruising along the coastline or heading out for a night on the town, driving a Mustang ensures a memorable and thrilling experience. Highly recommend checking out rental services in Dubai to enjoy an unforgettable ride in a Ford Mustang!
ReplyDelete