I've been trying to build a select box that will be added dynamically from a button click. I can add and remove the select boxes, but the issue is, whenever I remove an item from the array, the selected option on the remaining elements does not update accordingly. if I remove an element from the last index, it's working perfectly, the problem occurs whenever the item removes from the beginning or in the middle. here's what I have so far.
type RailcardsProps = {
value: string,
fieldName: string,
count: string;
}
const railCardsList = [
{
text: 'Select Rail card',
value: ''
},
{
text: 'Child Saver',
value: 'childSaver'
},
{
text: 'Adult Saver',
value: 'adultSaver'
},
{
text: 'Senior Railcard',
value: 'seniorRailcard'
},
{
text: 'Network Railcard',
value: 'networkRailcard'
},
]
const PassangerDropdown = () => {
const [railCards, setRailCards] = useState<RailcardsProps[]>([]);
const handleRailCardCLick = () => {
const railCardsArray = railCards;
const count = railCardCount;
setRailCards(railCardsArray.concat({
value: 'Select Rail Card',
fieldName: `railcard-${railCardCount}`,
count: '1'
}));
setRailCardCount(count + 1);
}
useEffect(() => {
console.log(railCards);
}, [railCards])
const handleSelectRailcard = (event: ChangeEvent<HTMLSelectElement>) => {
const value = (event.target.value);
const updatedRailCards = railCards;
const index = event.target.getAttribute('data-index')!;
updatedRailCards[+index].value = value;
setRailCards([...updatedRailCards]);
}
const handleRailcardCount = (event: ChangeEvent<HTMLSelectElement>) => {
const count = (event.target.value);
const updatedRailCards = railCards;
const index = event.target.getAttribute('data-index')!;
updatedRailCards[+index].count = count;
setRailCards([...updatedRailCards]);
}
const handleRemoveRailcard = (index: number) => {
const updatedRailCards = railCards;
const count = railCardCount;
updatedRailCards.splice(index, 1);
setRailCardCount(count - 1);
console.log(index);
setRailCards([...updatedRailCards]);
}
return (
<div className="passanger-dropdown-item--left w-100">
<button type="button" className="btn custom-link mb-2" onClick={handleRailCardCLick}>Add Railcards</button>
{
railCards.map(({ value, fieldName, count }, index) => (
<div key={index} className="passanger-dropdown-item--content p-0">
<select
defaultValue={value}
name={fieldName}
className="form-control passanger-dropdown-item--select w-70 mr-1"
data-index={index}
onChange={handleSelectRailcard} >
{
railCardsList.map(({ value, text }) => (
<option key={value} defaultValue={value}>{text}</option>
))
}
</select>
<select
defaultValue={count}
name={`${fieldName}-count`}
className="form-control passanger-dropdown-item--select w-30"
data-index={index}
disabled={value === 'Select Rail Card'}
onChange={handleRailcardCount} >
{
['1', '2', '3', '4', '5', '6', '7', '8', '9'].map((item) => (
<option key={item} defaultValue={item}>{item}</option>
))
}
</select>
<button
type="button"
className="btn passanger-dropdown-item--btn"
onClick={() => handleRemoveRailcard(index)}>x</button>
</div>
))
}
</div>)
}
in this site passenger dropdown kind of thing what I'm trying to achieve, in the passenger dropdown we can add or remove rail cards, How can I solve this issue?