You can add more text fields of size, color, and stocks. If I’ll add more sizes, the values for the color and stocks will duplicate from what was first entered. But there’s no duplicate forExample:
Expected output:
1st Size : small
color: red, stocks: 10
color: green, stocks: 3
2nd Size: medium
color: white, stocks: 3
color: red, stocks: 6 the sizes field.
What it currently does is in the 2nd size, it will just duplicate whatever value was entered from the first size. How can I fix this? Thank you.
Link: https://codesandbox.io/s/form-2-add-more-size-ddqqo?file=/demo.js
import React, { useState, useEffect } from "react";
import Box from "@mui/material/Box";
import { TextField, Button } from "@mui/material";
export default function BasicSelect() {
const [productName, setProductName] = useState();
const [sizeList, setSizeList] = useState([{ size: "" }]);
const [colorList, setColorList] = useState([{ color: "", colorStocks: "" }]);
//sizes
const handleServiceChange = (e, index) => {
const { name, value } = e.target;
const list = [...sizeList];
list[index][name] = value;
setSizeList(list);
};
const handleServiceRemove = (index) => {
const list = [...sizeList];
list.splice(index, 1);
setSizeList(list);
};
const handleServiceAdd = () => {
setSizeList([...sizeList, { service: "" }]);
};
// color
const handleColorChange = (e, index) => {
const { name, value } = e.target;
const list = [...colorList];
list[index][name] = value;
setColorList(list);
// console.log(colorList);
};
const handleColorStocksChange = (e, index) => {
const { name, value } = e.target;
const list = [...colorList];
list[index][name] = value;
setColorList(list);
// console.log(colorList);
};
// const handleColorChange = (e, index) => {
// const { value } = e.target;
// const arr = [...colorList]; //Shallow copy the existing state
// arr[index].color = value; //Update the size to the selected size
// console.log(arr[index].value);
// setColorList([...arr]); //Set the updated array to be the new state
// };
// const handleColorStocksChange = (e, index) => {
// const { value } = e.target;
// console.log(value);
// const arr = [...colorList];
// arr[index].colorStocks = value;
// // console.log(arr)
// setColorList([...arr]);
// };
const handleColorRemove = (index) => {
const list = [...colorList];
list.splice(index, 1);
setColorList(list);
};
const handleColorAdd = () => {
setColorList([...colorList, { color: "", colorStocks: "" }]);
};
const handleSubmit = async (e) => {
e.preventDefault();
console.log("Product: ", productName, "size: ", sizeList, colorList);
};
return (
<Box sx={{ minWidth: 120 }}>
<form onSubmit={handleSubmit}>
<TextField
label="Product Name"
name="name"
type="text"
id="productName"
value={productName}
onChange={(e) => setProductName(e.target.value)}
required
/>
{sizeList.map((singleSize, index) => (
<div key={index}>
<TextField
label="Size"
name="size"
type="text"
id="size"
required
value={singleSize.size}
onChange={(e) => handleServiceChange(e, index)}
/>
{colorList.map((singleColor, index) => (
<div key={index}>
<TextField
label="color"
name="color"
type="text"
id="color"
required
value={singleColor.color}
onChange={(e) => handleColorStocksChange(e, index)}
/>
<TextField
label="Stocks"
name="colorStocks"
type="text"
id="colorStocks"
required
value={singleColor.colorStocks}
onChange={(e) => handleColorChange(e, index)}
/>
{colorList.length !== 1 && (
<Button onClick={() => handleColorRemove(index)}>
Remove
</Button>
)}
<br />
{colorList.length - 1 === index && (
<Button onClick={handleColorAdd}>Add Color</Button>
)}
<br /> <br />
{/* add or remove sizes */}
</div>
))}
{sizeList.length - 1 === index && (
<Button type="button" onClick={handleServiceAdd}>
Add size
</Button>
)}
{sizeList.length - 1 === index && (
<Button type="button" onClick={() => handleServiceRemove(index)}>
Remove Size
</Button>
)}
</div>
))}
<br />
<Button type="submit">Submit </Button>
</form>
<div className="output">
<h2>Output</h2>
<h3>Sizes:</h3>
{sizeList &&
sizeList.map((singleSize, index) => (
<ul key={index}>{singleSize.size && <li>{singleSize.size}</li>}</ul>
))}
<br />
<h3>Color:</h3>
{colorList &&
colorList.map((singleSize, index) => (
<ul key={index}>
{singleSize.color && (
<li>{singleSize.color + " - " + singleSize.colorStocks}</li>
)}
</ul>
))}
</div>
</Box>
);
}