I have a table of text boxes and want a given text box to highlight when I change it. I have the CSS class but am not able to apply it to a specific field. Currently the setChanged() makes the entire table highlighted. I should be able to focus it to a given field given that I have access to the ID and the field name within the event handler righ?
function AppCookie() {
const [cookies, setCookies] = useState();
const [changed, setChanged] = useState('');
const onChangeInput = (e, id) => {
const { name, value } = e.target
setChanged('highlighted-input')
const editData = cookies.map((cookie) =>
cookie.id === id && name ? { ...cookie, [name]: value } : cookie
)
editData[id-1].name
setCookies(editData)
}
const handleClick = async (event) => {
...}
useEffect(() => {
populateCookieData();
}, []);
const contents = cookies === undefined
? <p><em>Loading...</em></p>
:
<table className="table table-striped" aria-labelledby="tabelLabel">
<thead>
<tr>
<th>Id</th>
<th>Date</th>
<th>Name</th>
<th>Desc</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{cookies.map(cookie =>
<tr key={cookie.id}>
<td>{cookie.id}</td>
<td>{cookie.date}</td>
<td>
<input
name="name"
value={cookie.name}
type="text"
onChange={(e) => onChangeInput(e,
cookie.id)}
placeholder="Type Name"
className={changed} />
</td>
<td><input
name="desc"
value={cookie.desc}
type="text"
onChange={(e) => onChangeInput(e, cookie.id)}
placeholder="Type Desc"
/></td>
<td><input
name="price"
value={cookie.price}
type="text"
onChange={(e) => onChangeInput(e, cookie.id)}
placeholder="Type Price"
className={changed}/></td>
<td> <button type="submit" onClick={() => handleClick({cookie})}>Update</button></td>
</tr>
)}
</tbody>
</table>;
return (
<div>
<h1 id="tabelLabel">Cookies</h1>
{contents}
</div>
);
async function populateCookieData() {
...
}
}