我是一名新的 ReactJS 开发人员,希望创建一个包含输入字段的容器,您可以在其中添加/删除新字段。我希望所有这些都位于可滚动容器中,但相反,我的代码添加了新输入,但移动了页面的其余部分,并且不保留在容器中。下面是react和css。css 还有一些我稍后将包含的字段
反应
import React, { useState } from "react";
import './Dropdown.css'
function Dropdown() {
const [inputList, setInputList] = useState([{ courseName: "", courseRating: "" }]);
// handle input change
const andleInputChange = (e, index) => {
const { name, value } = e.target;
const list = [...inputList];
list[index][name] = value;
setInputList(list);
};
// handle click event of the Remove button
const handleRemoveClick = index => {
const list = [...inputList];
list.splice(index, 1);
setInputList(list);
};
// handle click event of the Add button
const handleAddClick = () => {
setInputList([...inputList, { courseName: "", courseRating: "" }]);
};
return (
<div className="base-container-drop">
<div className="content-drop">
{inputList.map((x, i) => {
return (
<div className="container-for-drop">
<div className="form-drop">
<div className="form-group-drop">
<label className="label-drop" htmlFor="courseName"></label>
<input className="input-drop"
name="courseName"
placeholder="Course Name"
value={x.firstName}
onChange={e => handleInputChange(e, i)}
/>
</div>
<div className="form-group-drop-rating">
<label className="label-drop" htmlFor="courseRating"></label>
<input className="input-drop"
type="number"
name="courseRating"
placeholder="0"
max='10'
min='0'
value={x.lastName}
onChange={e => handleInputChange(e, i)}
/>
</div>
);
})}
</div>
</div>
);
}
桃花长相依
相关分类