建立一个可以包含多行的表格格式的表单输入,需要将多行输入的值作为对象存储在数组中(教育如代码所示)。
我很难根据教育索引将相应数据存储在教育数组中 如何解决这个问题?它是如何完成的?
const AddEducationDetails: React.FC = () => {
const [showLevel, setShowLevel] = useState("upto 10");
const [educations, setEducations] = useState<Education[]>([
{
level: "",
studiedCourse: "",
studiedAt: "",
studiedFrom: "",
passedYear: "",
board: "",
university: "",
file: "",
},
]);
const handleInputChange = () => {};
const handleLevelSelection = (
e: React.ChangeEvent<HTMLSelectElement>,
index: number,
) => {
const { name, value } = e.target;
setShowLevel(value);
console.log(index);
};
return (
<table className="min-w-full overflow-hidden rounded-lg bg-[#373747]">
//thead
<tbody>
{educations.map((education, index) => (
<tr key={index} className="border border-gray-600">
<td className="px-4 py-2">
<select
name="level"
onChange={(e) => handleLevelSelection(e, index)}
className="mb-4 w-[110px] flex-1 rounded-md border-0 bg-gray-700 p-2 text-gray-200 focus:bg-gray-600 focus:outline-none focus:ring-1 focus:ring-blue-500"
>
<option value="upto 10">Upto 10</option>
<option value="+2">+2</option>
<option value="above +2">Above +2</option>
</select>
</td>
<td className="px-4 py-2">
<Input
type="text"
value={education.studiedCourse}
onChange={handleInputChange}
/>
</td>
//multiple td with form inputs
</tr>
))}
</tbody>
</table>
);
};