Birthday Date Input
Screen is created using these React Native Core Components: <SafeAreaView />, <View />, <TouchableOpacity />, <Text />, <TextInput />
import React, { useState } from 'react';
import {
StyleSheet,
Alert,
SafeAreaView,
View,
TouchableOpacity,
Text,
TextInput,
} from 'react-native';
import FeatherIcon from 'react-native-vector-icons/Feather''@expo/vector-icons/Feather';
export default function Example() {
const [form, setForm] = useState({});
const [input, setInput] = useState('');
const handleInputChange = value => {
setInput(value);
if (value.length === 8) {
try {
const year = parseInt(value.slice(4, 8), 10);
if (Number.isNaN(year) || year < 1900 || year > 2025) {
throw new Error('Invalid year.');
}
const month = parseInt(value.slice(0, 2), 10);
if (Number.isNaN(month) || month < 1 || month > 12) {
throw new Error('Invalid month.');
}
const day = parseInt(value.slice(2, 4), 10);
const maxDaysInMonth = new Date(year, month, 0).getDate();
if (Number.isNaN(day) || day < 1 || day > maxDaysInMonth) {
throw new Error('Invalid day.');
}
setForm({ dateOfBirth: `${year}-${month}-${day}` });
} catch (_) {
Alert.alert('Invalid date entered, try again.');
setInput('');
}
}
};
return (
<SafeAreaView style={{ flex: 1, backgroundColor: '#e8ecf4' }}>
<View style={styles.container}>
<View style={styles.header}>
<TouchableOpacity
onPress={() => {
// handle onPress
}}
style={styles.headerBack}>
<FeatherIcon
color="#1D2A32"
name="chevron-left"
size={30} />
</TouchableOpacity>
</View>
<Text style={styles.title}>Enter your Birthday</Text>
<Text style={styles.subtitle}>
Provide your birth date to complete your profile.
</Text>
<View style={styles.form}>
<View style={styles.input}>
<TextInput
autoCapitalize="none"
autoCorrect={false}
autoFocus={true}
caretHidden={true}
keyboardType="number-pad"
maxLength={8}
onChangeText={handleInputChange}
returnKeyType="done"
style={styles.inputControl}
value={input} />
<View style={styles.inputOverflow}>
{'MM/DD/YYYY'.split('').map((placeholder, index, arr) => {
const countDelimiters = arr
.slice(0, index)
.filter(char => char === '/').length;
const indexWithoutDelimeter = index - countDelimiters;
const current = input[indexWithoutDelimeter];
return (
<Text key={index} style={styles.inputChar}>
{placeholder === '/' || !current ? (
<Text style={styles.inputCharEmpty}>{placeholder}</Text>
) : (
current
)}
</Text>
);
})}
</View>
</View>
<Text style={styles.subtitle}>
Your profile will show your age, not your date of birth.
</Text>
</View>
<TouchableOpacity
onPress={() => {
// handle onPress
}}
style={styles.btn}>
<Text style={styles.btnText}>Next</Text>
</TouchableOpacity>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flexGrow: 1,
flexShrink: 1,
flexBasis: 0,
paddingHorizontal: 24,
paddingBottom: 16,
},
title: {
fontSize: 31,
fontWeight: '700',
color: '#1D2A32',
marginBottom: 6,
},
subtitle: {
fontSize: 15,
fontWeight: '500',
color: '#929292',
},
form: {
flexGrow: 1,
flexShrink: 1,
flexBasis: 0,
marginTop: 24,
},
/** Header */
header: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
marginBottom: 12,
},
headerBack: {
padding: 8,
paddingTop: 0,
position: 'relative',
marginLeft: -16,
},
/** Input */
input: {
marginBottom: 16,
position: 'relative',
},
inputControl: {
height: 50,
backgroundColor: 'transparent',
paddingHorizontal: 16,
borderRadius: 12,
fontSize: 15,
fontWeight: '500',
color: 'transparent',
borderWidth: 1,
borderColor: '#C9D3DB',
borderStyle: 'solid',
zIndex: 2,
},
inputOverflow: {
backgroundColor: '#fff',
borderRadius: 12,
zIndex: 1,
position: 'absolute',
top: 0,
right: 0,
left: 0,
bottom: 0,
width: '100%',
flexDirection: 'row',
justifyContent: 'space-around',
paddingHorizontal: 16,
},
inputChar: {
flexGrow: 1,
flexShrink: 1,
flexBasis: 0,
lineHeight: 50,
fontSize: 28,
textAlign: 'center',
fontWeight: '600',
},
inputCharEmpty: {
color: '#BBB9BC',
fontWeight: '400',
},
/** Button */
btn: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
borderRadius: 30,
paddingVertical: 10,
paddingHorizontal: 20,
borderWidth: 1,
backgroundColor: '#075eec',
borderColor: '#075eec',
},
btnText: {
fontSize: 18,
lineHeight: 26,
fontWeight: '600',
color: '#fff',
},
});Dependencies
Before getting started, make sure you have all the necessary dependencies for this component. Follow the steps below to install any missing dependencies.
-
Install the package.
npm install --save react-native-vector-icons
-
Link the native packages for iOS.
npx pod-install
In React Native 0.60+ the CLI autolink feature links the module while building the app.
-
Add Fonts to the
Info.plistfile.
Open your XCode project, right click on the Info.plist, and select Open As -> Source Code.
Next, copy the fonts below into your Info.plist.
<key>UIAppFonts</key>
<array>
<string>AntDesign.ttf</string>
<string>Entypo.ttf</string>
<string>EvilIcons.ttf</string>
<string>Feather.ttf</string>
<string>FontAwesome.ttf</string>
<string>FontAwesome5_Brands.ttf</string>
<string>FontAwesome5_Regular.ttf</string>
<string>FontAwesome5_Solid.ttf</string>
<string>FontAwesome6_Brands.ttf</string>
<string>FontAwesome6_Regular.ttf</string>
<string>FontAwesome6_Solid.ttf</string>
<string>Foundation.ttf</string>
<string>Ionicons.ttf</string>
<string>MaterialIcons.ttf</string>
<string>MaterialCommunityIcons.ttf</string>
<string>SimpleLineIcons.ttf</string>
<string>Octicons.ttf</string>
<string>Zocial.ttf</string>
<string>Fontisto.ttf</string>
</array>
Note: You can only select the fonts you would like to use in your React Native Application.
After you added these fonts the Info.plist file should look like this:
![]()
-
Add
fonts.gradlefor Android.
Open android/app/build.gradle (NOT android/build.gradle) and add the following code to the bottom:
apply from: file("../../node_modules/react-native-vector-icons/fonts.gradle")
If you want to customize fonts, use:
project.ext.vectoricons = [
iconFontNames: [ 'MaterialIcons.ttf', 'EvilIcons.ttf' ]
]
apply from: file("../../node_modules/react-native-vector-icons/fonts.gradle")
Original instructions can be found on Github: https://github.com/oblador/react-native-vector-icons