I have an input and when placing a number x it is generating dynamic selects descendingly, and I need to create a new input where the value that I select from each dynamic select is added dynamically, I have tried but I have not been able to achieve it, I have not been able to capture the values of the select and I have tried several ways and I have not been able to, if someone can help me I would appreciate it, the only thing I need is for there to be a new input where the sum of the values that I am selecting from each dynamic select that is created.
https://codesandbox.io/s/green-silence-p91q1?file=/src/App.js
import React, { useState } from "react";
let initialState = {
first: null,
arraySelect: []
};
const Test = () => {
const [arraySelect, setarraySelect] = useState(initialState.arraySelect);
const [numberIni, setnumberIni] = useState(initialState.first);
const [selectedNumbers, setSelectedNumbers] = useState([]);
const getArray = (value) => {
const numValue = parseInt(value, 10);
const arr = [];
for (let i = 0; i < numValue - 1; i++) {
arr.push(numValue - i - 1);
}
if (arr.length) {
return arr;
}
};
const setSelect = (value) => {
let isArray = getArray(value);
if (isArray) {
setarraySelect([...arraySelect, isArray]);
}
};
const handleChange = (index, value) => {
const tmpSelectedNumbers = [...selectedNumbers];
tmpSelectedNumbers[index] = value;
setSelectedNumbers(tmpSelectedNumbers);
};
const handleSubmit = (event) => {
event.preventDefault();
setnumberIni(event.target.numberIni.value - 1);
};
const resetForm = () => {
setnumberIni(null);
setarraySelect([]);
};
return (
<div>
<form onSubmit={handleSubmit}>
<input name="numberIni" type="number" />
<input type="submit" value="Generar select" />
</form>
{numberIni && (
<div>
<select
onChange={(e) => handleChange(0, e.target.value)}
name=""
id=""
>
<option value="seleccione">Seleccione</option>
{Array(parseInt(numberIni))
.fill(1)
.map((value, key) => {
return (
<option value={numberIni - key}>{numberIni - key}</option>
);
})}
</select>
<button
type="submit"
className="btn btn-success"
onClick={() => {
setSelect(selectedNumbers[0]);
}}
>
crear
</button>
{Array(parseInt(numberIni))
.fill(1)
.map((value, key2) => {
return (
<div>
{arraySelect[key2] && (
<>
<select
onChange={(e) => handleChange(key2 + 1, e.target.value)}
name=""
id=""
>
<option value="seleccione">Seleccione</option>
{arraySelect[key2].map((value, key3) => {
return (
<option value={arraySelect[key2][key3]}>
{arraySelect[key2][key3]}
</option>
);
})}
</select>
<button
type="submit"
className="btn btn-success"
onClick={() => {
setSelect(selectedNumbers[key2 + 1]);
}}
>
crear
</button>
</>
)}
</div>
);
})}
</div>
)}
{numberIni && (
<input onClick={() => resetForm()} type="button" value="Reiniciar" />
)}
</div>
);
};
export default Test;
What you are trying to do can be done in 2 possible ways:
Since I wasn't sure when you wanted to do it, I did both and you take the one that best suits your needs.
The most important thing is how you are storing the information, in your case it is an array
selectedNumbers
. So my recommendation is to simply add all the values in the array. I have done it with areduce
Documentation here . But in the end you can do it even with onefor
orforEach
Step 1: Get the value
reduce
forEach
for
Step 2: When to set it?
If you have to change it every time a select changes, then it's so simple you won't have to do anything (see my full code
acc
)If you have to change it After they hit create then you need an additional variable that has this value and you update it every time they hit the create button. (see my full code
confirmedAcc
)Full Code: