I am new on React and these state values are going to kill me! What I am trying to do is add input with select optionsand each select optionis connected to its inputand when I delete on of them I want to delete them together but this is not happening.
When I delete any one, only the last line is deleted and the select option does not stick with the same input.
This is my code:
import React, { Component } from 'react';
class SocialMedia extends Component {
constructor() {
super();
this.state = {
name: '',
SocialData: [],
socialArray: [
{
"id": 1,
"name": "Website",
"regex": "(https|http):\\/\\/(www\\.)*"
},
{
"id": 4,
"name": "Last.fm",
"regex": "(https|http):\\/\\/www\\.(lastfm\\.com|last\\.fm\\/|lastfm\\.ch).*"
},
{
"id": 5,
"name": "Facebook",
"regex": "https:\\/\\/www\\.facebook\\.com\\/.*"
},
{
"id": 6,
"name": "Twitter",
"regex": "(https|http):\\/\\/(twitter\\.com).*"
},
{
"id": 8,
"name": "Instagram",
"regex": "https:\\/\\/(instagr\\.am\\/.*|instagram\\.com\\/.*)"
},
{
"id": 9,
"name": "YouTube Channel",
"regex": "((http|https):\\/\\/|)(www\\.)?youtube\\.com\\/(channel\\/|user\\/)[a-zA-Z0-9]{1,}"
},
{
"id": 11,
"name": "YouTube Video",
"regex": "^.*(youtu.be\\/|v\\/|e\\/|u\\/\\w+\\/|embed\\/|v=)([^#\\&\\?]*).*"
},
{
"id": 12,
"name": "Spotify",
"regex": "spotify\\:+"
}
]
};
this.handleAddSocial = this.handleAddSocial.bind(this);
this.handleInputVlaueChange = this.handleInputVlaueChange.bind(this);
this.handleRemoveSocial = this.handleRemoveSocial.bind(this);
this.selectHandler = this.selectHandler.bind(this);
}
handleAddSocial () {
let array = this.state.SocialData;
array.push({ id: array.length+1 , socialname: '' })
this.setState({ SocialData: array})
}
handleInputVlaueChange (e ) {
console.log(e.target.value)
}
handleRemoveSocial (e) {
let someArray = this.state.SocialData;
someArray.splice( someArray.indexOf(e), 1);
this.setState({ SocialData: someArray })
}
render() {
return (
<div >
<button
className="newFlyerButton btn mb-4"
type="button"
onClick={this.handleAddSocial}
>
<span>
<img src={plus} alt="plus" className="newFlyerPlus"/>
<span className="buttonText">ADD NEW</span>
</span>
</button>
<table className="table mt-3 bordered table-hover white-table addNewSocial">
<tbody>
{this.state.SocialData.map((Social, idx) => (
<tr key={idx} className="row Social">
<td className="col-6 socialInput">
<input
type="text"
placeholder={`Add New # ${idx+1}`}
value={Social.name}
onChange={this.handleInputVlaueChange}
/>
</td>
<td className="col-4 socialSelector">
<select defaultValue="SelectOption">
<option value="SelectOption" disabled >Select your option</option>
{ this.state.socialArray.map(socidata =>
<option
value={socidata.name}
data={socidata}
key={socidata.id}
>
{socidata.name}
</option>
)}
</select>
</td>
<td className="col-2 closingLink">
<i className="fas fa-fw fa-times" onClick={ () => this.handleRemoveSocial(idx) }></i>
</td>
</tr>
))}
</tbody>
</table>
</div>
)
}
}
export default SocialMedia;
How can I connect each social media selection with the same input in line?