Created
July 24, 2023 06:51
-
-
Save alexsoyes/82af5eacc9c91489b97a2add0dcbf9d7 to your computer and use it in GitHub Desktop.
Formulaire de contact généré par ChatGPT 3.5
This file contains hidden or 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
/* | |
Agis comme un développeur senior sur JavaScript, ta spécialité est React et tu as fait partie de l'équipe qui a crée la lib. | |
Je suis développeur React depuis 5 ans, spécialisé dans TypeScript et JavaScript. | |
J'aimerais un composant qui permet de créer un formulaire de contact avec nom, prénom, email et message. | |
Pour cela: | |
- utilise la derniere version de TypeScript | |
- utilise la derniere version connue de React | |
- n'utilise pas de lib tierce | |
- respecte bonnes pratiques de React | |
- applique le clean code | |
*/ | |
import React, { useState } from 'react'; | |
interface ContactFormProps { | |
onSubmit: (formData: FormData) => void; | |
} | |
interface FormData { | |
firstName: string; | |
lastName: string; | |
email: string; | |
message: string; | |
} | |
const ContactForm: React.FC<ContactFormProps> = ({ onSubmit }) => { | |
const [formData, setFormData] = useState<FormData>({ | |
firstName: '', | |
lastName: '', | |
email: '', | |
message: '', | |
}); | |
const handleChange = (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { | |
const { name, value } = event.target; | |
setFormData((prevFormData) => ({ | |
...prevFormData, | |
[name]: value, | |
})); | |
}; | |
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => { | |
event.preventDefault(); | |
onSubmit(formData); | |
}; | |
return ( | |
<form onSubmit={handleSubmit}> | |
<div> | |
<label htmlFor="firstName">Prénom:</label> | |
<input | |
type="text" | |
id="firstName" | |
name="firstName" | |
value={formData.firstName} | |
onChange={handleChange} | |
required | |
/> | |
</div> | |
<div> | |
<label htmlFor="lastName">Nom:</label> | |
<input | |
type="text" | |
id="lastName" | |
name="lastName" | |
value={formData.lastName} | |
onChange={handleChange} | |
required | |
/> | |
</div> | |
<div> | |
<label htmlFor="email">Email:</label> | |
<input | |
type="email" | |
id="email" | |
name="email" | |
value={formData.email} | |
onChange={handleChange} | |
required | |
/> | |
</div> | |
<div> | |
<label htmlFor="message">Message:</label> | |
<textarea | |
id="message" | |
name="message" | |
value={formData.message} | |
onChange={handleChange} | |
required | |
/> | |
</div> | |
<button type="submit">Envoyer</button> | |
</form> | |
); | |
}; | |
export default ContactForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment