next-app/app/components/form.tsx
Alex Stan fffda90529 changed the topic card, the form and .gitignore (#9)
Co-authored-by: Alex Stan <alex.stan.2010@proton.me>
Co-committed-by: Alex Stan <alex.stan.2010@proton.me>
2024-06-01 16:57:47 +03:00

48 lines
1.1 KiB
TypeScript

'use client';
import { useState } from "react";
const Form = <T,>({ initialData, submitFunction }) => {
const [formData, setFormData] = useState( initialData );
const handleSubmit = async (event) => {
event.preventDefault();
//q = formData;
//addQuestion( q );
//console.log( `Adding question ${JSON.stringify(q)}` );
await submitFunction( formData );
}
const handleChange = (event) => {
const { name, value } = event.target;
setFormData( (prevData) => ({
...prevData,
[name]: value
}));
}
return (
<form onSubmit={handleSubmit} className="flex">
<div>
{Object.entries( initialData ).map( (elem) => {
return(
<input
type = { typeof( elem[1] ) == typeof( 0 ) ? "number" : "text" /* pretty hacky, we should change this */ }
id = {elem[0]}
name = {elem[0]}
value = {formData[elem[0]]}
onChange = {handleChange}
required
/>)})}
</div>
<button type="submit">Submit</button>
</form>
);
}
export default Form;