forked from BeingBharat/BSBOceanProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.jsx
More file actions
120 lines (108 loc) · 2.96 KB
/
form.jsx
File metadata and controls
120 lines (108 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// FormComponent.jsx
import React, { useState } from 'react';
import { View, TextInput, Text, TouchableOpacity, StyleSheet } from 'react-native';
const FormComponent = ({ onSubmit }) => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [errors, setErrors] = useState({});
const validateForm = () => {
const newErrors = {};
// Email validation
if (!email) {
newErrors.email = 'Email is required';
} else if (!/\S+@\S+\.\S+/.test(email)) {
newErrors.email = 'Email is invalid';
}
// Password validation
if (!password) {
newErrors.password = 'Password is required';
} else if (password.length < 6) {
newErrors.password = 'Password must be at least 6 characters';
}
setErrors(newErrors);
return Object.keys(newErrors).length === 0;
};
const handleSubmit = () => {
if (validateForm()) {
onSubmit({ email, password });
}
};
return (
<View style={styles.container} accessibilityLabel="login-form">
<Text style={styles.title}>Login Form</Text>
<View style={styles.inputContainer}>
<Text>Email</Text>
<TextInput
style={styles.input}
value={email}
onChangeText={setEmail}
placeholder="Enter your email"
keyboardType="email-address"
autoCapitalize="none"
testID="email-input"
accessibilityLabel="email-input"
/>
{errors.email && <Text style={styles.errorText} testID="email-error">{errors.email}</Text>}
</View>
<View style={styles.inputContainer}>
<Text>Password</Text>
<TextInput
style={styles.input}
value={password}
onChangeText={setPassword}
placeholder="Enter your password"
secureTextEntry
testID="password-input"
accessibilityLabel="password-input"
/>
{errors.password && <Text style={styles.errorText} testID="password-error">{errors.password}</Text>}
</View>
<TouchableOpacity
style={styles.button}
onPress={handleSubmit}
testID="submit-button"
accessibilityRole="button"
accessibilityLabel="submit-button"
>
<Text style={styles.buttonText}>Submit</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
container: {
padding: 20,
backgroundColor: '#fff',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
textAlign: 'center',
},
inputContainer: {
marginBottom: 15,
},
input: {
borderWidth: 1,
borderColor: '#ddd',
padding: 10,
borderRadius: 5,
marginTop: 5,
},
button: {
backgroundColor: '#007BFF',
padding: 15,
borderRadius: 5,
alignItems: 'center',
},
buttonText: {
color: '#fff',
fontWeight: 'bold',
},
errorText: {
color: 'red',
marginTop: 5,
},
});
export default FormComponent;