So I have this field where you can enter the quantity. It was working, however, when I added another input field for the pick, it will just duplicate whatever was entered in either of the fields. How do I fix this? Any help would be appreciated. Thank you.
codesandbox: Add to Cart Sampled 2 with Material-UI Table Pagination - CodeSandbox
adding into the cart items:
const handleAdd = (id, name, price, size, cat, color, quan = null, pick) => {
console.log("add", id, name, price, size, cat, color, quan, pick);
const productExist = cartItems.find(
(item) => item.id === id && item.color === color
);
if (productExist) {
setCartItems(
cartItems.map((item) =>
item.id === id && item.color === color
? {
...productExist,
quantity:
quan === "" || quan ? quan : +productExist.quantity + 1
}
: item
)
);
} else {
setCartItems([
...cartItems,
{ id, name, price, size, cat, color, quantity: 1, pick }
]);
}
};
Table for the cart items:
<TableContainer component={Paper}>
<Table
aria-label="simple table"
size="small"
aria-label="a dense table"
>
//table Head
<TableBody>
<input
style={{ width: "1rem" }}
value={item.quantity}
onChange={(e) => {
if (e.target.value === "0") {
return handleRemove(item.quantity + item.color);
}
const re = /^[0-9\b]+$/;
if (
e.target.value === "" ||
re.test(e.target.value)
) {
handleAdd(
item.id,
item.prodName,
item.price,
item.size,
item.cat,
item.color,
e.target.value
);
}
}}
/>
</TableCell>
<TableCell>
<label>Pick</label>
<input
style={{ width: "5rem" }}
value={item.pick}
onChange={(e) => {
console.log(e.target.value, "esdf");
return handleAdd(
item.id,
item.prodName,
item.price,
item.size,
item.cat,
item.color,
e.target.value
);
}}
/>
</TableCell>
<TableCell align="right">
</TableBody>
</Table>
</TableContainer>