Skip to content

Instantly share code, notes, and snippets.

@PavanKu
Created May 19, 2019 18:55
Show Gist options
  • Save PavanKu/91a03fa9c05a752891919c88b6a72949 to your computer and use it in GitHub Desktop.
Save PavanKu/91a03fa9c05a752891919c88b6a72949 to your computer and use it in GitHub Desktop.
Login form in React native
import React from 'react';
import { View, StyleSheet, Text, TouchableOpacity, TextInput, Alert } from "react-native";
const initialState = { username: '', password: '' };
class LoginForm extends React.Component {
constructor(props) {
super(props);
this.state = initialState;
this.handleSubmitForm = this.handleSubmitForm.bind(this);
this.handleUsernameChange = this.handleUsernameChange.bind(this);
this.handlePasswordChange = this.handlePasswordChange.bind(this);
}
handleUsernameChange(text) {
this.setState({ username: text });
}
handlePasswordChange(text) {
this.setState({ password: text });
}
handleSubmitForm() {
const { username, password } = this.state;
Alert.alert(`User ${username} is log in with password ${password}`);
}
render() {
return (
<View style={styles.container}>
<View>
<Text style={styles.text}>Username</Text>
<TextInput autoCorrect={false} autoCapitalize={'none'} style={styles.input} onChangeText={this.handleUsernameChange}></TextInput>
</View>
<View>
<Text style={styles.text}>Password</Text>
<TextInput autoCorrect={false} autoCapitalize={'none'} style={styles.input} secureTextEntry onChangeText={this.handlePasswordChange}></TextInput>
</View>
<TouchableOpacity style={styles.button} onPress={this.handleSubmitForm}>
<Text style={[styles.text, styles.center]}>Login</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
alignSelf: 'stretch',
margin: 10,
padding: 10,
borderColor: '#ccc',
borderWidth: 1,
borderRadius: 5,
backgroundColor: '#fff'
},
text: {
fontSize: 14,
fontWeight: 'bold',
color: '#015169',
paddingVertical: 5
},
input: {
height: 40,
borderColor: "#e0e0e0",
borderWidth: 1,
borderRadius: 5,
marginBottom: 15,
paddingHorizontal: 10
},
button: {
padding: 10,
borderColor: '#bee6fe',
borderWidth: 1,
borderRadius: 5,
backgroundColor: '#eaf7fd'
},
center: {
alignSelf: 'center'
}
});
export default LoginForm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment