【发布时间】:2021-07-05 04:36:28
【问题描述】:
我有一个包含模板的部分,现在我希望用户选择其中一个模板并编辑模板中的数据,例如姓名、职位、联系方式等。
这是我在代码沙箱中的现场演示editor live demo
这里是templates.js
export default function Templates() {
const [selected, setSelected] = useState("");
let history = useHistory();
const handleSelected = (e) => {
const value = e.target.innerHTML;
setSelected(value);
history.push('/Settings')
};
return (
<div className="App">
<h2>Select one of the Template below by clicking title</h2>
<div className={`template_one ${selected === "Template_one" ? "selected" : "unselected"}`} onClick={(e) => {setSelected(selected !== "Template_one" ? "Template_one" : ""); handleSelected(e);}}>
<h1>Template_one</h1>
<table striped bordered hover size="sm">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Job</th>
<th>Facebook</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Mark</td>
<th>Developer</th>
<td>facebook/Mark</td>
</tr>
</tbody>
</table>
</div>
<div className={`template_two ${selected === "Template_two" ? "selected" : "unselected"}`} onClick={(e) => {setSelected(selected !== "Template_two" ? "Template_two" : "");handleSelected(e);}}>
<h1>Template_two</h1>
<table striped bordered hover size="sm">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Job</th>
<th>Company</th>
<th>Contact </th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Trump</td>
<th>President</th>
<td>Trump Organization</td>
<td>+1 728 256 345</td>
</tr>
</tbody>
</table>
</div>
</div>
);
}
这里是 settings.js
import React from "react";
export default function Settings() {
return (
<div>
<h1>Settings</h1>
<div className="Settings">
<form>
Name: <input type="text" name="firstname" />
Job: <input type="text" name="job" /> <br />
Facebook: <input type="text" name="facebook" />
Company: <input type="text" name="company" />
Contact: <input type="text" name="contact" />
</form>
<div className="selected-template">
Here should be the selected template
</div>
</div>
</div>
);
}
预期结果:
问题:我不知道如何在设置组件中显示选择的模板。
如何将选定的模板复制到设置组件,以便用户可以编辑选定的模板?
【问题讨论】:
标签: javascript html reactjs forms react-hooks