47 lines
1.1 KiB
TypeScript
47 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;
|
|
|