【发布时间】:2020-10-03 15:45:14
【问题描述】:
我创建了一个包含公式的表格,我希望每个单元格在onBlur 时显示公式的结果,并在onFocus 时显示公式本身。
- 但问题是,当我单击任何单元格 => 具有值
25的单元格也会受到影响,尽管我希望单元格25或"=age+3"仅在精确单击时才会生效(focus on it)
import React, { useState } from "react";
import "./styles.css";
const table = [
{ id: 1, row: ["name", "age", "email",'formula'] },
{ id: 2, row: ["Ali", 23, "alisci@yahoo.com", "=age+3"] },
{ id: 3, row: ["alex", 22, "alexsci@yahoo.com",'22'] }
];
export default function App() {
const [focused, setFocused] = useState(false);
const age = table[2].row[1];
return (
<table className="App">
{table.map((item, index) => (
<tr key={index}>
{item.row.map((cell, index) => {
return (
<td
onFocus={(e) => setFocused(true)}
onBlur={(e) => setFocused(false)}
contentEditable
key={index}
>
{cell[0] === "=" & !focused ? eval(cell.slice(1)) : cell}
</td>
);
})}
</tr>
))}
</table>
);
}
【问题讨论】:
标签: reactjs react-hooks rendering