-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathtestsubmit.tsx
More file actions
125 lines (115 loc) · 3.49 KB
/
Copy pathtestsubmit.tsx
File metadata and controls
125 lines (115 loc) · 3.49 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
121
122
123
124
125
/**
* @component TestSubmit
* @description
* A styled React Form component using Formik and Yup for form state management and validation.
* This form includes two fields: Email and Age, both of which are required.
*
* Features:
* - Email validation using Yup's `.email()` and `.required()` rules.
* - Age validation that ensures:
* - the input is numeric (transforms empty string to NaN),
* - it's a whole number (integer),
* - it's positive,
* - and it's required.
* - Inline styles for a clean and modern appearance.
* - Error messages shown below each field using <ErrorMessage>.
* - Debug info (`isValid`, `touched`, `errors`) displayed using <pre> block.
* - Form resets on successful submission.
*
* @usage
* <TestSubmit />
*
* @note
* - Age input uses Yup's `.transform()` to handle empty strings correctly, ensuring validation works with edge cases like decimal input.
* - All logic is contained in a single component for simplicity.
*/
import React from 'react';
import { Formik, Form, Field, ErrorMessage } from 'formik';
import * as Yup from 'yup';
// Validation Schema
const validationSchema = Yup.object({
email: Yup.string()
.email('Invalid email format')
.required('Email is required'),
age: Yup.number()
.transform((value, originalValue) =>
String(originalValue).trim() === '' ? NaN : Number(originalValue)
)
.typeError('Age must be a number')
.integer('Age must be a whole number')
.positive('Age must be a positive number')
.required('Age is required'),
});
const styles = {
formContainer: {
maxWidth: '400px',
margin: '40px auto',
padding: '24px',
border: '1px solid #ccc',
borderRadius: '10px',
fontFamily: 'Arial, sans-serif',
boxShadow: '0 0 10px rgba(0,0,0,0.1)',
backgroundColor: '#f9f9f9',
},
fieldGroup: {
marginBottom: '16px',
},
label: {
display: 'block',
marginBottom: '6px',
fontWeight: 'bold',
},
input: {
width: '100%',
padding: '8px',
borderRadius: '4px',
border: '1px solid #ccc',
},
errorText: {
color: 'red',
fontSize: '12px',
marginTop: '4px',
},
submitButton: {
padding: '10px 20px',
backgroundColor: '#4caf50',
color: 'white',
border: 'none',
borderRadius: '6px',
cursor: 'pointer',
},
};
const TestSubmit = () => {
return (
<Formik
initialValues={{ email: '', age: '' }}
validationSchema={validationSchema}
onSubmit={(values, { resetForm }) => {
console.log('✅ Form submitted successfully:', values);
alert('Form submitted successfully');
resetForm();
}}
>
{({ isValid, touched, errors }) => (
<Form style={styles.formContainer} noValidate>
<div style={styles.fieldGroup}>
<label style={styles.label}>Email:</label>
<Field name="email" type="email" style={styles.input} />
<ErrorMessage name="email" component="div" style={styles.errorText} />
</div>
<div style={styles.fieldGroup}>
<label style={styles.label}>Age:</label>
<Field name="age" type="number" style={styles.input} />
<ErrorMessage name="age" component="div" style={styles.errorText} />
</div>
<button type="submit" style={styles.submitButton}>
Submit
</button>
{/* Debug output */}
<pre>{JSON.stringify({ isValid, touched, errors }, null, 2)}</pre>
</Form>
)}
</Formik>
);
};
export default TestSubmit;