import { useState } from "react"
import { createProfile } from "../endpoints/endpoints";
export default function CreateProfile() {
const [user, setUser] = useState({})
const handleChange = (e) => {
setUser({...user, [e.target.name]: e.target.value});
}
const handleSubmit = (e) => {
e.preventDefault();
createProfile(user)
.then(res => {
window.location.href="/login";
})
.catch(e => console.log(e))
}
return (
<>
<h1>Create Profile</h1>
<form>
<label>Enter name:</label>
<input type="text" name="name" onChange={handleChange} />
<br/>
<label>Enter role:</label>
<input type="text" name="role" onChange={handleChange} />
<br/>
<label>Enter username:</label>
<input type="text" name="username" onChange={handleChange} />
<br/>
<label>Enter password:</label>
<input type="password" name="password" onChange={handleChange} />
<br/>
<button onClick={handleSubmit}>Create</button>
</form>
</>
)
}
import React from "react"
export default function Form() {
const [formData, setFormData] = React.useState(
{
firstName: "",
lastName: "",
email: "",
comments: "",
isFriendly: true,
employment: "",
favColor: ""
}
)
function handleChange(event) {
const {name, value, type, checked} = event.target
setFormData(prevFormData => {
return {
...prevFormData,
[name]: type === "checkbox" ? checked : value
}
})
}
function handleSubmit(event) {
event.preventDefault()
// submitToApi(formData)
console.log(formData)
}
return (
<form onSubmit={handleSubmit}>
<input
type="text"
placeholder="First Name"
onChange={handleChange}
name="firstName"
value={formData.firstName}
/>
<input
type="text"
placeholder="Last Name"
onChange={handleChange}
name="lastName"
value={formData.lastName}
/>
<input
type="email"
placeholder="Email"
onChange={handleChange}
name="email"
value={formData.email}
/>
<textarea
value={formData.comments}
placeholder="Comments"
onChange={handleChange}
name="comments"
/>
<input
type="checkbox"
id="isFriendly"
checked={formData.isFriendly}
onChange={handleChange}
name="isFriendly"
/>
<label htmlFor="isFriendly">Are you friendly?</label>
<br />
<br />
<fieldset>
<legend>Current employment status</legend>
<input
type="radio"
id="unemployed"
name="employment"
value="unemployed"
checked={formData.employment === "unemployed"}
onChange={handleChange}
/>
<label htmlFor="unemployed">Unemployed</label>
<br />
<input
type="radio"
id="part-time"
name="employment"
value="part-time"
checked={formData.employment === "part-time"}
onChange={handleChange}
/>
<label htmlFor="part-time">Part-time</label>
<br />
<input
type="radio"
id="full-time"
name="employment"
value="full-time"
checked={formData.employment === "full-time"}
onChange={handleChange}
/>
<label htmlFor="full-time">Full-time</label>
<br />
</fieldset>
<br />
<label htmlFor="favColor">What is your favorite color?</label>
<br />
<select
id="favColor"
value={formData.favColor}
onChange={handleChange}
name="favColor"
>
<option value="red">Red</option>
<option value="orange">Orange</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="indigo">Indigo</option>
<option value="violet">Violet</option>
</select>
<br />
<br />
<button>Submit</button>
</form>
)
}
const COLORS = ['white', 'red', 'blue', 'black', 'cream'];
function RegisterYourCatForm() {
return (
<form>
<h2>Register Your Cat</h2>
<label>Name*:</label>
<input />
<label>Color*:</label>
<select>
<option value="">Select color</option>
{COLORS.map(c => <option key={c}>{c}</option>)}
</select>
<label>Age*:</label>
<input />
<label>Habits:</label>
<textarea />
<button type="submit">Submit</button>
</form>
);
}
const [inputs, setInputs] = useState({});
const handleChange = (event) => {
const name = event.target.name;
const value = event.target.value;
setInputs(values => ({...values, [name]: value}))
}
<form>
<label>
Nome:
<input type="text" name="name" />
</label>
<input type="submit" value="Enviar" />
</form>
https://dmitripavlutin.com/react-forms-tutorial/